- Home /
Vertical input won't work without Horizontal
Hey everyone, I have a problem when I try to move my character in the vertical axis it won't work unless I give a Horizontal input, Does someone have an idea why is it like that? Here is my code in case you need that. Thanks for helping :) {
public float PlayerMovementSpeedX = 1f;
public float PlayerMovementSpeedY = 1f;
// Start is called before the first frame update
void Start()
{
p_t = GetComponent<Transform>();
p_rb = GetComponent<Rigidbody>();
p_a = GetComponent<Animator>();
}
private void FixedUpdate()
{
float PlayerMovementY = Input.GetAxis("Vertical");
float PlayerMovementX = Input.GetAxis("Horizontal");
Vector2 Movement = new Vector2(PlayerMovementX, PlayerMovementY);
if (Movement.x == 0)
{
p_a.Play("Idle");
}
else if(Movement.y !=0 | Movement.x != 0)
{
p_rb.velocity = new Vector2(PlayerMovementX * PlayerMovementSpeedX, PlayerMovementY * PlayerMovementSpeedY);
p_a.Play("Walk");
}
//Event to call Flip functione
if(FacingRight == true & Movement.x < 0 | FacingRight == false & Movement.x > 0)
{
flip();
FacingRight = !FacingRight;
}
}
void flip()
{
Vector2 Scale = new Vector2 (transform.localScale.x * -1, transform.localScale.y);
transform.localScale = Scale;
}
}
Thanks for helping ;)
Answer by daniel14657 · Sep 01, 2020 at 10:50 AM
Hey, Rookie mistake, After my first "if" I did "else if" which mean if only X is not equal to zero, then continue to the next "else if", Just had to change the "else if" to "if". Thanks for helping
I already see another issue, you retrieve input in FixedUpdate instead of Update, this makes it so that you won't retrieve input on each frame, but only when fixed update runs. This makes movement unresponsive or jerky.
private float Player$$anonymous$$ovementX, Player$$anonymous$$ovementY;
private void Update()
{
Player$$anonymous$$ovementY = Input.GetAxis("Vertical");
Player$$anonymous$$ovementX = Input.GetAxis("Horizontal");
}
private void FixedUpdate()
{
// Do logic to Player$$anonymous$$ovementX and Y here
}
This is better.
Your answer
Follow this Question
Related Questions
Touching Falling Objects 1 Answer
AddTorque doesn't rotate? 3 Answers
Making a ship move 1 Answer
How do i get the cube to turn when up and right or left arrow is pressed? 1 Answer
Jump Input Sensitivity 0 Answers