tip:transition 勾选Has Exit Time B动画播放完毕后就可以自己返回A不用代码控制。因为想做一个小人静止时 隔两秒会摆动小手的特效。
附上代码参考:
using UnityEngine;
using System.Collections; public class playeMove : MonoBehaviour
{ public Animator PlayerAnimator;
public const int HERO_UP = ;
public const int HERO_RIGHT = ;
public const int HERO_DOWN = ;
public const int HERO_LEFT = ;
float FreakTime=;
//人物当前行走的方向状态
public int state = ;
//人物移动速度
public int moveSpeed =; //初始化人物位置
public void Awake()
{
state = HERO_UP;
}
// Use this for initialization
void Start()
{ } // Update is called once per frame
void Update()
{ //获取控制的方向, 上下左右,
float KeyVertical = Input.GetAxis("Vertical");
float KeyHorizontal = Input.GetAxis("Horizontal");
//Debug.Log("keyVertical" + KeyVertical);
//Debug.Log("keyHorizontal" + KeyHorizontal);
if (KeyVertical <)
{
setHeroState(HERO_DOWN);
}
else if (KeyVertical >)
{
setHeroState(HERO_UP);
} if (KeyHorizontal >)
{
setHeroState(HERO_RIGHT);
}
else if (KeyHorizontal <)
{
setHeroState(HERO_LEFT);
} //得到正在播放的动画状态
AnimatorStateInfo info = PlayerAnimator.GetCurrentAnimatorStateInfo(); //如果没有按下方向键且状态不为walk时播放走路动画
if (KeyVertical != || KeyHorizontal != && !info.IsName("Walk"))
{
PlayerAnimator.Play("Walk");
}
//否则如果按下方向键且状态为walk时播放静止动画
else if((KeyVertical == && KeyHorizontal == && info.IsName("Walk") ))
{
PlayerAnimator.Play("Idle");
} //这里设定是玩家静止时隔2s会摆动一次
if (KeyVertical == && KeyHorizontal == )
{
//当玩家静止时,FreakTime才会计时
if (info.IsName("Idle"))
{
FreakTime -= Time.deltaTime;
if (FreakTime <= )
{
Debug.Log(FreakTime);
FreakTime = ;
//FreakingOut设置为播放后自动退出到idle
PlayerAnimator.Play("FreakingOut ");
}
}
} } void setHeroState(int newState)
{
//根据当前人物方向与上一次备份的方向计算出模型旋转的角度
int rotateValue = (newState - state) * ;
Vector3 transformValue = new Vector3(); //播放行走动画 //模型移动的位置数值
switch (newState)
{
case HERO_UP:
transformValue = Vector3.forward * Time.deltaTime;
break;
case HERO_DOWN:
transformValue = (-Vector3.forward) * Time.deltaTime;
break;
case HERO_LEFT:
transformValue = Vector3.left * Time.deltaTime;
break;
case HERO_RIGHT:
transformValue = (-Vector3.left) * Time.deltaTime;
break;
} transform.Rotate(Vector3.up, rotateValue);
//移动人物
transform.Translate(transformValue * moveSpeed, Space.World);
state = newState;
} }