- Home /
Question by
argentummanusbeta · Apr 01, 2020 at 10:07 PM ·
rotationlerplookat
Slowly Rotate to Look at
I have a simple line of code that slowly rotates towards a ray cast point. I use a reference point to know the current point that is looked at. turret.LookAt(Vector3.Lerp(hit.point, refPoint.position, 0.75f)); This always ends up "Snapping" to the proper rotation. Why is this?
Comment
Best Answer
Answer by Hellium · Apr 01, 2020 at 10:20 PM
Because you don't actually use Lerp
correctly (see the answer I gave here: https://answers.unity.com/questions/998544/having-difficulty-with-lerp.html)
Either make the 3rd parameter of your Lerp
method go from 0
to 1
overtime, or use the Quaternion.RotateTowards
method:
public float RotationSpeed = 1;
private Quaternion targetRotation;
private void Start()
{
targetRotation = turret.rotation;
}
void Update()
{
if (Physics.Raycast(...))
{
targetRotation = Quaternion.LookRotation(hit.point - turret.position);
}
turret.rotation = Quaternion.RotateTowards(turret.rotation, targetRotation, Time.deltaTime * RotationSpeed);
}