Animator animator;
MovePlayer movePlayer;
string currentState;
const string Idle_Down = "Idle_Down";
const string Idle_Up = "Idle_Up";
const string Run_Down = "Run_Down";
const string Run_Up = "Run_Up";
const string Run_Side = "Run_Side";
bool isMovingHorizontal;
bool isMovingVerticalUp;
bool isMovingVerticalDown;
private void Start() {
movePlayer = GetComponent<MovePlayer>();
animator = GetComponent<Animator>();
IsMovingBoolSet();
}
void IsMovingBoolSet() {
SetMovingHorizontal();
SetMovingVertical();
}
void SetMovingHorizontal() {
if (movePlayer.moveVector.x != 0f) {
isMovingHorizontal = true;
} else {
isMovingHorizontal = false;
}
}
void SetMovingVertical() {
if (movePlayer.moveVector.y >= 0.5f) {
isMovingVerticalUp = true;
} else {
isMovingVerticalUp = false;
}
if (movePlayer.moveVector.y <= -0.5f) {
isMovingVerticalDown = true;
}
else {
isMovingVerticalDown = false;
}
}
private void Update() {
IsMovingBoolSet();
MoveAnimations();
}
void MoveAnimations() {
MoveAnimationsHorizontal();
MoveAnimationsVertical();
}
void MoveAnimationsHorizontal() {
if (isMovingHorizontal) {
ChangeAnimationState(Run_Side);
}
}
void MoveAnimationsVertical() {
if (isMovingVerticalUp) {
ChangeAnimationState(Run_Up);
}
if (isMovingVerticalDown) {
ChangeAnimationState(Run_Down);
}
}
void ChangeAnimationState(string newState) {
// Stop the same animation from interrupting itself
if (currentState == newState) return;
// Play the Animation
animator.Play(newState);
// Reassign the current state
currentState = newState;
}