转换之间的Unity切换

时间:2018-07-21 13:14:40

标签: c# unity3d

我有这个动画师图:

enter image description here

我希望对象的空闲状态为“空闲”,但是当用户单击空间时,我希望他“跳转”。这是我的代码

private Animation anim;
private Animator animator;

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

private void Update () {
    if (Input.GetKeyDown("space")) { }
}

现在我不知道在if语句中写什么才能播放“跳转”状态而不是“空闲”。

2 个答案:

答案 0 :(得分:0)

我创建了一个参数“ Jump”,并从条目中设置了一个条件,以仅当jump为true且用户单击空格时才允许此过渡:

    animator.SetBool("Jump",true);

答案 1 :(得分:0)

有两种方法可以实现您想要的。

首先,您可以创建动画师的参数/触发器并在动画状态之间创建过渡(在您的情况下为空闲 => 跳转),您可以在此处查看文档https://docs.unity3d.com/Manual/AnimationParameters.html

另一种方法是直接在代码中播放动画状态。

// Precalculated hash for animation state for more performance
public static int JUMP_STATE = Animator.StringToHash("Jump");
private Animator _animator;

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

void Update () {
    if(Input.GetKeyDown("Jump")){
        // Play the state directly
        _animator.Play(JUMP_STATE);
        // Or you can just call like below if you don't use precalculated hash
        // _animator.Play("Jump");
    }
}

有关更多信息,您可以检查文档https://docs.unity3d.com/ScriptReference/Animator.Play.html