由于这个插件提供的 AnimationController.cs仅对 Animation动画进行支持,对 Animator动画支持的话需要自己实现。文档上说明实现的方式:
- The WeaponTrail can be built by calling Itterate(float itterateTime) and UpdateTrail(float currentTime, float deltaTime). These functions are called by AnimationController, however if you don't want to use AnimationController you can call these yourself.
首先,在 Animator窗口,创建休闲 idle状态和攻击 attack状态,设置它们相应的Motion,设置从idle到attack的动画参数为 Attack,类型为 Trigger,如下图所示:
Speed属性可以控制当前状态动作的速度。接着,创建个脚本 TestMyTrail.cs附加到角色上,脚本代码如下:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
using UnityEngine;
using System.Collections; public class TestMyTrail : MonoBehaviour { private Animator animator; void Start () { animator = GetComponent<Animator>(); } void OnGUI() { if (GUI.Button( new Rect( 0, 0, 50, 50), "攻击")) { animator.SetTrigger( "Attack"); } } } |
查看模型,可以看到武器是绑在右手上的,如下图所示:
修改 TestMyTrail.cs代码为如下:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
using UnityEngine;
using System.Collections; public class TestMyTrail : MonoBehaviour { public WeaponTrail myTrail; private Animator animator; private float t = 0.033f; private float tempT = 0; private float animationIncrement = 0.003f; void Start () { animator = GetComponent<Animator>(); } void LateUpdate() { t = Mathf.Clamp(Time.deltaTime, 0, 0.066f); if (t > 0) { while (tempT < t) { tempT += animationIncrement; if (myTrail.time > 0) { myTrail.Itterate(Time.time - t + tempT); } else { myTrail.ClearTrail(); } } tempT -= t; if (myTrail.time > 0) { myTrail.UpdateTrail(Time.time, t); } } } void OnGUI() { if (GUI.Button( new Rect( 0, 0, 50, 50), "攻击")) { animator.SetTrigger( "Attack"); } } } |
现在运行,可以看到休闲状态时,武器拖尾的若隐若现,如下图所示:
攻击时的效果:
要调整好 Trail对象的位置、旋转等,尽量贴合武器,设置拖尾的高度,尽量与武器同长度,才能产生较好的效果。当攻击结束,武器往回收的时候,也会有拖尾,如下图所示:
如果要去掉这个时候的拖尾,可以采用更精确的控制拖尾的出现。选中攻击动作,切换到" Animations",播放动作,在攻击开始时刻,添加一个事件,如下图所示:
在攻击完毕,也添加一个事件,如下图所示:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
void Start ()
{ animator = GetComponent<Animator>(); // 默认没有拖尾效果 myTrail.SetTime( 0.0f, 0.0f, 1.0f); } public void heroAttack() { //设置拖尾时长 myTrail.SetTime( 2.0f, 0.0f, 1.0f); //开始进行拖尾 myTrail.StartTrail( 0.5f, 0.4f); } public void heroIdle() { //清除拖尾 myTrail.ClearTrail(); } |
武器回收的时候,也不会有拖尾了,如下图所示: