- Home /
How to make behaviour respond to a key press but not a hold?
I'm creating a game which relies on a weapon rotating in intervals around an object. The object will rotate when the specified key is pressed but if I hold it then the object will continue to rotate until the key is released. Does anyone know how I can stop it from rotating further than the interval by holding. Here is my code:
public class WeaponRotation : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey("left"))
{
transform.Rotate(0, 0, 45);
}
if (Input.GetKey("right"))
{
transform.Rotate(0, 0, -45);
} } }
If anyone could help it would be appreciated and any sample code would be a great help.
Answer by perchik · Mar 03, 2014 at 08:04 PM
If you only want it to rotate once, each time you press the key, then use GetKeyUp
instead of GetKey
.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Snap object rotation 1 Answer
Looking for ideas of how to control specific values from C# 1 Answer