加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Unity学习(二)Mecanim动画系统之受击

(2015-05-06 15:46:24)
标签:

unity动作

分类: Unity

前面简单的学习了用动画系统如何控制角色进行攻击,有了攻击就得有被击打的效果吧,然后就增加了闪避和击飞的动作状态,汗,下载的模型动作个数有限,只能用现用的动作进行尝试了。

效果如下:

http://s7/mw690/002wvREMgy6S3uMR5NI76&690


Animator设置如下:

http://s7/mw690/002wvREMgy6S3uizOiq46&690
增加闪避和倒地的条件:

http://s16/mw690/002wvREMgy6S3uKF6i30f&690

 

下面的代码只是做一个简单的尝试,所有都写在一个脚本了,主要是为了熟悉动画系统。

真正开发时,需定义好用到的动作进行配置,程序大多时候只是读配置进行不同的逻辑处理。

在这里被击打的对象就简单命名为foe_1了,然后m_isPlayer设置为false,表示不用进行输入控制。

  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4.   
  5. [RequireComponent(typeof(Animator))]  
  6. public class PlayerCtrl MonoBehaviour  
  7.  
  8.     public bool m_isPlayer true 
  9.     public float m_moveSpeed 5 
  10.     public GameObject m_effect1;  
  11.     public GameObject m_effect2;  
  12.   
  13.     private Transform m_trans;  
  14.     private Animator m_anim;      
  15.     // 当前状态和连击次数  
  16.     private AnimatorStateInfo m_currentState;  
  17.     private int m_curComboCount 0 
  18.     // 方向和特效  
  19.     private int m_curDir 1//left right -1  
  20.     private GameObject m_curEffect;  
  21.       
  22.     // 动画  
  23.     public struct AnimaName  
  24.      
  25.         public const string m_idle "idle" 
  26.         public const string m_a "a" 
  27.         public const string m_b "b" 
  28.         public const string m_c "c" 
  29.         public const string m_d "d" 
  30.   
  31.         public const string m_param_run "run" 
  32.         public const string m_param_atk "atk" 
  33.         public const string m_param_down "down" 
  34.         public const string m_param_land "land" 
  35.      
  36.   
  37.     public class AtkInfo  
  38.      
  39.         public int id 0 
  40.         public float time;  
  41.         public bool isFirst false 
  42.         public bool isLast false 
  43.         public float atkTime 0 
  44.         public bool isSky true  // 是不是击飞  
  45.         public bool isAtk false// 是否攻击过  
  46.   
  47.         public AtkInfo(int id, float time, bool isFirst,bool isLast, float atkTime, bool isSky)  
  48.          
  49.             this.id id;  
  50.             this.time time;  
  51.             this.isFirst isFirst;  
  52.             this.isLast isLast;  
  53.             this.atkTime atkTime;  
  54.             this.isSky isSky;  
  55.          
  56.      
  57.       
  58.     public Dictionary m_atkInfoList new Dictionary()   
  59.      
  60.         {AnimaName.m_a, new AtkInfo(10.8f, truefalse0.3f, false)},  
  61.         {AnimaName.m_b, new AtkInfo(20.8f, false,false0.4f,false)},  
  62.         {AnimaName.m_c, new AtkInfo(30.8f, false,false0.3f,false)},  
  63.         {AnimaName.m_d, new AtkInfo(40.8f, false,true0.3f,true)},  
  64.     };  
  65.   
  66.     // 敌人列表  
  67.     private List m_foeList new List();  
  68.   
  69.     void Start ()  
  70.      
  71.         m_trans transform;  
  72.         m_anim GetComponent();  
  73.         m_currentState m_anim.GetCurrentAnimatorStateInfo(0);  
  74.         // 模拟检测到一个敌人  
  75.         m_foeList.Add(GameObject.Find("foe_1"));  
  76.      
  77.   
  78.     void Update()  
  79.      
  80.         m_currentState this.m_anim.GetCurrentAnimatorStateInfo(0);  
  81.         if (m_isPlayer)  
  82.          
  83.             _AttackAnima();  
  84.             _AttackInput();  
  85.             _Move();  
  86.          
  87.      
  88.   
  89.     private void _Move()  
  90.      
  91.         if (m_currentState.IsName(AnimaName.m_idle) || m_currentState.IsName(AnimaName.m_param_run))  
  92.          
  93.             if (Input.GetKey(KeyCode.D))  
  94.              
  95.                 m_anim.SetBool(AnimaName.m_param_run, true);  
  96.                 m_trans.position += new Vector3(001f) Time.deltaTime m_moveSpeed;  
  97.                 m_trans.eulerAngles new Vector3(000);  
  98.                 m_curDir 1 
  99.              
  100.             else if (Input.GetKey(KeyCode.A))  
  101.              
  102.                 m_anim.SetBool(AnimaName.m_param_run, true);  
  103.                 m_trans.position -= new Vector3(001f) Time.deltaTime m_moveSpeed;  
  104.                 m_trans.eulerAngles new Vector3(0-1800);  
  105.                 m_curDir -1 
  106.              
  107.             else  
  108.              
  109.                 m_anim.SetBool(AnimaName.m_param_run, false);  
  110.                 m_curComboCount 0 
  111.              
  112.          
  113.      
  114.   
  115.     private void _AttackAnima()  
  116.      
  117.         // 重置  
  118.         if (!m_currentState.IsName(AnimaName.m_idle))  
  119.          
  120.             m_anim.SetBool(AnimaName.m_param_atk, false);  
  121.          
  122.   
  123.         foreach (KeyValuePair item in m_atkInfoList)  
  124.          
  125.             if (m_currentState.IsName(item.Key) &&   
  126.                 (m_currentState.normalizedTime item.Value.time) &&   
  127.                 (m_curComboCount == (item.Value.id 1)) &&  
  128.                 !item.Value.isLast)  
  129.              
  130.                 m_anim.SetBool(AnimaName.m_param_atk, true);  
  131.              
  132.             // 特效触发  
  133.             if (m_currentState.IsName(item.Key) &&   
  134.                 Mathf.Abs(m_currentState.normalizedTime item.Value.atkTime) 0.02f &&  
  135.                 !item.Value.isAtk)  
  136.              
  137.                 item.Value.isAtk true 
  138.                 // 攻击  
  139.                 Attack(item.Value);  
  140.                 // 特效  
  141.                 if (!item.Value.isSky)  
  142.                     m_curEffect m_effect1;  
  143.                 else  
  144.                     m_curEffect m_effect2;  
  145.                 GameObject effect GameObject.Instantiate(m_curEffect) as GameObject;  
  146.                 effect.transform.position m_trans.position (new Vector3(01.2f, m_curDir 1f));  
  147.                 effect.transform.localScale Vector3.one;  
  148.                 GameObject.Destroy(effect, effect.GetComponent().startLifetime);  
  149.              
  150.          
  151.      
  152.   
  153.     private void _AttackInput()  
  154.      
  155.         if (Input.GetKeyUp(KeyCode.J))  
  156.          
  157.             // 连击开始  
  158.             if (m_currentState.IsName(AnimaName.m_idle))  
  159.              
  160.                 m_anim.SetBool(AnimaName.m_param_atk, true);  
  161.                 this.m_curComboCount 1 
  162.                 foreach (KeyValuePair item in m_atkInfoList)  
  163.                     item.Value.isAtk false 
  164.              
  165.             else  
  166.              
  167.                 foreach (KeyValuePair item in m_atkInfoList)  
  168.                  
  169.                     if (m_currentState.IsName(item.Key))  
  170.                      
  171.                         // 当前状态时,输入了就是需要触发下一个状态,所以id+1  
  172.                         this.m_curComboCount item.Value.id 1 
  173.                      
  174.                   
  175.              
  176.          
  177.      
  178.   
  179.     private void Attack(AtkInfo info)  
  180.      
  181.         // 检测范围,先随便判断个距离  
  182.         foreach (GameObject item in m_foeList)  
  183.          
  184.             if (Vector3.Distance(m_trans.position, item.transform.position) 2 
  185.              
  186.                 PlayerCtrl player item.GetComponent();  
  187.                 if (info.isSky)  
  188.                  
  189.                     player.Down();  
  190.                  
  191.                 else  
  192.                  
  193.                     player.Land();  
  194.                  
  195.              
  196.          
  197.      
  198.     // 倒地  
  199.     public void Down()  
  200.      
  201.         m_anim.SetTrigger(AnimaName.m_param_down);  
  202.      
  203.     // 闪避  
  204.     public void Land()  
  205.      
  206.         m_anim.SetTrigger(AnimaName.m_param_land);  
  207.      
  208.  

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有