I am a beginner in both programming and game development in Unity so please have patience.
I am trying to add a force to the player upon the press of a button. The main concept behind this is that if the player is facing either left or right then the player should be launched in that direction.
I have been trying to make the code detect if the player is facing left or right depending on which button I pressed last ("A" or "D") using the "InputGetKeyDown(KeyCode.A/D) command.
I made two private bools (IsFacingRight and IsFacingLeft). IsFacingRight starts true by default and Left starts false but if the player switches direction then the bools would change accordingly. The launch would have happened when I pressed left-shift but that doesn't register either.
I tried using "Debug.Log" to see if the code detects the inputs and as I expected, it didn't. When I pressed "A" or "D" or "LeftShift" nothing would appear in the Debug console. So now I have confirmation that unity doesn't detect the inputs.
The code is a lot bigger but everything from it works expect the part that I will list below. I should also mention that the code doesn't get any errors and the simulation runs perfectly except for everything mentioned above. Here is the code:
private bool IsFacingR = true;
private bool IsFacingL = false;
public Rigidbody2D RB;
private float thrust = 20f;
void Sliding()
{
if (Input.GetKeyDown(KeyCode.D))
{
Debug.Log("Im R");
IsFacingR = true;
IsFacingL = false;
}
if (Input.GetKeyDown(KeyCode.A))
{
Debug.Log("Im L");
IsFacingR = false;
IsFacingL = true;
}
if (Input.GetButtonDown("Crouch")) ;
{
Slide();
}
}
void Slide()
{
if (IsFacingR = true) ;
{
RB.AddForce(Vector2.right * thrust);
}
if (IsFacingL = true) ;
{
RB.AddForce(-Vector2.right * thrust);
}
}