How do i reverse an if statement to make it like an else statement
If (Input.GetButton(“Fire2”))
{
Animator.SetBool(“recoil”, false)
}
If not
{
Animator.SetBool(“recoil”, true)
}
How do i reverse an if statement to make it like an else statement
If (Input.GetButton(“Fire2”))
{
Animator.SetBool(“recoil”, false)
}
If not
{
Animator.SetBool(“recoil”, true)
}
Do you mean you want to use an else statement after the if statement, like this?
if (Input.GetButton("Fire2")) {
Animator.SetBool("recoil", false);
} else {
Animator.SetBool("recoil", true);
}
To invert a boolean expression, you can use the !
operator (logical negation operator in C#). This takes the opposite of your boolean expression, turning true into false, and false into true. So if you just wanted to ask the opposite question, you can use:
if (!Input.GetButton("Fire2")) {
//...Happens whenever they're NOT pressing Fire2
}
But in your example, there’s a pattern, and you don’t have to ask an if statement at all. You’re setting the Animator’s “recoil” parameter to the opposite of Input.GetButton(“Fire2”). When Fire2 is pressed (true), recoil is set to false, and when Fire2 is not pressed (false), recoil is set to true. So you can do this in one line:
Animator.SetBool("recoil", !Input.GetButton("Fire2"));
So this sets “recoil” to the opposite of Input.GetButton(“Fire2”).
You could try If (Input.GetButton(“Fire2”)) { Animator.SetBool(“recoil”, false) } If (Input.GetButton(“Fire2”) == null) { Animator.SetBool(“recoil”, true) }