首页 > 解决方案 > unity on trigger enter 无法播放动画

问题描述

我是统一的 c# 菜鸟。当刚体进入设置为 Is Trigger 的盒子对撞机时,我一直在尝试触发动画“命中”。我有动画剪辑,控制台显示我有“输入触发器”。一旦发生这种情况,我似乎无法播放我的动画剪辑。顺便说一下,这是关于 3D 模型的。任何人都可以帮忙吗?

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

    public class newAttack : MonoBehaviour {

        public GameObject tiger;
        public AnimationClip attack;

        private Animation myAnimation;

      /*  IEnumerator Wait()
        {

            myAnimation = GetComponent<Animation>();
            myAnimation.Play(attack.name);
            yield return new WaitForSeconds(3);

        }
    */
        void OnTriggerEnter(Collider other)
        {
            Debug.Log("Entered Trigger");
            myAnimation = GetComponent<Animation>();
            myAnimation.Play(attack.name);
           // StartCoroutine(Wait());
        }
// Use this for initialization

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

       }
    }

标签: c#unity3d

解决方案


您可以尝试使用 Animator 并播放动画的名称(存在于 animator 中)。

代码想:

myAnimation = GetComponent<Animator>();
myAnimation.Play(attack.name);

参考:https ://docs.unity3d.com/ScriptReference/Animator.Play.html


推荐阅读