我正在尝试使用刚体和 Cinemachine 实现 3D、第三人称角色移动。角色应该能够在地面和天花板上以及在墙壁上行走(面对正 X 轴时,只能在左右墙壁上行走)。
我通过向刚体添加/更改恒定力来实现这一点,以更改重力方向并在 x 上旋转角色上下,z 左右旋转角色。
CinemachineBrain 将 World Up Override 设置为 Characters 变换,CinemchineFreeLook 绑定模式设置为“Simple Follow with World Up”。这让摄像机/电影机的轨道相对于角色的旋转进行旋转。
现在我已经在我的 PlayerMovement 脚本中实现了 4 种方法,一种用于每个重力方向(上、下、左、右),它们中的每一个都基本相同,只是改变了旋转和重力。
MoveUp() 和 MoveDown() 方法按预期工作。
但我无法让 MoveLeft() 和 MoveRight() 方法工作。
问题是角色在添加摄像机角度时无法进行 360 度旋转(此处在 x 轴上),但在面对正或负 x 时只能进行 180 度旋转(感觉就像在 y 上有一堵看不见的墙轴。
我以某种方式期望 Cinemachine / the + Camera.main.transform.eulerAngles.x; 是这里的问题,但我无法弄清楚它到底是什么。
这是我在 Unity / 游戏开发中的第一个项目 - 所以我希望我提供了所有必要的信息。
感谢您的回答。
这是我的脚本:
(我按照本教程实现了基本运动并将其更改为使用刚体:https ://youtu.be/4HpC--2iowE )
public class PlayerMovement : MonoBehaviour
{
public float walkSpeed = 5f;
public float runSpeed = 15f;
public float idleSpeed = 0f;
public string gravityDiretion = "down";
public ConstantForce gravity = new ConstantForce();
private float actualSpeed;
public float turnSmoothing = 0.1f;
float turnSmoothVelocity;
private Rigidbody character;
private void Start()
{
character = GetComponent<Rigidbody>();
gravity = gameObject.AddComponent<ConstantForce>();
}
private void FixedUpdate()
{
Move();
}
private void Move()
{
if (gravityDiretion.Equals("up"))
{
MoveUp();
}
else if (gravityDiretion.Equals("down"))
{
MoveDown();
}
else if (gravityDiretion.Equals("right"))
{
MoveRight();
}
else if (gravityDiretion.Equals("left"))
{
MoveLeft();
}
}
private void MoveUp()
{
gravity.force = new Vector3(0.0f, 9.81f, 0.0f);
if (Input.GetKey(KeyCode.LeftShift))
{
actualSpeed = runSpeed;
}
else
{
actualSpeed = walkSpeed;
}
float horizonal = Input.GetAxisRaw("Horizontal"); // 1 = left -1 righ
float vertical = Input.GetAxisRaw("Vertical"); // 1 = forward -1 bakwadrd
Debug.Log("Axis Vertical = " + vertical);
Debug.Log("Axis Horizontal = " + horizonal);
Vector3 direction = new Vector3(horizonal, 0f, -vertical).normalized; //normalize to not moving faster when pressing two keys to walk diagonal
if (direction.magnitude >= 0.1f) // If lenght of the vecctor is > 0
{
//Character Rotation
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + Camera.main.transform.eulerAngles.y; // The angle the character should turn to face forward
Debug.Log("targetAngle = " + targetAngle);
float smoothedAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, - turnSmoothing);
Debug.Log("smoothedAngle = " + smoothedAngle);
character.MoveRotation(Quaternion.Euler(180f, smoothedAngle, 0f));
//Character Movement
Vector3 moveDir = Quaternion.Euler(180f, targetAngle, 0f) * Vector3.forward;
Debug.Log("Movedir = " + moveDir.ToString());
character.MovePosition(transform.position + moveDir.normalized * actualSpeed * Time.fixedDeltaTime);
}
else
{
actualSpeed = 0f;
}
}
private void MoveDown()
{
//Gravity
gravity.force = new Vector3(0.0f, -9.81f, 0.0f);
if (Input.GetKey(KeyCode.LeftShift))
{
actualSpeed = runSpeed;
}
else
{
actualSpeed = walkSpeed;
}
float horizonal = Input.GetAxisRaw("Horizontal"); // 1 = left -1 righ
float vertical = Input.GetAxisRaw("Vertical"); // 1 = forward -1 bakward
Debug.Log("Axis Vertical = " + vertical);
Debug.Log("Axis Horizontal = " + horizonal);
Vector3 direction = new Vector3(horizonal, 0f, vertical).normalized;
if (direction.magnitude >= 0.1f)
{
//Character Rotation
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + Camera.main.transform.eulerAngles.y;
float smoothedAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothing);
character.MoveRotation(Quaternion.Euler(0f, smoothedAngle, 0f));
//Character Movement
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
Debug.Log("Movedir = " + moveDir.ToString());
character.MovePosition(transform.position + moveDir.normalized * actualSpeed * Time.fixedDeltaTime);
}
else
{
actualSpeed = 0f;
}
}
private void MoveRight()
{
//Gravity
gravity.force = new Vector3(9.81f, 0.0f, 0.0f);
if (Input.GetKey(KeyCode.LeftShift))
{
actualSpeed = runSpeed;
}
else
{
actualSpeed = walkSpeed;
}
float horizonal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Debug.Log("Axis Vertical = " + vertical);
Debug.Log("Axis Horizontal = " + horizonal);
Vector3 direction = new Vector3( 0f, horizonal, vertical).normalized;
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.y, direction.z) * Mathf.Rad2Deg + Camera.main.transform.eulerAngles.x;
Debug.Log("targetAngle = " + targetAngle);
float smoothedAngle = Mathf.SmoothDampAngle(transform.eulerAngles.x, targetAngle, ref turnSmoothVelocity, turnSmoothing);
Debug.Log("smoothedAngle = " + smoothedAngle);
character.MoveRotation( Quaternion.Euler(targetAngle, 0f , 90f));
//Character Movement
Vector3 moveDir = Quaternion.Euler(targetAngle, 0.0f, 90f) * Vector3.forward;
Debug.Log("Movedir = " + moveDir.ToString());
character.MovePosition(transform.position + moveDir.normalized * actualSpeed * Time.fixedDeltaTime);
}
else
{
actualSpeed = 0f;
}
}
private void MoveLeft()
{
//Gravity
gravity.force = new Vector3(-9.81f, 0.0f, 0.0f);
if (Input.GetKey(KeyCode.LeftShift))
{
actualSpeed = runSpeed;
}
else
{
actualSpeed = walkSpeed;
}
float horizonal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Debug.Log("Axis Vertical = " + vertical);
Debug.Log("Axis Horizontal = " + horizonal);
Vector3 direction = new Vector3(0f, horizonal, vertical).normalized;
if (direction.magnitude >= 0.1f)
{
//Character Rotation
float targetAngle = Mathf.Atan2(direction.z, direction.y) * Mathf.Rad2Deg + Camera.main.transform.eulerAngles.x;
float smoothedAngle = Mathf.SmoothDampAngle(transform.eulerAngles.x, targetAngle, ref turnSmoothVelocity, turnSmoothing);
character.MoveRotation(Quaternion.Euler(smoothedAngle, 0f, -90f));
//Character Movement
Vector3 moveDir = Quaternion.Euler(targetAngle, 0.0f, -90f) * Vector3.right;
Debug.Log("Movedir = " + moveDir.ToString());
character.MovePosition(transform.position + moveDir.normalized * actualSpeed * Time.fixedDeltaTime);
}
else
{
actualSpeed = 0f;
}
}
public void ChangeDravityDirection(string newGravityDirection)
{
gravityDiretion = newGravityDirection;
}