- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
lotzi11 · Sep 22, 2014 at 07:25 AM ·
transform.rotateinput.getkey
How to get constant rotation from tapping a button
I want to be able to press a button and have my gameObject be constantly rotating on the y-axis. When I press the same button again, I want my gameObject to stop rotating. Here's is what I have so far.
public int speed;
void Update(){
if (Input.GetKey (KeyCode.W)) {
transform.Rotate (Vector3.up * Time.deltaTime * speed );
}
}
When I press W it will start to rotate but stop with I let go of the button.
Comment
Answer by HarshadK · Sep 22, 2014 at 07:26 AM
This will work:
public int speed;
bool shouldRotate = false;
void Update(){
if (Input.GetKeyDown(KeyCode.W))
{
shouldRotate = !shouldRotate;
}
if(shouldRotate)
{
transform.Rotate (Vector3.up * Time.deltaTime * speed );
}
}
Your answer
Follow this Question
Related Questions
Limiting and Rotating a Platform. 1 Answer
Transform.rotation vs Wrapping 1 Answer
How detect player is which side of box? 2 Answers
Any way to sync transform 1 Answer
How to use a negative value when MathF.Clamping a eulerAngle? 1 Answer