【问题标题】:Unity on trigger enter can't play animationunity on trigger enter 无法播放动画
【发布时间】:2018-07-15 21:01:15
【问题描述】:

我是统一的 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 () {

       }
    }

【问题讨论】:

  • 如果你打印出 myAnimation.Play 的布尔结果,你得到的是真还是假?例如,如果找不到动画,Play 方法会返回 false 并且不会发出其他警报。

标签: c# unity3d


【解决方案1】:

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

代码如下:

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

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

【讨论】:

  • s0me1ne 我按照你说的做了,用动画师替换动画很有帮助。我的攻击动画正在运行。非常感谢。
  • @Joel 不要忘记,如果提供的答案是您问题的解决方案,请将其标记为解决方案。要将答案标记为已接受,请单击答案旁边的复选标记以将其从灰显切换为已填充。
  • Hristo 谢谢,对不起,我是堆栈溢出的新手。完成了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-26
  • 2016-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
相关资源
最近更新 更多