Hello, I’m new to coding and this is my first topic. I’m Currently working on character movement/animations (unity 2D). I’m having a issue with the last directions for idle. I created two booleans with ‘directionRight’ ‘directionDown’ and used it for the ‘xAxis’ and ‘yAxis’ but the ‘yAxis’ doesn’t seems to work properly. The ‘xAxis’ works but the ‘yAxis’ only has his walk animations and not his idle animation. My theorie is that I messed up with using so much ‘else if’ statements or I’m not able to use two booleans in one if statement. I hope this is clear if it’s not clear pls correct me. Here is my code (In 'fixedupdate’is where you can find my issue I think):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Animator animator;
private string currentState;
private Rigidbody2D rb;
float walkSpeed = 4f;
float xAxis;
float yAxis;
public bool directionRight = true;
public bool directionDown = false;
const string Walk_U = "Walk_Up";
const string Walk_D = "Walk_Down";
const string Walk_L = "Walk_Left";
const string Walk_R = "Walk_Right";
const string Idle_U = "Idle_Up";
const string Idle_D = "Idle_Down";
const string Idle_L = "Idle_Left";
const string Idle_R = "Idle_Right";
void Start()
{
animator = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
xAxis = Input.GetAxisRaw("Horizontal");
yAxis = Input.GetAxisRaw("Vertical");
}
void FixedUpdate()
{
rb.velocity = new Vector2(xAxis, yAxis).normalized * walkSpeed;
if(xAxis > 0f)
{
ChangeAnimationState(Walk_R);
directionRight = true;
}else if(xAxis < 0f)
{
ChangeAnimationState(Walk_L);
directionRight = false;
}else if(yAxis > 0f)
{
ChangeAnimationState(Walk_U);
directionDown = false;
}else if(yAxis < 0f)
{
ChangeAnimationState(Walk_D);
directionDown = true;
}else if(directionRight)
{
ChangeAnimationState(Idle_R);
}else if(!directionRight)
{
ChangeAnimationState(Idle_L);
}else if(directionDown)
{
ChangeAnimationState(Idle_D);
}else if(!directionDown)
{
ChangeAnimationState(Idle_U);
}
}
void ChangeAnimationState(string newAnim)
{
if(currentState == newAnim) return;
animator.Play(newAnim);
currentState = newAnim;
}
}
[1]:
via+GIPHY