# Rotate Child GameObject Over Time

Good Morning All,

I have a character gameobject with a few child gameobjects that lead to the sprite.

• Character
• Character Rotator
• Sprite Holder

I have been flipping the character like this depending on if left or right is pressed and it's worked fine:

// Flip the player's sprite based on input and x vector
if (myVector.x < 0 && facingRight)
{
facingRight = !facingRight;
// childToRotate is Character Rotator
hildToRotate.transform.localRotation = Quaternion.Euler(0, 180, 0);
}

if (myVector.x > 0 && !facingRight)
{
facingRight = !facingRight;
// childToRotate is Character Rotator
childToRotate.transform.localRotation = Quaternion.Euler(0, 0, 0);
}


Now, I want to rotate the character 180 degrees over time, rather than it being an instant flip. ( Just like in Paper Mario games )

This was my attempt at creating the effect:

public IEnumerator RotateCharacterModel()
{
float startRotation = childToRotate.transform.eulerAngles.y;
float endRotation = startRotation + 180f;
float t = 0.0f;
float duration = .3f;
while (t < duration)
{
t += Time.deltaTime;
float yRotation = Mathf.Lerp(startRotation, endRotation, t / duration) % 180.0f;

// this is where I need to rotate the "Character Rotator's" local rotation but I am struggling...

}
_characterIsRotating = false;
}


I think I'm very close, I just need assistance replacing the last comment with code that rotates the "Character Rotators" local rotation.

Any assistance with this would be immensely appreciated! Thank you for taking the time!

Interpolating Euler angles is fraught. You'll generally get more intuitive behaviour with fewer confusing edge cases by expressing your orientations as Quaternions, and interpolating those:

public IEnumerator RotateCharacterModel()
{
Quaternion startRotation = childToRotate.transform.rotation;
// Rotate start by 180 degrees on the world y axis.
Quaternion endRotation = Quaternion.Euler(0, 180, 0) * startRotation;

float duration = .3f;
// Thinking of this as a speed saves a division every step.
float speed = 1f/duration;

// Use a for loop instead of a while,
// that way all your loop variable stuff stays together.
for (float t = 0; t < 1f; t += speed * Time.deltaTime)
{
childToRotate.transform.rotation = Quaternion.Lerp(
startRotation,
endRotation,
t
);
}
// Snap to the final orientation (loop can end with spare change left over)
childToRotate.transform.rotation = endRotation;
_characterIsRotating = false;
}

• Nobody more helpful than Douglas! This worked flawlessly. I especially appreciate you commenting it all out so I can understand ^^. Though, there is one unintended issue. Along with the Y rotation changing from 0 > - 180 depending on the direction, the X axis is changing from 0 > 18 as well. I made sure that the gameobject's rotation is (0,0,0). Any idea why this could be occurring? Commented Nov 17, 2022 at 1:44
• As a follow-up, I realized why this is occurring. The gameobject's parent has a rotation of 9 in the x axis. Is there a way to only rotate "childToRotate"? Thanks! Commented Nov 17, 2022 at 1:56
• So sorry to triple comment. I don't believe there is an edit button ^^; I realized a more accurate question is: "Is there a way to rotate childToRotate independent of its parent's rotation?" Commented Nov 17, 2022 at 2:14
• Replace all instances of .rotation with .localRotation to rotate in the parent's local space rather than world space. Commented Nov 17, 2022 at 2:49
• All issues resolved! Thanks so much! Commented Nov 17, 2022 at 3:12