Search Unity

Rotating Camera with mathf.sin

Discussion in 'Scripting' started by Bazoozoo, Nov 25, 2015.

  1. Bazoozoo

    Bazoozoo

    Joined:
    Mar 27, 2015
    Posts:
    28
    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):
    1. function Update ()
    2. {
    3.      if(Time.timeSinceLevelLoad >  5)
    4.      {  
    5.            transform.rotation = Quaternion.Euler(0,0, Mathf.Sin(Time.realtimeSinceStartup) * 70);
    6.      }
    7. }
     
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    I'm going to take a guess and what you mean by transition smoothly and post this fix.

    Code (csharp):
    1.  
    2. function Update ()
    3. {
    4.      float startRotationTime = 5;
    5.      if(Time.timeSinceLevelLoad >  startRotationTime)
    6.      {  
    7.            transform.rotation = Quaternion.Euler(0,0, Mathf.Sin(Time.realtimeSinceStartup - startRotationTime) * 70);
    8.      }
    9. }
    10.  
     
  3. Bazoozoo

    Bazoozoo

    Joined:
    Mar 27, 2015
    Posts:
    28
    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?
     
  4. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    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):
    1.  
    2. startRotation : Quaternion;
    3. function Start()
    4. {
    5.       startRotation = transform.rotation;
    6. }
    7.  
    8. function Update ()
    9. {
    10.      if(Time.timeSinceLevelLoad >  5)
    11.      {  
    12.            transform.rotation = startRotation * Quaternion.Euler(0,0, Mathf.Sin(Time.realtimeSinceStartup) * 70);
    13.      }
    14. }
    15.  
    16.  
     
  5. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    Actually another thing you could do is just call transform.Rotate()

    Code (csharp):
    1.  
    2. function Update ()
    3. {
    4.      if( Time.timeSinceLevelLoad >  5)
    5.      {  
    6.           transform.Rotate( Vector3( 0, 0, Mathf.Sin(Time.realtimeSinceStartup) * 70 ), Space.Self);
    7.      }
    8. }
    9.