在两个空闲动画状态之间切换

时间:2017-06-16 20:12:07

标签: c# animation unity3d

我的播放器角色有一个默认状态。

但是,我想要一个二级空闲。当我按下C键时,它转到第二个空闲动画并停留在那里。当我按下X键时,它会返回默认的空闲动画。

但这就是问题的起点。当我按C键再次切换到辅助动画时,会快速跳转到第二个空闲动画并返回默认动画,而不会停留或等待任何进一步的命令。

我希望它留在我告诉它的地方。

此外,问题发生后,我再次点击C键,动画不会改变。但是当我在此时点击X键,然后在此之后再点击C键时,它再一次在动画之间来回移动。

所以我相信它'认为'当它真的没有时它被切换了。如果你能告诉我如何解决这个问题,我会成为你最好的朋友。感谢。

using UnityEngine;
using System.Collections;


public class Player : MonoBehaviour
{

private Animator anim;
Rigidbody2D rb;
public float Speed;
private bool aim = false;
private bool shot = false;
private bool idle = true;
public Transform arrowSpawn;
public GameObject arrowPrefab;
private bool idle2 = false;


void Start()
{

    rb = GetComponent<Rigidbody2D>();
    anim = GetComponent<Animator>();

}

// Update is called once per frame
void Update()
{
    Movement();
    Inputer();
    Morph (); 

}

void Movement()
{
    float moveH = Input.GetAxis("Horizontal");

    {
        rb.velocity = new Vector2 (moveH * Speed, rb.velocity.y);
    }

    anim.SetFloat ("Speed", Mathf.Abs (moveH));


}

void Inputer()
{

    if (!aim && Input.GetKeyDown (KeyCode.S)) {

        aim = true;
        anim.SetTrigger ("AIm");
    }

    if (aim && Input.GetKeyUp (KeyCode.S)) {
        shot = true;
        anim.SetTrigger ("Shot");

    }

    if (shot) {
        shot = false;
        aim = false;
        idle = true;
        anim.SetTrigger ("Idle");
        Instantiate (arrowPrefab, arrowSpawn.position, arrowSpawn.rotation);

    }
}

    void Morph()
{
    idle = !idle2;

    if (idle && Input.GetKeyDown (KeyCode.C)) {
        idle2 = true;
        anim.SetTrigger ("idle2");
    }

    if (!idle && Input.GetKeyDown (KeyCode.X)) {

        idle = true;
        anim.SetTrigger ("Idle");
        idle2 = false;

    }
}           

}

1 个答案:

答案 0 :(得分:0)

我不确定 - 当我追踪逻辑时,似乎一切正常,但是......好吧,对空闲和空闲2的处理似乎有点怀疑。

我的意思是,看看'X'键上的逻辑:

  1. 开启空闲状态
  2. 启动空闲触发器
  3. 关闭idle2
  4. ...并在'C'键上:

    1. 打开idle2
    2. 启动idle2触发器
    3. ......闲置在哪里?它似乎在等待下一个Morph()函数来设置该变量。更糟糕的是,如果在代码中的其他任何地方将idle2设置为false,则您的Morph()函数将空闲转为true ...但不会启动您的空闲触发器。

      我建议尝试其中一项,看看它们是否适合你:

      void Morph()
      {
          if (!idle && !idle2)
              anim.SetTrigger ("Idle");
          idle = !idle2;
      
          if (idle && Input.GetKeyDown (KeyCode.C)) {
              idle2 = true;
              anim.SetTrigger ("idle2");
          }
      
          if (!idle && Input.GetKeyDown (KeyCode.X)) {
              idle = true;
              anim.SetTrigger ("Idle");
              idle2 = false;
          }
      }
      

      ......或......

      void Morph()
      {
          if (idle && Input.GetKeyDown (KeyCode.C)) {
              idle2 = true;
              anim.SetTrigger ("idle2");
              idle = false;
              return;
          }
          if (!idle && Input.GetKeyDown (KeyCode.X)) {
              idle = true;
              anim.SetTrigger ("Idle");
              idle2 = false;
              return;
          }
      }