Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Reset camera orbit - can someone please review my code for errors?

Discussion in 'Editor & General Support' started by Pedagogic368, Jun 21, 2012.

  1. Pedagogic368

    Pedagogic368

    Joined:
    Sep 4, 2011
    Posts:
    48
    Hello, I have been working on this for several days and not getting anywhere. I need to "reset" my camera so that each and every time it is used in a screen it start from its initial position and rotation - as if it has not been orbited at all.

    I am following the examples on the forum, setting up position and rotation in start and then calling to these transforms - but nothing is working, it is not resetting - can someone please take a look at my code and let me know what I have missed?

    Thanks in advance.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. /// <summary>
    5. /// Adaptation of the standard MouseOrbit script to use the finger drag gesture to rotate the current object using
    6. /// the fingers/mouse around a target object
    7. /// NOTE: TBInputManager NOT required
    8. /// </summary>
    9. [AddComponentMenu( "FingerGestures/Toolbox/Misc/DragOrbit" )]
    10. public class TBDragOrbitTwo : MonoBehaviour
    11. {
    12.    
    13.     public enum PanMode
    14.     {
    15.         Disabled,
    16.         OneFinger,
    17.         TwoFingers
    18.     }
    19.  
    20.     /// <summary>
    21.     /// The object to orbit around
    22.     /// </summary>
    23.     public Transform target;
    24.  
    25.  
    26.     private Vector3 initialTwoposition;//what I need to reset the camera position
    27.     private Quaternion initialTworotation;//what I need to reset teh camera rotation
    28.    
    29.    
    30.     public float rotationX = 0F;//X rotation
    31.     public float rotationY = 0F;//Y rotation
    32.    
    33.     /// <summary>
    34.     /// Initial camera distance to target
    35.     /// </summary>
    36.     public float initialDistance = 10.0f;
    37.  
    38.     /// <summary>
    39.     /// Minimum distance between camera and target
    40.     /// </summary>
    41.     public float minDistance = 1.0f;
    42.  
    43.     /// <summary>
    44.     /// Maximum distance between camera and target
    45.     /// </summary>
    46.     public float maxDistance = 20.0f;
    47.  
    48.     /// <summary>
    49.     /// Affects horizontal (y-axis) rotation speed
    50.     /// </summary>
    51.     public float yawSensitivity = 80.0f;
    52.  
    53.      /// <summary>
    54.      /// Keep yaw angle value (horizontal rotation) between minYaw and maxYaw
    55.      /// </summary>
    56.      public bool clampYawAngle = false;
    57.      public float minYaw = 0;
    58.      public float maxYaw = 360;
    59.  
    60.     /// <summary>
    61.     /// Affects vertical (X-axis) rotation speed
    62.     /// </summary>
    63.     public float pitchSensitivity = 80.0f;
    64.  
    65.     /// <summary>
    66.     /// Keep pitch angle value between minPitch and maxPitch?
    67.     /// </summary>
    68.     public bool clampPitchAngle = true;
    69.     public float minPitch = -20;
    70.     public float maxPitch = 80;
    71.  
    72.     /// <summary>
    73.     /// Allow the user to affect the orbit distance using the pinch zoom gesture
    74.     /// </summary>
    75.     public bool allowPinchZoom = true;
    76.  
    77.     /// <summary>
    78.     /// Affects pinch zoom speed
    79.     /// </summary>
    80.     public float pinchZoomSensitivity = 2.0f;
    81.  
    82.     /// <summary>
    83.     /// Use smooth camera motions?
    84.     /// </summary>
    85.     public bool smoothMotion = true;
    86.     public float smoothZoomSpeed = 3.0f;
    87.     public float smoothOrbitSpeed = 4.0f;
    88.  
    89.     /// <summary>
    90.     /// Two-Finger camera panning.
    91.     /// Panning will apply an offset to the pivot/camera target point
    92.     /// </summary>
    93.     public bool allowPanning = false;
    94.     public bool invertPanningDirections = false;
    95.     public float panningSensitivity = 1.0f;
    96.     public Transform panningPlane;  // reference transform used to apply the panning translation (using panningPlane.right and panningPlane.up vectors)
    97.     public bool smoothPanning = true;
    98.     public float smoothPanningSpeed = 8.0f;
    99.     float lastPanTime = 0;
    100.  
    101.     float distance = 0.0f;
    102.     float yaw = 0;
    103.     float pitch = 0;
    104.  
    105.     float idealDistance = 0;
    106.     float idealYaw = 0;
    107.     float idealPitch = 0;
    108.    
    109.     /// <summary>
    110.     /// Reset Panning
    111.     /// </summary>
    112.    
    113.     Vector3 idealPanOffset = Vector3.zero;
    114.     Vector3 panOffset = Vector3.zero;
    115.    
    116.     public float Distance
    117.     {
    118.         get { return distance; }
    119.     }
    120.  
    121.     public float IdealDistance
    122.     {
    123.         get { return idealDistance; }
    124.         set { idealDistance = Mathf.Clamp( value, minDistance, maxDistance ); }
    125.     }
    126.  
    127.     public float Yaw
    128.     {
    129.         get { return yaw; }
    130.     }
    131.  
    132.     public float IdealYaw
    133.     {
    134.         get { return idealYaw; }
    135.         set { idealYaw = clampYawAngle ? ClampAngle( value, minYaw, maxYaw): value ; }
    136.     }
    137.  
    138.     public float Pitch
    139.     {
    140.         get { return pitch; }
    141.     }
    142.  
    143.     public float IdealPitch
    144.     {
    145.         get { return idealPitch; }
    146.         set { idealPitch = clampPitchAngle ? ClampAngle( value, minPitch, maxPitch ) : value; }
    147.     }
    148.  
    149.     public Vector3 IdealPanOffset
    150.     {
    151.         get { return idealPanOffset; }
    152.         set { idealPanOffset = value; }
    153.     }
    154.  
    155.     public Vector3 PanOffset
    156.     {
    157.         get { return panOffset; }
    158.     }
    159.  
    160.    void Start()
    161.    
    162.  
    163.     {
    164.    
    165.    
    166.         //Apply();
    167.        
    168.         if( !panningPlane )
    169.             panningPlane = this.transform;
    170.  
    171.       Vector3 angles = transform.eulerAngles;
    172.  
    173.        distance = IdealDistance = initialDistance;
    174.         yaw = IdealYaw  = angles.y;
    175.         pitch = IdealPitch = angles.x;
    176.  
    177.          //Make the rigid body not change rotation
    178.         if( rigidbody )
    179.         rigidbody.freezeRotation = true;
    180.        
    181.         //set the inital position and rotation
    182.         initialTwoposition = GameObject.Find("Camera180").transform.localPosition;
    183.         initialTworotation = transform.localRotation;
    184.    
    185.         //call for a reset
    186.         rotationX = 0;
    187.         rotationY = 0;
    188.         ResetCamera();
    189.        
    190.  
    191.         }
    192.        
    193. //reset the camera
    194.    void ResetCamera()
    195.    
    196. {
    197.         transform.localPosition = initialTwoposition;
    198.         transform.position = initialTwoposition;
    199.        
    200.         transform.localRotation = initialTworotation;
    201.         print(initialTworotation);
    202.        
    203.     }
    204.    
    205.     void OnEnable()
    206.     {
    207.         FingerGestures.OnDragMove += FingerGestures_OnDragMove;
    208.         FingerGestures.OnPinchMove += FingerGestures_OnPinchMove;
    209.         FingerGestures.OnTwoFingerDragMove += FingerGestures_OnTwoFingerDragMove;
    210.     }
    211.        
    212.     void OnDisable()
    213.     {
    214.         FingerGestures.OnDragMove -= FingerGestures_OnDragMove;
    215.         FingerGestures.OnPinchMove -= FingerGestures_OnPinchMove;
    216.         FingerGestures.OnTwoFingerDragMove -= FingerGestures_OnTwoFingerDragMove;
    217.     }
    218.  
    219.     void FingerGestures_OnDragMove( Vector2 fingerPos, Vector2 delta )
    220.     {
    221.         // if we panned recently, give a bit of time for all the fingers to lift off before we allow for one-finger drag
    222.         if( Time.time - lastPanTime < 0.25f )
    223.             return;
    224.  
    225.         if( target )
    226.         {
    227.             IdealYaw += delta.x * yawSensitivity * 0.02f;
    228.             IdealPitch -= delta.y * pitchSensitivity * 0.02f;
    229.         }
    230.     }
    231.  
    232.     void FingerGestures_OnPinchMove( Vector2 fingerPos1, Vector2 fingerPos2, float delta )
    233.     {
    234.         if( allowPinchZoom )
    235.             IdealDistance -= delta * pinchZoomSensitivity;
    236.     }
    237.  
    238.     void FingerGestures_OnTwoFingerDragMove( Vector2 fingerPos, Vector2 delta )
    239.     {
    240.         if( allowPanning )
    241.         {
    242.             Vector3 move = -0.02f * panningSensitivity * ( panningPlane.right * delta.x + panningPlane.up * delta.y );
    243.            
    244.             if( invertPanningDirections )
    245.                 IdealPanOffset -= move;
    246.             else
    247.                 IdealPanOffset += move;
    248.  
    249.             lastPanTime = Time.time;
    250.         }
    251.     }
    252.  
    253.     void Apply()
    254.     {
    255.         if( smoothMotion )
    256.         {
    257.             distance = Mathf.Lerp( distance, IdealDistance, Time.deltaTime * smoothZoomSpeed );
    258.             yaw = Mathf.Lerp( yaw, IdealYaw, Time.deltaTime * smoothOrbitSpeed );
    259.             pitch = Mathf.Lerp( pitch, IdealPitch, Time.deltaTime * smoothOrbitSpeed );
    260.         }
    261.         else
    262.         {
    263.             distance = IdealDistance;
    264.             yaw = IdealYaw;
    265.             pitch = IdealPitch;
    266.         }
    267.  
    268.         if( smoothPanning )
    269.             panOffset = Vector3.Lerp( panOffset, idealPanOffset, Time.deltaTime * smoothPanningSpeed );
    270.         else
    271.             panOffset = idealPanOffset;
    272.  
    273.         transform.rotation = Quaternion.Euler( pitch, yaw, 0 );
    274.         transform.position = ( target.position + panOffset ) - distance * transform.forward;
    275.     }
    276.  
    277.     void LateUpdate()
    278.     {
    279.         Apply();
    280.     }
    281.  
    282.     static float ClampAngle( float angle, float min, float max )
    283.     {
    284.         if( angle < -360 )
    285.             angle += 360;
    286.        
    287.         if( angle > 360 )
    288.             angle -= 360;
    289.  
    290.         return Mathf.Clamp( angle, min, max );
    291.     }
    292.  
    293.     // recenter the camera
    294.     public void ResetPanning()
    295.     {
    296.         IdealPanOffset = Vector3.zero;
    297.     }
    298.    
    299. }
    300.  
    301.  
    302.  
     
  2. Pedagogic368

    Pedagogic368

    Joined:
    Sep 4, 2011
    Posts:
    48
    bump. please?
     
  3. Pedagogic368

    Pedagogic368

    Joined:
    Sep 4, 2011
    Posts:
    48
    UPDATE. I have been able to set up a button that resets the camera. The problem is that it does not reset it to the original yaw, pitch and position - it keeps "resetting to the LAST yaw and pitch and position -

    Can anyone help me figure out how I can set up the Original rotation and position in start so that it goes back to this on "reset" thanks in advance.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. /// <summary>
    5. /// Adaptation of the standard MouseOrbit script to use the finger drag gesture to rotate the current object using
    6. /// the fingers/mouse around a target object
    7. /// NOTE: TBInputManager NOT required
    8. /// </summary>
    9. [AddComponentMenu( "FingerGestures/Toolbox/Misc/DragOrbitTwo" )]
    10. public class TBDragOrbitTwo : MonoBehaviour
    11. {
    12.    
    13.     public enum PanMode
    14.     {
    15.         Disabled,
    16.         OneFinger,
    17.         TwoFingers
    18.     }
    19.  
    20.     /// <summary>
    21.     /// The object to orbit around
    22.     /// </summary>
    23.     public Transform target;
    24.    
    25.    
    26.     /// <summary>
    27.     /// Initial camera distance to target
    28.     /// </summary>
    29.     public float initialDistance = 10.0f;
    30.  
    31.     /// <summary>
    32.     /// Minimum distance between camera and target
    33.     /// </summary>
    34.     public float minDistance = 1.0f;
    35.  
    36.     /// <summary>
    37.     /// Maximum distance between camera and target
    38.     /// </summary>
    39.     public float maxDistance = 20.0f;
    40.  
    41.     /// <summary>
    42.     /// Affects horizontal (y-axis) and (X-axis) rotation speed
    43.     /// </summary>
    44.     public float yawSensitivity = 80.0f;
    45.     public float pitchSensitivity = 80.0f;
    46.    
    47.      /// <summary>
    48.      /// Keep yaw angle value (horizontal rotation) between minYaw and maxYaw
    49.      /// </summary>
    50.      public bool clampYawAngle = false;
    51.      public float minYaw = 0;
    52.      public float maxYaw = 360;
    53.  
    54.  
    55.     /// <summary>
    56.     /// Keep pitch angle value between minPitch and maxPitch?
    57.     /// </summary>
    58.     public bool clampPitchAngle = true;
    59.     public float minPitch = -20;
    60.     public float maxPitch = 80;
    61.    
    62.     public float yaw = 0;
    63.     float pitch = 0;
    64.    
    65.     float idealYaw = 0;
    66.     float idealPitch = 0;
    67.    
    68.    
    69.     /// <summary>
    70.     /// Allow the user to affect the orbit distance using the pinch zoom gesture
    71.     /// </summary>
    72.     public bool allowPinchZoom = true;
    73.  
    74.     /// <summary>
    75.     /// Affects pinch zoom speed
    76.     /// </summary>
    77.     public float pinchZoomSensitivity = 2.0f;
    78.  
    79.     /// <summary>
    80.     /// Use smooth camera motions?
    81.     /// </summary>
    82.     public bool smoothMotion = true;
    83.     public float smoothZoomSpeed = 3.0f;
    84.     public float smoothOrbitSpeed = 4.0f;
    85.  
    86.     /// <summary>
    87.     /// Two-Finger camera panning.
    88.     /// Panning will apply an offset to the pivot/camera target point
    89.     /// </summary>
    90.     public bool allowPanning = false;
    91.     public bool invertPanningDirections = false;
    92.     public float panningSensitivity = 1.0f;
    93.     public Transform panningPlane;  // reference transform used to apply the panning translation (using panningPlane.right and panningPlane.up vectors)
    94.     public bool smoothPanning = true;
    95.     public float smoothPanningSpeed = 8.0f;
    96.     float lastPanTime = 0;
    97.  
    98.     float distance = 0.0f;
    99.     float idealDistance = 0;
    100.    
    101.    
    102.     /// <summary>
    103.     /// Reset Panning
    104.     /// </summary>
    105.    
    106.     Vector3 idealPanOffset = Vector3.zero;
    107.     Vector3 panOffset = Vector3.zero;
    108.  
    109.    
    110.     //reset camera
    111.     public bool resetCam;
    112.     private Vector3 resetTwoposition;
    113.     private Quaternion resetTworotation;
    114.     Quaternion originalRotation;
    115.    
    116.  
    117.     public float Distance
    118.     {
    119.         get { return distance; }
    120.     }
    121.  
    122.     public float IdealDistance
    123.     {
    124.         get { return idealDistance; }
    125.         set { idealDistance = Mathf.Clamp( value, minDistance, maxDistance ); }
    126.     }
    127.  
    128.     public float Yaw
    129.     {
    130.         get { return yaw; }
    131.     }
    132.  
    133.     public float IdealYaw
    134.     {
    135.         get { return idealYaw; }
    136.         set { idealYaw = clampYawAngle ? ClampAngle( value, minYaw, maxYaw): value ; }
    137.     }
    138.  
    139.     public float Pitch
    140.     {
    141.         get { return pitch; }
    142.     }
    143.  
    144.     public float IdealPitch
    145.     {
    146.         get { return idealPitch; }
    147.         set { idealPitch = clampPitchAngle ? ClampAngle( value, minPitch, maxPitch ) : value; }
    148.     }
    149.  
    150.     //----------------------------------
    151.     public Vector3 IdealPanOffset
    152.    {
    153.        get { return idealPanOffset; }
    154.         set { idealPanOffset = value; }
    155.    }
    156.  
    157.     public Vector3 PanOffset
    158.     {
    159.         get { return panOffset; }
    160.     }
    161.    
    162.    void Start()
    163.    
    164.  
    165.     {
    166.         //Apply();
    167.        
    168.         if( !panningPlane )
    169.             panningPlane = this.transform;
    170.  
    171.         Vector3 angles = transform.eulerAngles;
    172.  
    173.         distance = IdealDistance = initialDistance;
    174.         //yaw = IdealYaw  = angles.y;
    175.         //pitch = IdealPitch = angles.x;
    176.  
    177.          //Make the rigid body not change rotation
    178.         if( rigidbody )
    179.         rigidbody.freezeRotation = true;
    180.        
    181.         //set the inital position and rotation
    182.         resetTwoposition = GameObject.Find("Camera180").transform.localPosition;
    183.         resetTworotation = transform.localRotation;
    184.        
    185.         if (target)
    186.  
    187.         {
    188.  
    189.             transform.localPosition = target.localPosition;
    190.         }
    191.  
    192.         else
    193.  
    194.             target.localPosition = transform.localPosition;
    195.  
    196.        
    197.         originalRotation = transform.localRotation;
    198.    
    199.         //ResetCamera();
    200.        
    201.         //ResetPanning();
    202.  
    203.         }
    204.        
    205.  
    206.    
    207.     void OnEnable()
    208.     {
    209.         FingerGestures.OnDragMove += FingerGestures_OnDragMove;
    210.         FingerGestures.OnPinchMove += FingerGestures_OnPinchMove;
    211.         FingerGestures.OnTwoFingerDragMove += FingerGestures_OnTwoFingerDragMove;
    212.     }
    213.        
    214.     void OnDisable()
    215.     {
    216.         FingerGestures.OnDragMove -= FingerGestures_OnDragMove;
    217.         FingerGestures.OnPinchMove -= FingerGestures_OnPinchMove;
    218.         FingerGestures.OnTwoFingerDragMove -= FingerGestures_OnTwoFingerDragMove;
    219.     }
    220.  
    221.     void FingerGestures_OnDragMove( Vector2 fingerPos, Vector2 delta )
    222.     {
    223.         // if we panned recently, give a bit of time for all the fingers to lift off before we allow for one-finger drag
    224.         if( Time.time - lastPanTime < 0.25f )
    225.             return;
    226.  
    227.         if( target )
    228.         {
    229.             IdealYaw += delta.x * yawSensitivity * 0.02f;
    230.             IdealPitch -= delta.y * pitchSensitivity * 0.02f;
    231.         }
    232.     }
    233.  
    234.     void FingerGestures_OnPinchMove( Vector2 fingerPos1, Vector2 fingerPos2, float delta )
    235.     {
    236.         if( allowPinchZoom )
    237.             IdealDistance -= delta * pinchZoomSensitivity;
    238.     }
    239.  
    240.     void FingerGestures_OnTwoFingerDragMove( Vector2 fingerPos, Vector2 delta )
    241.     {
    242.         if( allowPanning )
    243.         {
    244.             Vector3 move = -0.02f * panningSensitivity * ( panningPlane.right * delta.x + panningPlane.up * delta.y );
    245.            
    246.             if( invertPanningDirections )
    247.                 IdealPanOffset -= move;
    248.             else
    249.                 IdealPanOffset += move;
    250.  
    251.             lastPanTime = Time.time;
    252.         }
    253.     }
    254.  
    255.     void Apply()
    256.     {
    257.         if( smoothMotion )
    258.         {
    259.             distance = Mathf.Lerp( distance, IdealDistance, Time.deltaTime * smoothZoomSpeed );
    260.             yaw = Mathf.Lerp( yaw, IdealYaw, Time.deltaTime * smoothOrbitSpeed );
    261.             pitch = Mathf.Lerp( pitch, IdealPitch, Time.deltaTime * smoothOrbitSpeed );
    262.         }
    263.         else
    264.         {
    265.             distance = IdealDistance;
    266.             yaw = IdealYaw;
    267.             pitch = IdealPitch;
    268.         }
    269.  
    270.         if( smoothPanning )
    271.             panOffset = Vector3.Lerp( panOffset, idealPanOffset, Time.deltaTime * smoothPanningSpeed );
    272.         else
    273.             panOffset = idealPanOffset;
    274.  
    275.         transform.rotation = Quaternion.Euler( pitch, yaw, 0 );
    276.         transform.position = ( target.position + panOffset ) - distance * transform.forward;
    277.     }
    278.  
    279.     void LateUpdate()
    280.     {
    281.         Apply();
    282.     }
    283.  
    284.     static float ClampAngle( float angle, float min, float max )
    285.     {
    286.         if( angle < -360 )
    287.             angle += 360;
    288.        
    289.         if( angle > 360 )
    290.             angle -= 360;
    291.  
    292.         return Mathf.Clamp( angle, min, max );
    293.     }
    294.  
    295.     // recenter the camera
    296.     public void ResetPanning()
    297.     {
    298.         IdealPanOffset = Vector3.zero;
    299.         print(IdealPanOffset);
    300.        
    301.     }
    302.    
    303.    public void ResetCamera()
    304.    
    305.     {
    306.         transform.localPosition = resetTwoposition;
    307.         transform.position = resetTwoposition;
    308.         print(resetTwoposition);
    309.    
    310.         transform.localRotation = resetTworotation;
    311.         transform.rotation = resetTworotation;
    312.            
    313.     }
    314.    
    315.  
    316.     void OnGUI ()
    317.     {
    318.    
    319.         if( GUI.Button(new Rect (739, 390, 97, 34),"Reset")){
    320.        
    321.        
    322.         yaw = 0;
    323.         pitch = 0; 
    324.         ResetCamera();
    325.         ResetPanning();
    326.         }
    327.     }
    328. }
     
  4. Pedagogic368

    Pedagogic368

    Joined:
    Sep 4, 2011
    Posts:
    48
    UPDATE (SOLVED)

    lines 22 thru 23 as follows:

    yaw = idealYaw = 0;
    pitch = idealPitch = 0;
     
  5. Giantbean

    Giantbean

    Joined:
    Dec 13, 2012
    Posts:
    144
    I am trying to understand your fix so I can add a reset to the C# MouseOrbitZoom camera from the wiki.

    I have been setting the transform.position and transform.rotation to an initial camera location but it only works for one frame whether I use void LateUpdate or Void Update. I'm not sure what part of my understanding is missing, but days of work and I still can not get the camera to reset.

    Any ideas?

    Also did you really place the yaw and pitch code at line 22 and 23 (" /// </summary> , public Transform target;") or was it line 122 thru 123 or even line 222 thru 223 or a different line altogether?