Unity 武器拖尾效果


Pocket RPG Weapon Trails 武器拖尾效果
截图:



因为这个插件提供的 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.
即仅仅须要调用 Itterate UpdateTrail方法。

以下使用另外的角色模型进行測试拖尾效果。


測试角色的模型包: https://www.assetstore.unity3d.com/en/#!/content/15103
CSDN资源地址: http://download.csdn.net/detail/akof1314/7610385
首先。在 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( 005050),  "攻击"))
        {
            animator.SetTrigger( "Attack");
        }
    }
}
执行,能够看到默认角色是休闲状态,点击button是攻击状态。例如以下图所看到的:

查看模型,能够看到武器是绑在右手上的,例如以下图所看到的:

武器(Object003)加入一个子对象,命名为 Trail,为其加入 WeaponTrail.cs 脚本、 Mesh Renderer组件。材质为Pocket RPG Trails提供的材质,设置好例如以下图所看到的:

改动 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,  00.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( 005050),  "攻击"))
        {
            animator.SetTrigger( "Attack");
        }
    }
}
Trail对象赋给 My Trail属性,例如以下图所看到的:

如今执行,能够看到休闲状态时。武器拖尾的若隐若现,例如以下图所看到的:

攻击时的效果:

要调整好 Trail对象的位置、旋转等,尽量贴合武器,设置拖尾的高度,尽量与武器同长度。才干产生较好的效果。

当攻击结束,武器往回收的时候。也会有拖尾,例如以下图所看到的:

假设要去掉这个时候的拖尾,能够採用更精确的控制拖尾的出现。选中攻击动作。切换到"Animations"。播放动作,在攻击開始时刻,加入一个事件,例如以下图所看到的:

在攻击完成,也加入一个事件。例如以下图所看到的:

点击" Apply"进行应用。改动 TestMyTrail.cs 代码为例如以下:
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();
    }
如今执行,就会发现休闲状态时候,不会有拖尾效果。当进行攻击时。拖尾仅仅在对应的时间点进行出现。例如以下图所看到的:

武器回收的时候,也不会有拖尾了。例如以下图所看到的:


參考资料:
1.Unity3D 武器拖尾效果(刀光) 使用PocketRPG Trails  http://blog.csdn.net/xv_ly15/article/details/8509781
2.Unity3D研究院之挥动武器产生的剑痕特效(四十七)  http://www.xuanyusong.com/archives/2110  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM