【发布时间】:2017-01-06 18:36:45
【问题描述】:
我想从 Unity 上的一个小 3D 平台游戏开始。当我移动时,我希望角色看向移动的方向。因此,当我按下左/“A”时,我希望角色立即左转并向前走。其他方向也一样。问题是当我离开钥匙时,角色会回到默认旋转。
重要代码:
private void FixedUpdate()
{
float inputX = Input.GetAxis("Horizontal"); // Input
float inputZ = Input.GetAxis("Vertical"); // Input
if (GroundCheck()) // On the ground?
{
verticalVelocity = -gravity * Time.deltaTime; // velocity on y-axis
if (Input.GetButtonDown("Jump")) // Jump Key pressed
{
verticalVelocity = jumpPower; // jump in the air
}
}
else // Player is not grounded
{
verticalVelocity -= gravity * Time.deltaTime; // Get back to the ground
}
Vector3 movement = new Vector3(inputX, verticalVelocity, inputZ); // the movement vector
if (movement.magnitude != 0) // Input given?
{
transform.rotation = Quaternion.LookRotation(new Vector3(movement.x, 0, movement.z)); // Rotate the Player to the moving direction
}
rigid.velocity = movement * movementSpeed; // Move the character
}
第二件事是,在
transform.rotation = Quaternion.LookRotation(new Vector3(movement.x, 0, movement.z));
y 轴上有 0。它说查看向量为零。当我传入另一个数字(如movement.y)时,角色会倾斜到地板上。所以我不知道该传递什么。
【问题讨论】:
标签: unity3d 3d game-physics