I'm trying to rotate the camera with mathf.sin which is working fine with the below code. However it doesn't transition smoothly when run. Code (CSharp): function Update () { if(Time.timeSinceLevelLoad > 5) { transform.rotation = Quaternion.Euler(0,0, Mathf.Sin(Time.realtimeSinceStartup) * 70); } }
I'm going to take a guess and what you mean by transition smoothly and post this fix. Code (csharp): function Update () { float startRotationTime = 5; if(Time.timeSinceLevelLoad > startRotationTime) { transform.rotation = Quaternion.Euler(0,0, Mathf.Sin(Time.realtimeSinceStartup - startRotationTime) * 70); } }
I have the camera with rotation (34.17,0,0). When it runs the script the camera doesn't transition smoothly to the back and forth rotation of the camera. It initially snaps to the location and then proceeds to sway smoothly. Should I be using Lerp or Slerp?
well yeah, because you're setting the rotation to a new rotation. Try saving the original rotation and then multiply it with the sway rotation. Note that I don't know java syntax by heart so this will have errors. Code (csharp): startRotation : Quaternion; function Start() { startRotation = transform.rotation; } function Update () { if(Time.timeSinceLevelLoad > 5) { transform.rotation = startRotation * Quaternion.Euler(0,0, Mathf.Sin(Time.realtimeSinceStartup) * 70); } }
Actually another thing you could do is just call transform.Rotate() Code (csharp): function Update () { if( Time.timeSinceLevelLoad > 5) { transform.Rotate( Vector3( 0, 0, Mathf.Sin(Time.realtimeSinceStartup) * 70 ), Space.Self); } }