using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour {
public string Idle = "Idle";
public string Attack1 = "Attack1";
public string Attack2 = "Attack2";
public string Attack3 = "Attack3";
public int comboCount = 0;
private Animator ani;
void Start()
{
ani = this.GetComponent<Animator>();
}
void OnGUI()
{
if (GUILayout.Button("Attack")) Attack();
}
void Attack()
{
AnimatorStateInfo info = ani.GetCurrentAnimatorStateInfo(0);
if (info.IsName(Idle))
{
comboCount = 1;
ani.SetInteger("attack", comboCount);
}
if (info.IsName(Attack1) && comboCount == 1)
{
comboCount = 2;
}
if (info.IsName(Attack2) && comboCount == 2)
{
Debug.Log(info.normalizedTime);
comboCount = 3;
}
}
void NextAttack()
{
ani.SetInteger("attack", comboCount);
}
void StopAttack()
{
comboCount = 0;
ani.SetInteger("attack", comboCount);
}
}
1.需要注意的点,拥有事件触发方法的脚本必须在Animator组件的对象上。
2.注意触发时间,点击攻击的时候可获取到当前动画播放的normalizedTime,触发NextAttack事件的时间要晚于获取到的normalizedTime。
3.注意动画过度区!!! 不要执行stop方法