【问题标题】:Character animator issue animation not working correctly角色动画师问题动画无法正常工作
【发布时间】:2015-12-02 19:39:29
【问题描述】:

嗨,我正在统一开发一款迷你 2d 游戏,我只是创建了一个 2d 角色和一些动画(Up_Idle、Down_Idle、Right_Idle、Left_Idle/Up_run、Down_run、Right_run、Left_run) 问题是这些动画不能正常工作(例如,当我按向上箭头时,字符没有播放 Up_run 动画,但他同时播放 Up_run 和 left_run 动画)

这里是截图:

代码如下:

using UnityEngine;
using System.Collections;

public class movement : MonoBehaviour
{

    public float speedX =1f;

    public float speedY =1f;

    Animator animator;

    void Start()
    {
        animator = GetComponent <Animator> ();
    }

    void Update()
    {
        if (Input.GetKey (KeyCode.UpArrow)) {
                    transform.Translate (new Vector2 (speedX, speedY) * Time.deltaTime);
                    animator.SetFloat ("Up", 1);
                    return;
        } else {
            animator.SetFloat ("Up", 0);
        }

        if (Input.GetKey (KeyCode.DownArrow)) {
            transform.Translate (new Vector2 (-speedX, -speedY) * Time.deltaTime);
            animator.SetFloat ("Down", 1);
            return;

        } else {
            animator.SetFloat ("Down", 0);
        }

        if (Input.GetKey (KeyCode.RightArrow)) {
            transform.Translate (new Vector2 (speedX, -speedY) * Time.deltaTime);
            animator.SetFloat ("Right", 1);
            return;
        } else {
            animator.SetFloat ("Right", 0);
        }

        if (Input.GetKey (KeyCode.LeftArrow)) {
            transform.Translate (new Vector2 (-speedX, speedY) * Time.deltaTime);
            animator.SetFloat ("Left", 1);
            return;
        } else {
            animator.SetFloat ("Left", 0);
        }
    } 
}

【问题讨论】:

  • 嗯..我对 Unity 也不太陌生......但我没有看到所有的转换。我不知道是否有一种方法可以在没有过渡的情况下更改动画......但你说左边的动画以某种方式工作。我的建议是检查过渡。但我可能错了。 :O
  • 我阅读屏幕截图的方式是:怠速是默认的。只有 animator.SetFloat ("Up", 1);会触发一些东西来run_up。其他动画看起来无法访问。我的动画看起来像地狱,有很多过渡哈哈

标签: c# unity3d 2d animator


【解决方案1】:

private Animator 动画师;

// Use this for initialization
void Start()
{
    animator = this.GetComponent<Animator>();

}

// Update is called once per frame
void Update()
{

    var vertical = Input.GetAxis("Vertical");
    var horizontal = Input.GetAxis("Horizontal");

    if (vertical > 0)
    {
        animator.SetInteger("Direction", 1);
         transform.Translate(Vector2.up* Time.deltaTime);
        //animator.SetInteger("Direction", 2);
        animator.SetFloat("Speed", 1.0f);
    }
    else if (vertical < 0)
    {
        animator.SetInteger("Direction", 3);
        transform.Translate(Vector3.down* Time.deltaTime);
        //animator.SetInteger("Direction", 0);
        animator.SetFloat("Speed", 1.0f);
    }
    else if (horizontal < 0)
    {
        animator.SetInteger("Direction", 4);

          transform.Translate(Vector3.left* Time.deltaTime);

        //transform.Translate(Vector2.right* Time.deltaTime);
        animator.SetFloat("Speed", 1.0f);
    }
    else if (horizontal > 0)
    {

    animator.SetInteger("Direction", 2);
    transform.Translate(Vector2.right* Time.deltaTime);
     // animator.SetInteger("Direction", 4);
    animator.SetFloat("Speed", 1.0f);
    }
    else
    {
      //animator.SetInteger("Direction", 6);
        animator.SetFloat("Speed", 0.0f);

    }
}

【讨论】:

    【解决方案2】:

    如果同时播放两个动画,则表示该条件用作两个图形的过渡。例如,您的 idle_left --> run_left 和 idle_up --> run_up 肯定具有相同的转换条件(我假设 Up == 1.0)。如果您希望动画是独立的,它们不应该具有相同的过渡条件。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      • 2021-06-11
      • 1970-01-01
      相关资源
      最近更新 更多