Unity C# - 跳跃时移动角色

Unity C# - Move character while jumping

我的角色动作很棒,跳跃也很棒。但是当他跳跃时,他只是朝着他来的方向直线移动,你不能在空中旋转或移动他。怎么做到的?

来自更新函数:

if (controller.isGrounded)
{
    moveD = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
    moveD = transform.TransformDirection(moveD.normalized) * speed;
    moveDA = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));

    if (moveDA.magnitude > 0)
    {                 
        gameObject.transform.GetChild(0).LookAt(gameObject.transform.position + moveDA, Vector3.up);
    }

    if (Input.GetButton("Jump"))
    {
        moveD.y = jumpSpeed;
    }
}

moveD.y = moveD.y - (gravity * Time.deltaTime);
controller.Move(moveD * Time.deltaTime);

controller.isGrounded 仅当您上次调用 controller.Move() 对象的对撞机底部接触表面时才为真,因此在您的情况下,一旦您跳跃,您就无法移动,直到您击中再次地面

您可以像这样将移动代码和跳跃代码分开来解决这个问题:

moveD = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
moveD = transform.TransformDirection(moveD.normalized) * speed;
moveDA = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));

if (moveDA.magnitude > 0) 
{ 
  gameObject.transform.GetChild(0).LookAt(gameObject.transform.position + moveDA, Vector3.up);
}

if (controller.isGrounded)
{
  if (Input.GetButton("Jump"))
  {
    moveD.y = jumpSpeed;
  }
}
moveD.y = moveD.y - (gravity * Time.deltaTime);
controller.Move(moveD * Time.deltaTime);