- Home /
Detect when a UI button is not pressed and change GO transform rotation.
Hello. Hi. I'm developing a game for Android devices and I have a UI button that when pressed adds force to a GameObject. What I can't find is to detect that the button is not pressed and when it's not pressed, make a lerp between the rotation of the GO at that moment with another rotation that I want. The code I have now is this:
public float buttonForce;
private Rigidbody goRigidbody;
void Awake () {
goRigidbody = GetComponent<Rigidbody>();
}
public void ButtonAddForce () {
goRigidbody.AddForce (new Vector2 (0, buttonForce));
}
Answer by knorke · Sep 29, 2017 at 02:01 PM
A button not being pressed is the normal state you could try to set it explicit with a boolean e.g.
private bool pressed;
Set it true in your OnClick function and false in Update()
Then you could also check in the Update function if your key is not pressed
if (!pressed)
{
// do something
}
If you want to go on step further take a look at this component
It should have every event you could possibly need.
Answer by BlakeSchreurs · Sep 29, 2017 at 02:13 PM
There's a little bit of a disconnect here between discrete and continuous actions.
The playing game happens continuously. There are always updates, and the simulation continues in a steady stream.
UI events happen as discrete events. If someone clicks a button, something happens immediately at an instant.
When is the button not pressed? If you're just responding to an action, instead of checking button state, the only time the button is ever pressed are the few microseconds it takes for your ButtonAddForce to execute... For all intents and purposes, all of the time.
Most often, I see systems where either a) the game is monitoring the state of a button and reacts continuously to that button every frame, or b) the button press instantly changes the state of an object, and that object takes on a new behavior based on state over time (leading all rotations and physics actions to be taking place in the continuous simulation), and eventually reverts back to the previous state.