怎样通过unity wasd控制人物移动,不需要转身,今天我们就来看过程。通过wasd获得行走的方向,然后对应控制角色的位移就可以了
首先定义 一个 枚举变量,为角色可能的状态。
public enum ActionState
{
Attack_1,
Attack_2,
Attack_3_1,
Attack_3_2,
Attack_3_3,
Attack_4,
Fire,
Jump,
Run,
Idle
}
{
float delayTime = -0.1f;
switch (actionState) {
case ActionState.Attack_3_1:
delayTime = -0.1f;
playerAnimation.CrossFade("Attack3-1", 0.15f);
currentAnimationClip = playerAnimation["Attack3-1"].clip;
break;
case ActionState.Attack_3_2:
delayTime = -0.1f;
playerAnimation.CrossFade("Attack3-2", 0.15f);
currentAnimationClip = playerAnimation["Attack3-2"].clip;
break;
case ActionState.Attack_3_3:
delayTime = -0.1f;
playerAnimation.CrossFade("Attack3-3", 0.15f);
currentAnimationClip = playerAnimation["Attack3-3"].clip;
break;
case ActionState.Attack_4:
delayTime = -0.1f;
playerAnimation.CrossFade("Attack4", 0.15f);
currentAnimationClip = playerAnimation["Attack4"].clip;
break;
case ActionState.Idle:
playerAnimation.CrossFade("Idle", 0.35f);
currentAnimationClip = playerAnimation["Idle"].clip;
break;
case ActionState.Jump:
playerAnimation.CrossFade("Jump", 0.15f);
currentAnimationClip = playerAnimation["Jump"].clip;
break;
case ActionState.Run:
playerAnimation.CrossFade("Run", 0.15f);
currentAnimationClip = playerAnimation["Run"].clip;
break;
case ActionState.Fire:
playerAnimation.CrossFade("Fire", 0.15f);
currentAnimationClip = playerAnimation["Fire"].clip;
break;
default: break;
}
//Switch to default if an animation is almost over
if (playerAnimation[currentAnimationClip.name].time > (playerAnimation[currentAnimationClip.name].length + delayTime)){
actionState = ActionState.Idle;
currentAnimationClip = playerAnimation["Idle"].clip;
}
void Update () {
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
MovementManagement (h, v);
Animate ();
{
if(horizontal != 0f || vertical != 0f)
{
Rotating(horizontal, vertical);
actionState = ActionState.Run;
moveDirection = new Vector3(horizontal, 0.0f, vertical);
rigidbody.MovePosition(rigidbody.position + speed * moveDirection * Time.deltaTime);
}
}
首先定义 一个 枚举变量,为角色可能的状态。
public enum ActionState
{
Attack_1,
Attack_2,
Attack_3_1,
Attack_3_2,
Attack_3_3,
Attack_4,
Fire,
Jump,
Run,
Idle
}
private ActionState actionState = ActionState.Idle;
Animate函数来根据角色的状态播放对应的动画
void Animate(){
float delayTime = -0.1f;
switch (actionState) {
case ActionState.Attack_3_1:
delayTime = -0.1f;
playerAnimation.CrossFade("Attack3-1", 0.15f);
currentAnimationClip = playerAnimation["Attack3-1"].clip;
break;
case ActionState.Attack_3_2:
delayTime = -0.1f;
playerAnimation.CrossFade("Attack3-2", 0.15f);
currentAnimationClip = playerAnimation["Attack3-2"].clip;
break;
case ActionState.Attack_3_3:
delayTime = -0.1f;
playerAnimation.CrossFade("Attack3-3", 0.15f);
currentAnimationClip = playerAnimation["Attack3-3"].clip;
break;
case ActionState.Attack_4:
delayTime = -0.1f;
playerAnimation.CrossFade("Attack4", 0.15f);
currentAnimationClip = playerAnimation["Attack4"].clip;
break;
case ActionState.Idle:
playerAnimation.CrossFade("Idle", 0.35f);
currentAnimationClip = playerAnimation["Idle"].clip;
break;
case ActionState.Jump:
playerAnimation.CrossFade("Jump", 0.15f);
currentAnimationClip = playerAnimation["Jump"].clip;
break;
case ActionState.Run:
playerAnimation.CrossFade("Run", 0.15f);
currentAnimationClip = playerAnimation["Run"].clip;
break;
case ActionState.Fire:
playerAnimation.CrossFade("Fire", 0.15f);
currentAnimationClip = playerAnimation["Fire"].clip;
break;
default: break;
}
//Switch to default if an animation is almost over
if (playerAnimation[currentAnimationClip.name].time > (playerAnimation[currentAnimationClip.name].length + delayTime)){
actionState = ActionState.Idle;
currentAnimationClip = playerAnimation["Idle"].clip;
}
}
在update函数中添加对应的逻辑函数:
// Update is called once per framevoid Update () {
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
MovementManagement (h, v);
Animate ();
}
MovementManagement函数就是根据输入处理角色行走的
void MovementManagement(float horizontal, float vertical){
if(horizontal != 0f || vertical != 0f)
{
Rotating(horizontal, vertical);
actionState = ActionState.Run;
moveDirection = new Vector3(horizontal, 0.0f, vertical);
rigidbody.MovePosition(rigidbody.position + speed * moveDirection * Time.deltaTime);
}
}