zoukankan      html  css  js  c++  java
  • 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.
    即仅仅须要调用ItterateUpdateTrail方法。

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


    測试角色的模型包: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 

  • 相关阅读:
    Java 动态代理机制分析及扩展
    记:从百度空间搬家到博客园写博客要写的舒服
    字符串与byte[]之间的转换
    关于中文的几个编码GB2312、GBK、GB18030、GB13000
    深入biztalk中各种端口绑定方式(七) 直接绑定之Partner Orchestration
    X.509 数字证书结构和实例
    深入biztalk中各种端口绑定方式(六) 直接绑定之Self Correlating
    在代码中使用biztalk类库建立Message和Part
    Byte[]和BASE64之间的转换
    深入biztalk中各种端口绑定方式(五) 直接绑定之MessageBox
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/6764467.html
Copyright © 2011-2022 走看看