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

标签:
unity动作 |
分类: Unity |
前面简单的学习了用动画系统如何控制角色进行攻击,有了攻击就得有被击打的效果吧,然后就增加了闪避和击飞的动作状态,汗,下载的模型动作个数有限,只能用现用的动作进行尝试了。
效果如下:
http://s7/mw690/002wvREMgy6S3uMR5NI76&690
Animator设置如下:
http://s7/mw690/002wvREMgy6S3uizOiq46&690
增加闪避和倒地的条件:
http://s16/mw690/002wvREMgy6S3uKF6i30f&690
下面的代码只是做一个简单的尝试,所有都写在一个脚本了,主要是为了熟悉动画系统。
真正开发时,需定义好用到的动作进行配置,程序大多时候只是读配置进行不同的逻辑处理。
在这里被击打的对象就简单命名为foe_1了,然后m_isPlayer设置为false,表示不用进行输入控制。
-
using
UnityEngine; -
using
System.Collections; -
using
System.Collections.Generic; -
-
[RequireComponent(typeof(Animator))]
-
public
class PlayerCtrl : MonoBehaviour -
{
-
public bool true;m_isPlayer = -
public float m_moveSpeed 5;= -
public GameObject m_effect1; -
public GameObject m_effect2; -
-
private Transform m_trans; -
private Animator m_anim; -
// 当前状态和连击次数 -
private AnimatorStateInfo m_currentState; -
private int m_curComboCount 0;= -
// 方向和特效 -
private int m_curDir 1;= //left 1 right -1 -
private GameObject m_curEffect; -
-
// 动画 -
public struct AnimaName -
{ -
public const string "idle";m_idle = -
public const string "a";m_a = -
public const string "b";m_b = -
public const string "c";m_c = -
public const string "d";m_d = -
-
public const string "run";m_param_run = -
public const string "atk";m_param_atk = -
public const string "down";m_param_down = -
public const string "land";m_param_land = -
} -
-
public class AtkInfo -
{ -
public int id 0;= -
public float time; -
public bool false;isFirst = -
public bool false;isLast = -
public float atkTime 0;= -
public bool true;isSky = 是不是击飞 -
public bool false;isAtk = // 是否攻击过 -
-
public AtkInfo( intid, floattime, floatbool isFirst,bool isLast, atkTime, bool isSky) -
{ -
this.id = id; -
this.time = time; -
this.isFirst = isFirst; -
this.isLast = isLast; -
this.atkTime = atkTime; -
this.isSky = isSky; -
} -
} -
-
public Dictionary newm_atkInfoList = Dictionary() -
{ -
{AnimaName.m_a, new AtkInfo( 1,0.8f, true, false, 0.3f, false)}, -
{AnimaName.m_b, new AtkInfo( 2,0.8f, false,false, 0.4f,false)}, -
{AnimaName.m_c, new AtkInfo( 3,0.8f, false,false, 0.3f,false)}, -
{AnimaName.m_d, new AtkInfo( 4,0.8f, false,true, 0.3f,true)}, -
}; -
-
// 敌人列表 -
private List newm_foeList = List(); -
-
void Start () -
{ -
m_trans = transform; -
m_anim = GetComponent(); -
m_currentState = m_anim.GetCurrentAnimatorStateI nfo(0); -
// 模拟检测到一个敌人 -
m_foeList.Add(GameObject.Find("foe_1")); -
} -
-
void Update() -
{ -
m_currentState = this.m_anim.GetCurrentAnimatorStateI nfo( 0); -
if (m_isPlayer) -
{ -
_AttackAnima(); -
_AttackInput(); -
_Move(); -
} -
} -
-
private void _Move() -
{ -
if (m_currentState.IsName(AnimaName.m_idle) || m_currentState.IsName(AnimaName.m_param_run)) -
{ -
if (Input.GetKey(KeyCode.D)) -
{ -
m_anim.SetBool(AnimaName.m_param_run, true); -
m_trans.position += new Vector3( 0,0, 1f) * Time.deltaTime * m_moveSpeed; -
m_trans.eulerAngles = new Vector3( 0,0, 0); -
m_curDir = 1; -
} -
else if (Input.GetKey(KeyCode.A)) -
{ -
m_anim.SetBool(AnimaName.m_param_run, true); -
m_trans.position -= new Vector3( 0,0, 1f) * Time.deltaTime * m_moveSpeed; -
m_trans.eulerAngles = new Vector3( 0,- 180,0); -
m_curDir = -1; -
} -
else -
{ -
m_anim.SetBool(AnimaName.m_param_run, false); -
m_curComboCount = 0; -
} -
} -
} -
-
private void _AttackAnima() -
{ -
// 重置 -
if (!m_currentState.IsName(AnimaName.m_idle)) -
{ -
m_anim.SetBool(AnimaName.m_param_atk, false); -
} -
-
foreach (KeyValuePair item in m_atkInfoList) -
{ -
if (m_currentState.IsName(item.Key) && -
(m_currentState.normalizedTime > item.Value.time) && -
(m_curComboCount == (item.Value.id + 1)) && -
!item.Value.isLast) -
{ -
m_anim.SetBool(AnimaName.m_param_atk, true); -
} -
// 特效触发 -
if (m_currentState.IsName(item.Key) && -
Mathf.Abs(m_currentState.normalizedTime - item.Value.atkTime) < 0.02f && -
!item.Value.isAtk) -
{ -
item.Value.isAtk = true; -
// 攻击 -
Attack(item.Value); -
// 特效 -
if (!item.Value.isSky) -
m_curEffect = m_effect1; -
else -
m_curEffect = m_effect2; -
GameObject effect = GameObject.Instantiate(m_curEffect) as GameObject; -
effect.transform.position = m_trans.position + (new Vector3( 0,1.2f, m_curDir * 1f)); -
effect.transform.localScale = Vector3.one; -
GameObject.Destroy(effect, effect.GetComponent().startLifetime); -
} -
} -
} -
-
private void _AttackInput() -
{ -
if (Input.GetKeyUp(KeyCode.J)) -
{ -
// 连击开始 -
if (m_currentState.IsName(AnimaName.m_idle)) -
{ -
m_anim.SetBool(AnimaName.m_param_atk, true); -
this.m_curComboCount = 1; -
foreach (KeyValuePair item in m_atkInfoList) -
item.Value.isAtk = false; -
} -
else -
{ -
foreach (KeyValuePair item in m_atkInfoList) -
{ -
if (m_currentState.IsName(item.Key)) -
{ -
// 当前状态时,输入了就是需要触发下一个状态,所以id+1 -
this.m_curComboCount = 1;item.Value.id + -
} -
} -
} -
} -
} -
-
private void Attack(AtkInfo info) -
{ -
// 检测范围,先随便判断个距离 -
foreach (GameObject item in m_foeList) -
{ -
if (Vector3.Distance(m_trans.position, 2)item.transform.position) < -
{ -
PlayerCtrl player = item.GetComponent(); -
if (info.isSky) -
{ -
player.Down(); -
} -
else -
{ -
player.Land(); -
} -
} -
} -
} -
// 倒地 -
public void Down() -
{ -
m_anim.SetTrigger(AnimaName.m_param_down); -
} -
// 闪避 -
public void Land() -
{ -
m_anim.SetTrigger(AnimaName.m_param_land); -
} -
}