- Home /
Rotate an object's z value to a certain an angle over time then rotate it back to the original angle
Hi, I'm having trouble understanding coroutines and rotation in general. I'm trying to make a 2D top down sprite attack animation similar to the one in this video... https://www.youtube.com/watch?v=yr_DLfoz8Jo
My goal: when a character inputs an attack key, the sprite would be rotated by a certain angle, pause for a very short time, then rotates back to the exact original angle.
My codes so far:
public float maxTimer = 0.2f;
public float waitTime = 0.1f;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
StartCoroutine(RotateObject());
}
}
IEnumerator RotateObject()
{
float timer = 0f;
while (timer <= maxTimer)
{
transform.Rotate(Vector3.forward * Time.deltaTime * (40/maxTimer));
timer += Time.deltaTime;
yield return null;
}
yield return new WaitForSeconds(waitTime);
timer = 0f;
while (timer <= maxTimer)
{
transform.Rotate(Vector3.forward * Time.deltaTime * (-40/maxTimer));
timer += Time.deltaTime;
yield return null;
}
}
Now when I execute this code, the z value would slightly be different every time I hit space instead of returning to the original value of 0 (for example 1.1, -1.3, 0.5, -2.3, etc...). I also think there has to be a cleaner way of doing this. Note that the value 40 in the script is the angle value I want the sprite to be rotated by, and -40 to rotate it back to the original value. Any help would be appreciated!
Answer by unity_ek98vnTRplGj8Q · Feb 06, 2020 at 04:04 PM
I bet the reason you are seeing these variations is that you don't stop when timer == maxTimer, you stop on the first frame that its greater than maxTimer, and with variations on Time.deltaTime the amount of time you rotate forwards is going to differ slightly from the amount of time you rotate back. I personally recommend using Quaternion.Slerp since it will automatically clamp the rotation at the max value (and you will never overshoot). You can try this code out, I have not tested it but I think it will solve the issues you are having.
IEnumerator RotateObject()
{
Quaternion startingRotation = transform.rotation;
Quaternion targetRotation = startingRotation * Quaternion.Euler(0,0,40);
float timer = 0f;
while (timer <= maxTimer)
{
timer += Time.deltaTime;
transform.rotation = Quaternion.Slerp(startingRotation, targetRotation, timer/maxTimer);
yield return null;
}
yield return new WaitForSeconds(waitTime);
timer = 0f;
while (timer <= maxTimer)
{
timer += Time.deltaTime;
transform.rotation = Quaternion.Slerp(targetRotation, startingRotation, timer/maxTimer);
yield return null;
}
}
Thank you for your reply. This solution fixed the issue. Now, there's a new problem. I also want the rotation to only happen when the object has been rotated back to the original angle, otherwise there's a bug when I execute RotateObject() again before the second while loop is finished running after the WaitForSeconds() function. The rotation value gets all messed up if I spam the space key multiple times before the whole rotation animation is finished. Therefore, I added a boolean variable to check by setting it to true right when it's executed and false after the second while loop is finished. I am not sure if this is an efficient way of solving this problem.
public float maxTimer = 0.2f;
public float waitTime = 0.1f;
public bool isRotating;
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
if (!isRotating)
{
StartCoroutine(RotateObject());
}
}
}
IEnumerator RotateObject()
{
Quaternion startingRotation = transform.rotation;
// Quaternion startingRotation = Quaternion.Euler(0,0,0);
Quaternion targetRotation = startingRotation * Quaternion.Euler(0,0,40);
isRotating = true;
float timer = 0f;
while (timer <= maxTimer)
{
timer += Time.deltaTime;
transform.rotation = Quaternion.Slerp(startingRotation, targetRotation, timer/maxTimer);
yield return null;
}
yield return new WaitForSeconds(waitTime);
timer = 0f;
while (timer <= maxTimer)
{
timer += Time.deltaTime;
transform.rotation = Quaternion.Slerp(targetRotation, startingRotation, timer/maxTimer);
yield return null;
}
isRotating = false;
}
Is this the right way to prevent this from happening?
I don't see anything wrong with it, that's probably how I would have chosen to do it as well
Your answer
![](https://koobas.hobune.stream/wayback/20220612222833im_/https://answers.unity.com/themes/thub/images/avi.jpg)