Hi,
I wanted to check if my player is holding a gun, so I can play different animations.
I set a bool as a parameter called HoldingGun (false). I wanted to make it true when the player hit the key
(“g”). It is like a mode change from Idle to shooter. and that works, except that the boolean is just true, as long as I am pressing “g”, what is not what I want.
Is there a way to set a boolean true, as long as I press another key? And without holding the “G” key?
Here is my code:
void Update()
{
animator.SetBool("HoldingGun", false);
if (Input.GetKey("g"))
{
HoldGun = true;
animator.SetBool("HoldingGun", true);
}
void Start()
{
animator.SetBool(“HoldingGun”, false);
}
void Update()
{
if (Input.GetKeyDown("g") && !HoldGun)
{
HoldGun = true;
animator.SetBool("HoldingGun", true);
}
}
@unity_3DmGkY57st7DXQ I don’t know if it will help but if you are wanting like a toggle type of effect when ever you press the key you set up you could just set the bool value to be what it isn’t.
Example:
if (Input.GetKeyDown(KeyCode.J))
{
Hand = !Hand;
}
This would make it so if the value of Hand is false, pressing the button would change it to true. Pressing it again would change it back to false.
I don’t know if this helps but just a suggestion for having the value invert on button press when dealing with bools.
void Update()
{
animator.SetBool(“HoldingGun”, false);
if (Input.GetKeyDown("g"))
{
HoldGun = true;
animator.SetBool("HoldingGun", true);
}