Part2:animator转换(P29)
1:效果图
(1)给player创建子物体,用于存放动画和图形渲染
(2)给Player->Animator添加动画playeridle和playermove
(3)编辑动画器,添加新bool参数ide和move,用于作为动画切换
(4)设置跳转条件,将playeridle设置为默认动画,当触发Move则转化为playermove动画!
2:代码改变
//Player.cs添加代码
public Animator anim { get; private set; }//这样才能配合着拿到自己身上的animator的控制权
private void Awake()
{
stateMachine = new PlayerStateMachine();
//通过构造函数,在构造时传递信息
idleState = new PlayerIdleState(this, stateMachine, "Idle");
moveState = new PlayerMoveState(this, stateMachine, "Move");
//this 就是 Player这个类本身
}
private void Start()
{
anim = GetComponentInChildren<Animator>();//拿到自己身上的animator的控制权
stateMachine.Initialize(idleState);
}
private void Update()//在mano中update会自动刷新但其他没有mano的不会故,需要在这个updata中调用其他脚本中的函数stateMachine.currentState.update以实现 //stateMachine中的update
{
stateMachine.currentState.Update();//反复调用CurrentState的Update函数
}
//PlayerState.cs添加代码
public virtual void Enter()
{
player.anim.SetBool(animBoolName, true);//通过animator自带的函数,将animator里的Bool值改变为想要的值
}
public virtual void Exit()
{
player.anim.SetBool(animBoolName, false);//通过animator自带的函数,将animator里的Bool值改变为想要的值
}
//PlayerIdleState.cs添加代码(PlayerIdleState.cs同理)
public class PlayerIdleState : PlayerState//继承函数,获得PlayerState里的所有函数和参数
{
public PlayerIdleState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName)
{
}//构造函数,用于传递信息。
public override void Update()
{
base.Update();
if(Input.GetKeyDown(KeyCode.N))
{
stateMachine.ChangeState(player.moveState);
}
}}