How to gradually rotate an object towards another object in unity?
I tried using transform.lookAt(), in rotates object instantly, but i need to rotate it slower. Also I need to rotate camera, but player should save control over it. What can i do?
New contributor
Vovchik_tv Vovchik_tv is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
Use Quaternion.Slerp function to do it.
public Transform target; // The object to rotate towards
public float rotationSpeed = 2.0f; // Rotation speed
void Update()
{
if (!target)
return;
Vector3 direction = target.position - transform.position;
Quaternion targetRotation = Quaternion.LookRotation(direction);
// Slerp from current rotation to the target rotation
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
}