- Home /
Switch rotation on click
I'm getting slightly mad with this one. I have this game object which should rotate (forever) in one direction when a button is clicked.
BUT:
You can click on another button and the object should rotate (forever) in the opposite direction. Something like:
gameobject.transform.Rotate = Vector3.down rotSpeed Time.deltaTime; gameobject.transform.Rotate = Vector3.down -rotSpeed Time.deltaTime;
I know that the rotSpeed neutralizes itself and that's why the rotation stops entirely when I click the other button.
Isn't there a smart way to do so?
Answer by Zodiarc · Oct 26, 2016 at 08:20 AM
public class Rotator : MonoBehaviour {
public float rotSpeed = 5.0f;
private int direction = 1;
public void Update() {
if(Input.GetKeyDown(KeyCode.F)) {
this.direction *= -1;
}
this.transform.Rotate(Vector3.down * this.direction * this.rotSpeed * Time.deltaTime);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612095441im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotate gameobject with mouse/touch similar to meshlab or other 3D softwear 1 Answer
Change rotation until it's at 0,0,0 1 Answer
Direction of rotation 0 Answers
Return Object Rotation to x axis is zero 1 Answer
Rotating Rigidbodies 1 Answer