How to turn on and off a bool with a button press
Dirt simple problem: I need a bool to switch back and forth when a single button is pressed. In my case the trigger on an Xbox controller.
void Start () {
crawlCheck = false;
}
void Update () {
isCrawling = Input.GetAxis("Crawl Toggle");
if (isCrawling == 1 && crawlCheck == false)
{
crawlCheck = true;
}
else if (isCrawling == 1 && crawlCheck == true)
{
crawlCheck = false;
}
}
I just need crawlCheck to change to false or true when the trigger is pressed and stay there until it's pressed again.
What I get is it switching from true to false and back as long as the trigger is held.
Answer by Dave-Carlile · Nov 23, 2015 at 01:20 PM
You can toggle the value of a boolean by using the "not" operator:
not true = false
not false = true
So you can write something like this to toggle it back and forth;
if (your button is down)
{
crawlCheck = !crawlCheck;
}
Thanks, Dave! $$anonymous$$uch simpler. But this doesn't go in the update function, does it? If I put it there then crawlCheck reverses every frame that the button is held, right? I need it to just reverse once for every button press.
You'd generally use this with Input.GetButtonDown which handles the "only return true once until the button is released and pressed again" functionality, so if you you can set this up as a button in the Input $$anonymous$$anager that would be ideal. If you must use GetAxis then you'd need to preserve the last state and only execute this on a state change.
float lastCrawling;
void Update()
{
// get the current state
float crawling = Input.GetAxis("Crawl Toggle");
// is it different from the last state?
if (crawling != lastCrawling)
{
// preserve the new "last state"
lastCrawling = crawling;
// if the new state is "down" then do our down functionality
if (crawling != 0)
{
crawlCheck = !crawlCheck;
}
}
}
Thank you very much Dave. I've seen you before on these forums you are very insightful, and you help me out a lot even thought you might not know it.
Answer by Dudechester · Jul 15, 2018 at 05:38 PM
I'm a little confused by this thread guys, referring to the last message Dave, you said that you generally use the Input.GetButtonDown, but then you never used it in your example?
It may have made more sense if you did use GetButtonDown, because what does the GetAxis have to do with pushing a button? (I'm noob to code trying to learn) when you followed GetAxis, you did so with "Crawl Toggle" but this is a variable which you haven't declared previously? This code really confused me :)
What is the best way to do this please?
thankyou
Im not sure what he meant either by using the axis, but if you were to use a GetButton command, you could do something like this
public bool crawlCheck;
void Start()
{
crawlCheck = false;
}
void Update()
{
if (Input.GetButton("Crawl"))
{
crawlCheck = true;
}
else
{
crawlCheck = false;
}
}
Answer by K0ST4S · Apr 21, 2019 at 08:02 AM
Use properties instead of fields: public bool check; -> public bool check {get;set;} In the button component: Select "check" property in your script