- Home /
SOLVED Rotate object smoothly to calculated angle on key press (and change the target angle during rotating movement)
Hey!
I know this question has been asked earlier but the given answers haven't worked perfectly for me. So, I have a cube (=mainMenuCube) which I want to rotate smoothly towards the calculated angle on Y axis every key press. Pressing A subtracts 90 degrees from this target angle while pressing D adds 90 degrees to the angle. At the moment, my code looks like this:
GameObject mainMenuCube;
public float newYAngle;
public float oldYAngle;
void Start()
{
mainMenuCube = GameObject.Find("main_menu_cube");
}
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
newYAngle = oldYAngle - 90f;
StartCoroutine(Rotate(newYAngle));
oldYAngle = newYAngle;
}
if (Input.GetKeyDown(KeyCode.D))
{
newYAngle = oldYAngle + 90f;
StartCoroutine(Rotate(newYAngle));
oldYAngle = newYAngle;
}
}
IEnumerator Rotate(float targetAngle)
{
while (mainMenuCube.transform.rotation.y != targetAngle)
{
mainMenuCube.transform.rotation = Quaternion.Slerp(mainMenuCube.transform.rotation, Quaternion.Euler(0f, targetAngle, 0f), 3f * Time.deltaTime);
yield return null;
}
mainMenuCube.transform.rotation = Quaternion.Euler(0f, targetAngle, 0f);
yield return null;
}
However, the code works only once; when I press A or D for the second time, the cube stops before reaching it's destination and starts jittering instead. But I can see that the variables called "newYAngle" and "oldYAngle" are updating correctly in the Inspector and I'm not getting any error messages.
Notice that I want to be able to change the target angle even when the cube is still rotating, not only when it has stopped. In other words, if I press A once, the target angle will be -90 degrees but if I press A again before the previous rotation movement has been finished, the target angle switches to -180 degrees and the cube won't stop at -90 degrees.
By the way, I'm not an experienced coder so try to be clear with your possible answers. And sorry if my English is bad; it's not my mother language.
Answer by Cursed-form · Oct 03, 2019 at 11:11 AM
Wait! I actually managed to sort it out myself right after posting this question. XD All I needed to do was to add StopAllCoroutines();. Here's the final code if someone is interested in coding something similar:
GameObject mainMenuCube;
public float newYAngle;
public float oldYAngle;
void Start()
{
mainMenuCube = GameObject.Find("main_menu_cube");
}
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
StopAllCoroutines();
newYAngle = oldYAngle - 90f;
StartCoroutine(Rotate(newYAngle));
oldYAngle = newYAngle;
}
if (Input.GetKeyDown(KeyCode.D))
{
StopAllCoroutines();
newYAngle = oldYAngle + 90f;
StartCoroutine(Rotate(newYAngle));
oldYAngle = newYAngle;
}
}
IEnumerator Rotate(float targetAngle)
{
while (mainMenuCube.transform.rotation.y != targetAngle)
{
mainMenuCube.transform.rotation = Quaternion.Slerp(mainMenuCube.transform.rotation, Quaternion.Euler(0f, targetAngle, 0f), 3f * Time.deltaTime);
yield return null;
}
mainMenuCube.transform.rotation = Quaternion.Euler(0f, targetAngle, 0f);
yield return null;
}
Use it wisely. ;)
By the way, this script is NOT attached to the rotating cube itself, which is why I have to find it in the Start function. If your script is part of the object to be rotated, the code would be slightly different:
public float newYAngle;
public float oldYAngle;
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
StopAllCoroutines();
newYAngle = oldYAngle - 90f;
StartCoroutine(Rotate(newYAngle));
oldYAngle = newYAngle;
}
if (Input.GetKeyDown(KeyCode.D))
{
StopAllCoroutines();
newYAngle = oldYAngle + 90f;
StartCoroutine(Rotate(newYAngle));
oldYAngle = newYAngle;
}
}
IEnumerator Rotate(float targetAngle)
{
while (transform.rotation.y != targetAngle)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0f, targetAngle, 0f), 3f * Time.deltaTime);
yield return null;
}
transform.rotation = Quaternion.Euler(0f, targetAngle, 0f);
yield return null;
}
Your answer
Follow this Question
Related Questions
Cannot rotate an object around the center 5 Answers
Rotating 2D sprite to match surface. 0 Answers
Rotating an object in a 3D grid 2 Answers
Quaternion and rotation angles 2 Answers