虽然两个答案都应该起作用,但是使用协程和该
IsPlaying函数执行此 *** 作的另一种方法。如果您还想在动画之后执行其他任务,则可以使用协程解决方案。
对于Animation
系统:
旧的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");}
对于Animator
系统
这是新的Unity动画播放系统。这 应该使用
在你的新项目,而不是
AnimationAPI。在性能方面,最好使用
Animator.StringToHash和通过哈希数比较当前状态,而不是使用
IsName比较字符串的函数,因为哈希更快。
比方说,你有一个名为国家的名字
Jump,
Move和
Look。您可以如下获得它们的哈希值,然后在下面使用该函数播放和等待它们:
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");}
要么
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"); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)