c# - 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 () {
}
}
解决方案
您可以尝试使用 Animator 并播放动画的名称(存在于 animator 中)。
代码想:
myAnimation = GetComponent<Animator>();
myAnimation.Play(attack.name);
参考:https ://docs.unity3d.com/ScriptReference/Animator.Play.html
推荐阅读
- javascript - React.js如何将索引作为道具从this.props数组传递给子组件
- java - 如何创建 CMIS 服务器
- mysql - 在 MySql 查询中构造 JSON 对象时取消引用值
- wpf - XAML 错误:不知道如何检测 ReactiveUI.RoutedViewHost 何时激活/停用
- python - 在模块odoo localhost上添加字段
- javascript - 从(上一个调用)最后一个结果值循环 json 调用
- ios - 如何在 GLKView 上绘制圆角透明矩形
- sql-server - 多参数值逻辑
- angularjs - 从控制器关闭 md 菜单
- c# - 如何获取当前聚焦窗口的选定文本?