3
\$\begingroup\$

The Question is basically self explanatory. I have a scene with many cameras and I'd like to smoothly switch from one to another. I am not looking for a cross fade effect but more to a camera moving and rotating the view in order to reach the next camera point of view and so on. To this end I have tried the following code:

    firstCamera.transform.position.x = Mathf.Lerp(firstCamera.transform.position.x, nextCamer.transform.position.x,Time.deltaTime*smooth);
    firstCamera.transform.position.y = Mathf.Lerp(firstCamera.transform.position.y, nextCamera.transform.position.y,Time.deltaTime*smooth);
    firstCamera.transform.position.z = Mathf.Lerp(firstCamera.transform.position.z, nextCamera.transform.position.z,Time.deltaTime*smooth);

    firstCamera.transform.rotation.x = Mathf.Lerp(firstCamera.transform.rotation.x, nextCamera.transform.rotation.x,Time.deltaTime*smooth);
    firstCamera.transform.rotation.z = Mathf.Lerp(firstCamera.transform.rotation.z, nextCamera.transform.rotation.z,Time.deltaTime*smooth);
    firstCamera.transform.rotation.y = Mathf.Lerp(firstCamera.transform.rotation.y, nextCamera.transform.rotation.y,Time.deltaTime*smooth);

But the result is actually not that good.

Thank you all, I fixed it this way

 var pos:Vector3 = firstCamera.transform.position;
    var rot:Quaternion = firstCamera.transform.rotation;
    firstCamera.transform.position = Vector3.Lerp(pos, nextCamera.transform.position,Time.deltaTime*smooth);
    firstCamera.transform.rotation = Quaternion.Lerp(rot, nextCamera.transform.rotation,Time.deltaTime*smooth);
\$\endgroup\$
  • \$\begingroup\$ in the first line is the ply1.transform.position.x a mistake and meant to be nextCamera.transform.position.x? \$\endgroup\$ – Tetrad Jun 15 '12 at 20:23
  • \$\begingroup\$ also, consider using a 3rd party tweening library like iTween \$\endgroup\$ – Tetrad Jun 15 '12 at 20:26
  • \$\begingroup\$ You are using Lerp() incorrectly; please see this blog article. \$\endgroup\$ – BlueRaja - Danny Pflughoeft Feb 13 '15 at 1:06
  • \$\begingroup\$ Using Lerp to perform an exponential ease-out blend like this is not incorrect per se — it cam often give a very pleasing feel for camera moves in particular. We just need to ensure that we adjust for deltaTime correctly, as this is done wrong in almost every example out there. \$\endgroup\$ – DMGregory Jan 18 '18 at 18:08
5
\$\begingroup\$

Consider using Quaternion.Lerp or Quaternion.Slerp instead of lerping the euler angles directly.

\$\endgroup\$
1
\$\begingroup\$

You need to save the original position/rotation of firstCamera. It seems to be using the current values.

\$\endgroup\$
  • \$\begingroup\$ Using the current position as the start of a lerp and passing in a small value (i.e. delta time) is a cheap way of doing an ease-out. \$\endgroup\$ – Tetrad Jun 15 '12 at 20:24
  • \$\begingroup\$ That's true. I was trying to come up with a reason "the result is not that good". \$\endgroup\$ – Jimmy Jun 15 '12 at 20:38

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.