Unity给人物模型添加动画
发表于2019-01-08
给Unity人物模型添加动画,首先你得有个模型,在预制体中,有导入设置,重点是Rig设置。第一个选项是动画的类型,因为导入的是人物模型,所以应该选择人形;第二个是人物骨骼,这里已经设好了;第三个选项是对物体进行优化,建议勾选吧







将预制体拖拽到场景中后,系统会自动生成一个Animator组件,

我们需要创建一个Controller来控制动画,在工程里创建一个动画控制器Animator Controller,将这个控制器拖拽到Animator组件上。


打开Animator窗口,找到资源中的动画剪辑,这里是Idle,拖到窗口中,就生成一个橘黄色的Idle的框框,播放时人物就会使Idle状态。下面再给人物加上Attack状态,这里注意的是,单纯把Attack的动画剪辑拖拽到窗口中是灰色的,要设定一个过渡才能让Attack的动画播放出来,右键点击Idle,在选项中选择Make Transition会出现一个箭头,选中Attack状态即可。我们还想让Attack过渡到Idle状态,所以再连接一根到Idle的箭头


这里右方设置面板有一个Has ExitTime选项,勾选的意思是当前一个动画播放完,再播放下一个动画,因为我们希望攻击时瞬发的,所以不勾选。
在左边栏中有参数选项,我们可以创建一个Trigger来触发我们想要的动画效果,给动画过渡的Conditions中加上。Attack。现在播放时,我们点击Attack右边的小圆圈,人物就会攻击一次。当然人物可能会停在攻击的动作,因为我们Attack的动画设置不是循环,而Idle的动画是循环,所以会一直处于Idle状态,下面给人物挂载上脚本来控制人物的动画播放。
using UnityEngine; using System.Collections; public class Controller : MonoBehaviour { public Animator _animation;//创建一个Animator类,用来接受人物的组件 void Awake() { _animation = GetComponent<Animator>();//获取人物身上的动画组件 } void Update () { if (Input.GetMouseButtonDown(0))//当按下鼠标左键 { _animation.SetTrigger("Attack");//触发Attack动画 } } }
现在播放时,点击左键就可以让人物播放攻击动画了。