0

I've currently got a simple rotation on an object through code using eulerAngles, like this (This is just a quick example):

float rotateSpeed = 200;
float rotateZ = 0.0f;
public KeyCode FTurn; 

void FNotation(){
    if (Input.GetKeyDown(FTurn)){
       GetComponent<Transform>().eulerAngles = new Vector3(0.0f, 0.0f, rotateZ += 90f)
    }
}

void Update(){
    FNotation();
}

This code works fine as it rotates my object when I press 'F'. The only issue is that it performs this function in 1 frame due to the Update function, I know I can make animations and call these when pressed instead, but I wanted to know if there's a way through code for the cube to visually rotate instead of just in 1 frame?

When I press or hold the F key, my object rotates by 90 degrees but only in 1 frame, however I want the object to visually rotate 90 degrees only when I've pressed the 'F' key once! I do not want to be able to hold the key whilst the object is constantly spinning.

toadflax
  • 375
  • 4
  • 17
  • You want it to keep rotating until the key is released? – Programmer Feb 19 '18 at 14:05
  • No actually, only if the key is pressed once! Sorry I should have clarified that. – toadflax Feb 19 '18 at 14:06
  • Keep rotating when a key is pressed? When should it stop rotating? Please edit your question add what you expect to actually happen and what is happening now. Your question is not clear. – Programmer Feb 19 '18 at 14:08
  • @Programmer Just edited my question, hope that clears things up abit. – toadflax Feb 19 '18 at 14:12
  • Ok. You can do that with a coroutine function.The duplicate shows you how to rotate object to any angle over time. It shows shows how to rotate to a rotation over time. Choose which one you want. Just start a coroutine with one of those 2 functions when you press the key. – Programmer Feb 19 '18 at 14:15

0 Answers0