简体   繁体   English

如何通过 Unity 中的按键更改动画师状态?

[英]How to change animator states from a key press in Unity?

I am working on a small beat-em-up game as a summer project.我正在开发一个小型节拍游戏作为夏季项目。 I am stuck trying to change the sprite animation when a key is pressed.当按下一个键时,我试图更改精灵 animation。

Here is my code:这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    Animator anim;
    void Start()
    {
        anim = gameObject.GetComponent<Animator>();
    }

    void Update()
    {
        if (this.anim.GetCurrentAnimatorStateInfo(0).IsName("Jotaro Idle"))
        {
            if (Input.GetKey(KeyCode.D))
                anim.SetTrigger("WalkRightfromIdle");
            else if (Input.GetKey(KeyCode.A))
                anim.SetTrigger("WalkLeftFromIdle");
        }
    }
}

Here is a screenshot of my animator window (each transition is controlled by a trigger): https://imgur.com/a/f93GrAn这是我的动画师 window 的屏幕截图(每个过渡都由触发器控制): https://imgur.com/a/f93GrAn

Any help would be appreciated.任何帮助,将不胜感激。

Try using anim.Play(...) instead of anim.SetTrigger(...) .尝试使用anim.Play(...)而不是anim.SetTrigger(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM