直接贴上代码,旋转角色角度检测碰撞
Vector2 v2Normal = new Vector2(normal.x, normal.y);
float fAngle = Vector2.SignedAngle(v2Normal, Vector2.up);
gameObject.transform.localRotation = Quaternion.Euler(, fAngle,); Vector3 startPos = new Vector3(transform.position.x, transform.position.y+, transform.position.z);
Vector3 v3Dir =transform.TransformDirection(Vector3.forward);
Vector3 endPos = startPos + v3Dir * Time.deltaTime * mMoveSpeed; // 检测是否碰到模型
RaycastHit hitInfo;
if (Physics.Linecast(startPos, endPos, out hitInfo, << LayerMask.NameToLayer("Wall")))
{
// 贴着模型行走
for (int i = ; i < ; i++)
{
fAngle += 5.0f;
gameObject.transform.localRotation = Quaternion.Euler(, fAngle, );
v3Dir = transform.TransformDirection(Vector3.forward);
endPos = startPos + v3Dir * Time.deltaTime * mMoveSpeed;
if (!Physics.Linecast(startPos, endPos, out hitInfo, << LayerMask.NameToLayer("Wall")))
{
gameObject.transform.Translate(Vector3.forward * Time.deltaTime * mMoveSpeed);
break;
}
}
}
else
{
gameObject.transform.Translate(Vector3.forward * Time.deltaTime * mMoveSpeed);
}