I've been trying to make the player able to crouch by pressing a button once to crouch, and again to stand.
I thought this would be pretty simple and I could just use a bool, but it seems like when the button to crouch is pressed, it's giving input too fast and immediately uncrouching. I'm not sure if I'm getting my bool from the wrong place or what.
This is what I have to toggle crouch.
//CROUCH
if (crouch.IsPressed() && !isCrouching)
{
Crouch();
}
if(crouch.IsPressed() && isCrouching )
{
Stand();
}
These are the methods being called to crouch/stand. I'm thinking my problem has to do with setting my bool in these, but I can't figure it out.
void Crouch()
{
isCrouching = true;
transform.localScale = new Vector3(transform.localScale.x, 0.75f, transform.localScale.z);
transform.position = new Vector3(transform.position.x, transform.position.y - 0.15f, transform.position.z);
}
void Stand()
{
transform.localScale = new Vector3(transform.localScale.x, 1f, transform.localScale.z);
isCrouching = false;
}
Also to be clear this is inside FixedUpdated. Not sure if that has an effect either, but when I tried normal update it was the same.
CodePudding user response:
Two things: Use WasPressedThisFrame instead to get a bool which is only true one frame. And you should change your code to an if else statement so they can not be both true after one another in the same frame.