如何在Unity3D中平滑跳跃

如何在Unity3D中平滑跳跃,unity3d,Unity3d,我给播放器添加了一个CharacterController,但是当我测试跳转功能时,我发现播放器会立即向上移动 if (Player.isGrounded) { if (jump) { Move.y = JumpSpeed; jump = false; Player.Move (Move * Time.deltaTime); } } Move

我给播放器添加了一个CharacterController,但是当我测试跳转功能时,我发现播放器会立即向上移动

    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);`
  • 您在一帧内调用了两次
    Player.Move()
    。这可能是个问题
  • 您正在
    移动
    向量添加重力,这意味着调用此代码时,它将始终向上移动
  • Move
    这样命名变量不是一个好习惯。它在读取时会造成混乱,因为已经存在同名的方法。将其更改为
    moveDirection
  • 以下是示例代码:

    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);
        }
    }
    
    公共类示例类:单行为{
    公共浮子速度=6.0F;
    公共浮子跳跃速度=8.0F;
    公共浮子重力=20.0F;
    专用矢量3移动方向=矢量3.0;
    字符控制器;
    void Start()
    {
    控制器=GetComponent();
    }
    无效更新(){
    if(controller.isground){
    moveDirection=新矢量3(Input.GetAxis(“水平”),0,Input.GetAxis(“垂直”);
    moveDirection=transform.TransformDirection(moveDirection);
    移动方向*=速度;
    if(Input.GetButton(“跳转”))
    移动方向。y=跳跃速度;
    }
    moveDirection.y-=重力*时间增量;
    控制器移动(移动方向*时间增量);
    }
    }
    

    希望这能有所帮助。

    更广泛的代码示例将非常有用。此代码段是否位于FixeUpdate()中?你的玩家游戏对象上有刚体吗?谢谢^。^,这很有帮助