- Home /
Toggle Boolean by Input.getKey like if pressed one time true next time false and so on.
Hello All,
I want a Toggle Boolean by using Input.getKey(i don't want it by keydown) like if pressed one time true next time false and so on. I have tried the code but it gives weird result. here is the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public bool suri;
void Update()
{
if(Input.GetKey(KeyCode.A))
{
suri=toggle(suri);
}
}
public static bool toggle(bool suri)
{
return !suri;
}
}
GetKey
returns true every frame while the key is pressed.
Why don't you want to use GetKeyDown
or GetKeyUp
which return true only the frame when the key goes down / goes up respectively?
Thank you for your reply.
@Babish8 and @Hellium i am working on physical joystick which don't have button up function. It only have boolean, which onpress become true else false.
I want to have a toggle function on that button. so i cant use GetKeydown.
@AhmedElshazly i have tried this but it changes too fast and sometime don't work.
try adding a delay and make sure you're not using static variables
Answer by Hellium · Jun 07, 2020 at 03:16 PM
I haven't tried the following code
public class NewBehaviourScript : MonoBehaviour
{
public bool suri;
private bool isPressingKey;
private bool IsPressingKey
{
get { return isPressingKey; }
set
{
if(value == isPressingKey)
return;
isPressingKey = value;
// isSuri will be toggled when the key gets pressed down
// if you want the boolean to be toggles when the key is released
// change the condition to `if(!isPressingKey)`
if(isPressingKey)
suri = !suri;
}
}
void Update()
{
IsPressingKey = Input.GetKey(KeyCode.A);
}
}
However, it's tedious. Maybe the new Input System could solve your issue in a more elegant way.
This doesn't work on my setup it's still giving issues of randomly true or false. Like I have have pressed one time and it quickly goes through true true and some time skip false and vice versa.
I needed something like event or state. I think but no idea how to use it.
Well, this just manually emulates pretty much same behaviour as
if (Input.GetKeyDown(KeyCode.A))
suri = !suri;
The only difference would be when you use that inside FixedUpdate you get a different behaviour. So your code is essentially a GetKeyDown variant that works properly in FixedUpdate. Though in the original question he has the code in Update so this snippet works exactly like GetKeyDown as I shown above.
However since the actual goal is still unclear we essentially running circles here. The OP should clarify the question. Currently the question is not really clear and detailed.
Indeed @Bunny83, the code I wrote tends to copy the behaviour of Input.GetKeyDown
. This is what I understood from OP's question since he/she can't use Input.GetKeyDown
directly but still wants the same behaviour.
@suri44 I don't really get the problem you are facing. The script I've provided works as follow:
Default value of
suri
isfalse
When pressing down the
A
key,suri
turns totrue
and remains totrue
as long as you hold the keyWhen release the
A
key,suri
keeps itstrue
valueWhen pressing down the
A
key again,suri
changes tofalse
. Releasing the key does not change the value ofsuri
When pressing
A
a third time,suri
is changed totrue
Answer by AhmedElshazly · Jun 07, 2020 at 11:20 AM
Try Using this if(Input.GetKey(KeyCode.A)) { suri = !suri; }
Answer by Getsumi3 · Jun 07, 2020 at 03:31 PM
Here is my '5 cents'
BTW I'm not sure but I guess that if you use only GetKey (instead of GetKeyDown) it will keep calling the code inside untill you hold the key. so try it with GetKeyDown instead
public bool isActive = false;
private void Update()
{
if(input.GetKey(KeyCode.A))
{
isActive = !isActive;
//more code here
}
}
This will toggle the boolean every frame while the key is pressed.
well what else did you expected?
The OP wants with GetKey. I made an edit and mentioned about that.
Plus from persona experience I can't see anothe way to use Input.GetKey methods elsewhere except Update methods and Triggers
Your answer
Follow this Question
Related Questions
Render toggle help.. 1 Answer
How do I JavaScript to check if toggle is true? 0 Answers
Weapon Selector 1 Answer
Toggle camera sensitivity 1 Answer
How to make a Flashlight 0 Answers