- Home /
How can I deal with multiple if-statements?
Hello, I'm a new inexperience programmer and I'm trying to make a 2D game.
I'm currently having problems with a player controller script, for 2D player movement. In the script, what I want to do is changing the player's direction by pressing "Horizontal" or "Vertical" Inputs while pressing the other inputs.
Ex: When I press "Horizontal" input then move horizontally, when I press "Vertical" input while pressing the "Horizontal" input then change direction and move vertically.
But somehow my script isn't working when I press the "Vertical" input while I'm pressing the "Horizontal" input. (Oddly, when I press the "Horizontal" input while I'm pressing the "Vertical" input, it works fine)
So here is my script:
> public class PlayerController : MonoBehaviour {
public Rigidbody2D theRB;
public float theSpeed;
public static PlayerController instance;
void FixedUpdate()
{
theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")) * theSpeed;
if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1)
{
if (Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
{
theRB.velocity = new Vector2(0, Input.GetAxisRaw("Vertical")) * theSpeed;
}
else if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1)
{
theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), 0) * theSpeed;
}
}
if (Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
{
if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1)
{
theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), 0) * theSpeed;
}
else if (Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
{
theRB.velocity = new Vector2(0, Input.GetAxisRaw("Vertical")) * theSpeed;
}
}
}
Thank you for your help!
Answer by AWDXZA · Jun 26, 2019 at 05:04 AM
Okay, I solved the problem!
To answer my own question, the problem was that the if statements with Input.GetAxisRaw didn't recognized the order of the vertical and horizontal inputs. So what I mean is that to my code,
if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1)
{
if (Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
and
if (Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
{
if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1)
basically are same, since the code don't recognize the order of the two inputs.
So in order to set the order of these two inputs, I used Time.fixedDeltaTime and made the differences of the two inputs. So the final code looks like this:
public Rigidbody2D theRB;
public float theSpeed;
private float horizontalTime=0;
private float verticalTime=0;
void Update()
{
TimerForMove();
theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")) * theSpeed;
if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1)
{
if (Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
{
if (horizontalTime > verticalTime)
{
theRB.velocity = new Vector2(0, Input.GetAxisRaw("Vertical")) * theSpeed;
}
else theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), 0) * theSpeed;
}
//else if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1)
{
//theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), 0) * theSpeed;
}
}
if (Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
{
if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1)
{
if (verticalTime > horizontalTime)
{
theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), 0) * theSpeed;
}
else theRB.velocity = new Vector2(0, Input.GetAxisRaw("Vertical")) * theSpeed;
}
//else if (Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
{
//theRB.velocity = new Vector2(0, Input.GetAxisRaw("Vertical")) * theSpeed;
}
}
}
void TimerForMove()
{
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
{
horizontalTime += Time.fixedDeltaTime;
}
else
horizontalTime = 0;
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S))
{
verticalTime += Time.fixedDeltaTime;
}
else
verticalTime = 0;
}
When I tested, the player moved as what I expected and can be used as simple 2D player movement script!
Thank you for your help and if you have better code or suggestion for fixing some of this code, please let me know ;)
Answer by ShadyProductions · Jun 11, 2019 at 08:32 AM
You don't need all those if statements you already are setting the velocity using this one line:
theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")) * theSpeed;
But you may want to get the input from the update method instead and pass it to the fixed update method.
Well, what I want to do is changing the player's direction by pressing the other input. Also, I forgot mention but my another purpose for all those if statements are for limiting the player's movement by 4 directions. So I think I'll need all those if statements. But yes, I think I may need to get the input from the update method ins$$anonymous$$d of the fixed update method. Thank you for the help!
Your answer
Follow this Question
Related Questions
How can I limit my 2D player movement along the Y-axis? 1 Answer
My animation is overriding my character movement? 2 Answers
Snake Movement Problem 0 Answers
Move 2D Sprite Horizontally With Mouse 1 Answer
Player Dash doesn't work properly 0 Answers