如何在Unity3D中顺利跳转

时间:2016-09-14 01:59:48

标签: unity3d

我将一个CharacterController添加到Player.But当我测试跳转功能时,我发现玩家将立即向上移动。

    if (Player.isGrounded) 
    {
        if (jump) 
        {
            Move.y = JumpSpeed;
            jump = false;
            Player.Move (Move * Time.deltaTime);
        }
    }
    Move += Physics.gravity * Time.deltaTime * 4f;
    Player.Move (Move * Time.fixedDeltaTime);`

1 个答案:

答案 0 :(得分:-1)

  1. 您在一帧中呼叫Player.Move()两次。这可能是一个问题。
  2. 将重力添加到Move向量,这意味着当您调用此代码时,它总会向上移动。
  3. 命名像Move这样的变量不是一个好习惯。它在阅读时会产生混乱,因为已经存在同名的方法。将其更改为moveDirection
  4. 以下是示例代码:

    public class ExampleClass : MonoBehaviour {
        public float speed = 6.0F;
        public float jumpSpeed = 8.0F;
        public float gravity = 20.0F;
        private Vector3 moveDirection = Vector3.zero;
        CharacterController controller;
        void Start()
        {
            controller = GetComponent<CharacterController>();
        }
    
        void Update() {
            if (controller.isGrounded) {
                moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
                moveDirection = transform.TransformDirection(moveDirection);
                moveDirection *= speed;
                if (Input.GetButton("Jump"))
                    moveDirection.y = jumpSpeed;
    
            }
            moveDirection.y -= gravity * Time.deltaTime;
            controller.Move(moveDirection * Time.deltaTime);
        }
    }
    

    希望这会有所帮助。