动画状态与角色的行为不匹配(Unity、C#)

Animation states do not match character's behavior (Unity, C#)

我正在创建一个简单的无尽 运行ner 游戏。地面由不同的对撞机组成。动画有四种状态:

  1. 运行,
  2. 发力,
  3. 跳,
  4. 大跳。

每个状态都是循环的,没有退出时间。

遗憾的是,效果不佳。最大的问题是,当我快速按下空格键时,角色在空中时处于状态 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;
        }}

    }

}

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