I have a function that I use to rotate my GameObject, found on an extremely helpful post on Unity Answers, here, posted by @Hellium.
I’ve updated it slightly to allow me to specify whether the rotation should be clockwise or anti-clockwise:
private void TurnPlayer( Vector3 direction )
{
if( !Rotating )
{
if( direction == Vector3.left) // if left
{
StartCoroutine( Rotate( new Vector3( 0, -90, 0 ), 0.5f ) );
}
else if( direction == Vector3.right) // if right
{
StartCoroutine( Rotate( new Vector3( 0, 90, 0 ), 0.5f ) );
}
}
}
private IEnumerator Rotate( Vector3 angles, float duration )
{
Rotating = true;
Quaternion startRotation = transform.rotation;
Quaternion endRotation = Quaternion.Euler( angles ) * startRotation;
for( float t = 0; t < duration; t += Time.deltaTime )
{
transform.rotation = Quaternion.Lerp( startRotation, endRotation, t / duration );
yield return null;
}
transform.rotation = endRotation;
Rotating = false;
}
I also need a function to move a game object forward/backward in a similar manner, where i know the start position and end position, and smoothly transition to that end position. I guess if Quaternion.Lerp handles the rotation, i’m looking for the position equivalent of that.
How can I convert the code above to allow me to function that moves the GameObject forward / backward?
Thanks!
Of course, I was looking for
– oxidedivisionVector3.Lerp. Thanks very much (again) for your help!