Animation/Animator 播放结束后操作

在我的脚本中,当播放器位于平台顶部时,我将其向上移动. 它工作正常. 但是现在我想在它起床后播放剪辑"Down".

using UnityEngine;
using System.Collections;
using System.Reflection;

public class DetectPlayer : MonoBehaviour {

    GameObject target;

    public void ClearLog()
    {
        var assembly = Assembly.GetAssembly(typeof(UnityEditor.ActiveEditorTracker));
        var type = assembly.GetType("UnityEditorInternal.LogEntries");
        var method = type.GetMethod("Clear");
        method.Invoke(new object(), null);
    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.name == "Platform")
        {
            Debug.Log("Touching Platform");
        }

    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.name == "OnTop Detector")
        {
            Debug.Log("On Top of Platform");
            GameObject findGo = GameObject.Find("ThirdPersonController");
            GameObject findGo1 = GameObject.Find("Elevator");
            findGo.transform.parent = findGo1.transform;
            target = GameObject.Find("Elevator");
            target.GetComponent<Animation>().Play("Up");
        }
    }
}

线后

target.GetComponent<Animation>().Play("Up");

我想在它完成播放时播放它:

target.GetComponent<Animation>().Play("Down");

虽然这两个答案都应该有效,但使用协程和IsPlaying函数的另一种方法. 如果您还想在动画之后执行其他任务,您可以使用协程解决方案.

For the Animation system:

旧的 Unity 动画播放系统. 除非您仍在使用旧的 Unity 版本,否则不应在您的新项目中使用它.

IEnumerator playAndWaitForAnim(GameObject target, string clipName)
{
    Animation anim = target.GetComponent<Animation>();
    anim.Play(clipName);

    //Wait until Animation is done Playing
    while (anim.IsPlaying(clipName))
    {
        yield return null;
    }

    //Done playing. Do something below!
    Debug.Log("Done Playing");
}

For the Animator system

这是新的 Unity 动画播放系统. 这应该在您的新项目而不是Animation API 中使用. 在性能方面,最好使用Animator.StringToHash并通过哈希数比较当前状态,而不是比较字符串的IsName函数,因为哈希更快.

假设您有名为Jump 、 MoveLook的状态名称. 你得到他们的哈希值,然后使用函数在下面播放和等待他们:

const string animBaseLayer = "Base Layer";
int jumpAnimHash = Animator.StringToHash(animBaseLayer + ".Jump");
int moveAnimHash = Animator.StringToHash(animBaseLayer + ".Move");
int lookAnimHash = Animator.StringToHash(animBaseLayer + ".Look");

public IEnumerator PlayAndWaitForAnim(Animator targetAnim, string stateName)
{
    //Get hash of animation
    int animHash = 0;
    if (stateName == "Jump")
        animHash = jumpAnimHash;
    else if (stateName == "Move")
        animHash = moveAnimHash;
    else if (stateName == "Look")
        animHash = lookAnimHash;

    //targetAnim.Play(stateName);
    targetAnim.CrossFadeInFixedTime(stateName, 0.6f);

    //Wait until we enter the current state
    while (targetAnim.GetCurrentAnimatorStateInfo(0).fullPathHash != animHash)
    {
        yield return null;
    }

    float counter = 0;
    float waitTime = targetAnim.GetCurrentAnimatorStateInfo(0).length;

    //Now, Wait until the current state is done playing
    while (counter < (waitTime))
    {
        counter += Time.deltaTime;
        yield return null;
    }

    //Done playing. Do something below!
    Debug.Log("Done Playing");

}


对于专门针对碰撞回调函数 ( OnTriggerEnter ) 的特定问题的解决方案,有两种可能的方法:

1 、触发检测后启动协程函数播放动画:

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.name == "OnTop Detector")
    {
        Debug.Log("On Top of Platform");
        GameObject findGo = GameObject.Find("ThirdPersonController");
        GameObject findGo1 = GameObject.Find("Elevator");
        findGo.transform.parent = findGo1.transform;
        target = GameObject.Find("Elevator");
        StartCoroutine(playAnim(target));
    }
}

IEnumerator playAnim(GameObject target)
{
    Animation anim = target.GetComponent<Animation>();
    anim.Play("Up");

    //Wait until Up is done Playing the play down
    while (anim.IsPlaying("Up"))
    {
        yield return null;
    }

    //Now Play Down
    anim.Play("Down");
}

OR

2 .使OnTriggerEnter函数成为协程(IEnumerator)而不是void函数:

IEnumerator OnTriggerEnter(Collider other)
{
    if (other.gameObject.name == "OnTop Detector")
    {
        Debug.Log("On Top of Platform");
        GameObject findGo = GameObject.Find("ThirdPersonController");
        GameObject findGo1 = GameObject.Find("Elevator");
        findGo.transform.parent = findGo1.transform;
        target = GameObject.Find("Elevator");
        Animation anim = target.GetComponent<Animation>();
        anim.Play("Up");

        //Wait until Up is done Playing the play down
        while (anim.IsPlaying("Up"))
        {
            yield return null;
        }

        //Now Play Down
        anim.Play("Down");
    }
}

一种完全不需要手动检查的方法是使用排队

target.GetComponent<Animation>().PlayQueued("Down", QueueMode.CompleteOthers);

此代码将等待当前在对象上播放的任何其他动画完成,然后再播放排队的动画.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值