- Home /
How does "canceled" and "performed" work with an if statement?
I have these game objects in the main menu (play, settings, etc.) that can be selected with either the mouse or a controller by hovering over them and clicking the mouse or pressing x / a depending on the controller type. Previously, this was written:
if (Input.GetKeyDown(KeyCode.Mouse0) || ps4Controller.Land.X.ReadValue() == 1)
and that worked fine, but my issue is that the buttons would get pressed if I was holding down the X button on the controller before I even hovered over the game objects. So while the X button was already being pressed and the "float" value was 1, if I hovered over a game object, the game object would be instantly selected. I wanted to change it so that if the X button was RELEASED on the game object, then it would be selected. I figured using "canceled" would work, but it doesn't seem to, so what can i do here? How can I make "canceled" work in the if statement? P.S. I'm new to Unity.
Answer by henkehedstrom · Jan 18 at 09:12 AM
What does it say if you hover over "canceled"? Could it be that you spelled it wrong and it should be "cancelled" instead? I am not exactly sure what class you use here for the controllers but you could do your own checking if it is released or not. Here is an example of the top of my head:
private float myCurrentValue;
private float myLastValue;
private bool myClickedThisFrame;
void Update()
{
myCurrentValue = ps4Controller.Land.X.ReadValue();
if(myCurrentValue == 1 && myLastValue == 0)
myClickedThisFrame = true;
else
myClickedThisFrame = false;
myLastValue = myCurrentValue;
}
You check every frame what the currentValue is and you also save the value for the last frame, by keeping these variables you could know if the button was just released, if it is currently being held or if it was just clicked. I show you how to check if the button was clicked this frame but I am sure that you can figure out the logic for the other scenarios aswell. The class that you work with probably already has this feature but this could be how it is working underneath. It is not the best solution because you call on ps4Controller.Land.X.ReadValue every frame but I don't think it will be a big hit on your performance.
canceled was spelled correctly, it just says "the event 'InputAction.canceled' can only appear on the left hand side of += ort -=" which I already knew, it needs "context", but nothing seems to work when I put it in an if statement. However, your solution with the "myClickedThisFrame" worked, and I just put that in the if statement instead. Thank you!
I'm glad it helped! If you see this += or -= in the future it probably has something with events/action to do. I do not know the syntax and always have to look it up myself when I work with it :) You basically subscribe on something (in this example when canceled happens) with a function that you have created. If you are interested and want to read more you could check out these links. UnityEvent, UnityAction
Your answer
Follow this Question
Related Questions
Getting analog input values from multiple controllers 2 Answers
How do I add input for two different local players? 1 Answer
How do configure controller trigger input in old input system? 0 Answers
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
Thrustmaster wheel controller giving me weird values 0 Answers