Question by
DeaDTeeN85 · Jul 31, 2016 at 06:32 PM ·
transformrotate
how to stop a object spinning on key press
so im trying to get a object to stop spinning when i press the space key but i dont know how to stop it this is what i have so far:
// Update is called once per frame
void Update()
{
transform.Rotate(new Vector3(spinSpeed * Time.deltaTime, 0.0f, 0.0f));
if (Input.GetKey(KeyCode.Space))
{
transform.Rotate(0f, 0f, 0f, Space.Self);
}
Comment
Answer by vintar · Jul 31, 2016 at 09:24 PM
This is what I would do. You can stop and start rotation with key press :
bool _shouldTurn;
void Update()
{
if (Input.GetKey(KeyCode.Space))
{
_shouldTurn = !_shouldTurn;
}
if(_shouldTurn)
{
transform.Rotate(new Vector3(spinSpeed * Time.deltaTime, 0.0f, 0.0f));
}
}
Your answer
