角色对撞机仅在行走时随动画一起跳动

时间:2019-01-14 05:01:24

标签: c# unity3d animation

当他奔跑时,他陷入了空中并且可以在空中行走?这是我跳的怪人,这时我已经尝试了几种不同的跳技术,并且在这一件事上已经做了一个多星期的研究。动画本身没有错,当他空闲时,动画会播放,但CharacterController对撞机仍停留在地面上。

Animator anim;    


public float jumpSpeed = 8.0F;

     private Vector3 moveDirection = Vector3.zero;

     private CharacterController _charController;
     public float speed = 6.0f;
     public float gravity = 10f;

     private float verticalVelocity;
     private float jumpForce = 5.0f;
     private float gravityJump = 14.0f;

     void Start()
     {
         _charController = GetComponent<CharacterController>();
         anim = GetComponent<Animator>();
     }

     void Update()
     {          

         if (_charController.isGrounded)
         {
             verticalVelocity = -gravityJump * Time.deltaTime;
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 anim.SetTrigger("Jump");
                 float deltaX = Input.GetAxis("Horizontal") * speed;
                 float deltaZ = Input.GetAxis("Vertical") * speed;
                 Vector3 movement = new Vector3(deltaX, 0, deltaZ);
                 movement = Vector3.ClampMagnitude(movement, speed);

                 movement.y = gravity;

                 movement *= Time.deltaTime;
                 movement = transform.TransformDirection(movement);
                 _charController.Move(movement);
                 verticalVelocity = jumpForce;
             }
         }
         else
         {
             verticalVelocity -= gravityJump * Time.deltaTime;
         }

         Vector3 jumpVector = new Vector3(0, verticalVelocity, 0);
         _charController.Move(jumpVector * Time.deltaTime);

     }
 }

0 个答案:

没有答案