0
\$\begingroup\$

enter image description here As picture depicted my camera moving on path i want to lookAt bath different object smoothly. Currently have tried

trasnform.LooAt(activePath.gameObject.transform);

but it producing jerky result. object suddenly lookAt to next object how to avoid it. I searched and found this sloution but it also not working

var targetRotation = Quaternion.LookRotation(activePath.gameObject.transform.position - transform.position);
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime);
\$\endgroup\$
  • \$\begingroup\$ Slerp is an interpolation. Delta time must be a ratio of TimeSinceLookingAtLastTarget/TotalTimeDesiredToLookAtNextTarget. If you expect it to take 2 seconds to turn from target A to target B, and it's been .25 seconds, the interpolation ratio should be 0.125. \$\endgroup\$ – RobStone May 16 '16 at 15:47
2
\$\begingroup\$

Slerp is an interpolation. The final parameter T must be a ratio of TimeSinceLookingAtLastTarget/TotalTimeDesiredToLookAtNextTarget.

0 <= T <= 1

If you expect it to take 2 seconds to turn from target A to target B, and it's been .25 seconds, the interpolation ratio should be 0.125.

Time.DeltaTime is just the number of seconds in took to process the last frame. It should always be consistently low and won't change much. At 60FPS, that code would leave you looking 1/60th of the way between two targets.

So you will need to know the actual time and how long it should take. When you start the turn, capture

float SecondsToTurn = 2; //adjust this

float TurnStartTime = Time.Time;

Each frame calculate your slerpT as

float slerpT = (Time.Time - TurnStartTime) / SecondsToTurn

Use slerpT instead of Time.deltaTime in your Quaternion.Slerp

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.