Question by
NAYIR55IndieDev · Apr 24, 2018 at 01:09 AM ·
rotationrotate objectkeypress
How to make a gameobject rotate coninously on key press?
I'm working on a little project and I want to make a gameobject rotate continously after pressing a key, I did something basic with transform.Rotate(), but I have to press the key or hold it for the gameobject to rotate, here's the script
void Update ()
{
StartTurntableKey();
}
public void StartTurntableKey()
{
if (Input.GetKeyDown(KeyCode.I))
{
turntable.transform.Rotate(Vector3.up * turnSpeed * Time.deltaTime);
}
}
The code is fine (or at least it looks like it is), it supposed to start the rotation, but I have to press the key every frame to rotate...
Any suggestion? Any tip, or snippet to share?
Thanks
Comment
Answer by Alanisaac · Apr 24, 2018 at 01:16 AM
Sounds like you just need a variable to store whether your turntable is rotating or not. Try this on for size:
private bool isRotating;
void Update()
{
StartTurntableKey();
}
public void StartTurntableKey()
{
if (Input.GetKeyDown(KeyCode.I))
{
isRotating = true;
}
else if(Input.GetKeyDown(KeyCode.O))
{
isRotating = false;
}
if(isRotating)
{
turntable.transform.Rotate(Vector3.up * turnSpeed * Time.deltaTime);
}
}
I mapped "stop rotating" to the "O" key but you can map it to whatever you like.