- Home /
Smoothly Rotate Object Towards Target Object
I want to rotate my player vehicle into the target object direction/side. Though the following image, I have tried to explain my point in a better way:
I want to rotate my below tank object towards another tank object so that it can point into that direction.
I have written this code for this purpose but it is not working:
IEnumerator DoRotationAtTargetDirection(Transform opponentPlayer)
{
Quaternion targetRotation = Quaternion.identity;
do
{
Debug.Log("do rotation");
Vector3 targetDirection = opponentPlayer.position - transform.position;
targetRotation = Quaternion.LookRotation(targetDirection);
Quaternion nextRotation = Quaternion.Lerp(transform.localRotation, targetRotation, Time.deltaTime);
transform.localRotation = nextRotation;
yield return null;
} while (Quaternion.Angle(transform.localRotation, targetRotation) < 0.01f);
}
I just want to smoothly rotate and stop towards a target object. Please share your suggestion regarding this.
Answer by Hellium · Sep 05, 2019 at 04:20 PM
Quaternion.LookRotation(targetDirection)
defines a rotation in world space, but you are trying to change the local rotation of the object. Try:
Quaternion nextRotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime);
transform.rotation = nextRotation;
But I would use RotateTowards
instead
Vector3 targetDirection = (opponentPlayer.position - transform.position).normalized;
targetRotation = Quaternion.LookRotation(targetDirection);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation , Time.deltaTime);
yield return null;
Also, don't forget to change your condition to
while (Quaternion.Angle(transform.rotation, targetRotation) > 0.01f);
Still, it's not doing a complete rotation or turning the face towards another target object!!.
This is the updated code that is executing:
IEnumerator DoRotationAtTargetDirection(Transform opponentPlayer)
{
Quaternion targetRotation = Quaternion.identity;
do
{
Debug.Log("do rotation");
Vector3 targetDirection = (opponentPlayer.position - transform.position).normalized;
targetRotation = Quaternion.LookRotation(targetDirection);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime);
yield return null;
} while (Quaternion.Angle(transform.rotation, targetRotation) < 0.01f);
}
Didn't see the condition.
Change it to
while (Quaternion.Angle(transform.rotation, targetRotation) > 0.01f);
You want the object to continue while the object is not aligned with the target rotation.
You are a genius man :) Thank you so much - can you post updated code as an answer for both questions?
so I can mark this as correct.
Your answer
Follow this Question
Related Questions
Unit rotation fails consistently on all slerp rotations after the first? 0 Answers
How to ROTATE an object without slowing the ends (lerp) 3 Answers
How can I use lerp to rotate an object multiple times? 2 Answers
Quaternion Slerp Sanity Check 1 Answer
Is it possible to use a quaternion from a gameobjects position to another gameobjects position? 1 Answer