Delay in update and only one Input.GetButton
Hi there,
I have a function in void Update that I would like to give a random delay between 0-3 seconds. In my project I have 3 rotating objects which a player can stop by pressing H, J and K. These currently stop right away, yet I want to delay them. I would also like to limit player to be able to stop only one rotating object, so being able to press only H, J or K at one time.
I have three of the same scripts per rotating object which can be found underneath,
using UnityEngine; using System.Collections;
public class stopRotate1 : MonoBehaviour {
bool rotateObject = true;
void Update ()
{
if (rotateObject == true) {
transform.Rotate (new Vector3 (0, 0, Time.deltaTime * 200));
} else {
transform.Rotate (new Vector3 (0, 0, 0));
}
if (Input.GetButton ("Button1")) {
rotateObject = false;
} else {
rotateObject = true;
}
}
}
Here's a screenshot of my scene, I hope someone can help me with this, I'm quite new to unity, http://i.imgur.com/uOTW1bO.png
Answer by Firedan1176 · Feb 05, 2016 at 05:18 PM
If you use "GetButton", you are using unity's Input system, which will allow you to change controls later on. But if you want to make it as easy as possible, you can use Input.GetKeyDown()
which will specify which key you want to press in your code directly. So let's compare the two:
GetButton("Button1")
- You can always change what "Button1" is, even in-game. GetKeyDown()
- You specify which key you press; you cannot change in-game.
If you want to use GetKeyDown
, try something like this:
if(Input.GetKeyDown(KeyCode.K))
rotationObject = !rotationObject;
What this will do is toggle the boolean value. If you press the 'K' key, even if you're holding it, it will invert the boolean value (false will be true, vise versa).