动画状态与角色的行为不符(Unity,C#)

时间:2016-06-23 10:17:49

标签: c# animation unity3d unity5

我正在创造一个简单的无尽的亚军游戏。地面由不同的碰撞器组成。动画有四种状态:

  1. 运行,
  2. 获取力量,
  3. 跳转,
  4. 大跳。
  5. 每个州都是循环的,没有退出时间。

    不幸的是,它不能很好地运作。最大的问题是,当我快速按空格键时,角色在空中处于状态0(运行动画)。有时,在空中,角色也能够获得力量。对我而言,即使角色正在跳跃,它也可以触碰到另一个对手。但是,我的预防方法不起作用。如何使这些动画状态与角色行为相匹配?

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    
    public class Move : MonoBehaviour {
    
        public Animator animator;
        Rigidbody rigidBody;
        CapsuleCollider capsuleCollider;
        int isGrounded=2;
        int jumpType;
        float force = 2;
        float jump = 20000;
        float minJump = 0.1f;
        float jumpTime;
        bool isJumping;
        void Start () 
        {
            animator = GetComponentInChildren<Animator>();
            rigidBody=GetComponent<Rigidbody>();
            capsuleCollider = GetComponent<CapsuleCollider>();
        }
    
        void Update()
        {   
        if(isJumping)
        {
            jumpTime+=Time.deltaTime;
        }
            // if the character is grounded
            if (isGrounded==1)
            {
                //jump if the key is released, jump
                if(Input.GetKeyUp(KeyCode.Space) || Input.GetMouseButtonUp(0))
                {   isJumping=true;
                    isGrounded=2;
    
                    if(rigidBody.velocity.y<30)
                    {
                        rigidBody.AddForce(Vector3.up*jump*force*0.55f);
    
                        jumpType=2;
    
                        if (force>2.5f) jumpType =3;
                        animator.SetInteger("state",jumpType);
                        force=2;
                    }
    
                }
                //if the key is pressed, add force. longer you hold, higher you jump
                else if(Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0))
                {
                    animator.SetInteger("state",1);
                    force+=Time.deltaTime*0.7f;
                }
                //just keep running 
                else if(rigidBody.velocity.y<0)
                {   
                    animator.SetInteger("state",0); 
                }
            }
    
        }
    
    
        void OnCollisionEnter(Collision collision)
        {   
            //you are grounded. additional 'if', because sometimes you touch collider after jump        
            if(isJumping && isGrounded==2)
            {   if(jumpTime>minJump)
            {
                isGrounded=1;   
                isJumping=false;
                jumpTime=0;
            }}
    
        }
    
    }
    

1 个答案:

答案 0 :(得分:0)

如果有人关心,可以通过将转换持续时间减少到0来解决问题。可以在检查员中完成。