Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to smoothly rotate a camera around a point

Discussion in 'Scripting' started by TxAg03, Mar 21, 2016.

  1. TxAg03

    TxAg03

    Joined:
    May 20, 2015
    Posts:
    21
    Trying to create a view gizmo of my own (in game) that behaves like the Unity view gizmo in the top-right corner. Problem I'm having is the rotating of the camera around a point. I was using Vector3.Lerp to change the position of the camera and update the LookAt, but the effect is off a bit because the position isn't a set distance away from the point, as it would be if rotating around that point. Because of scripts already in place, I need to rotate the camera instead of gameObjects.

    Any suggestions on how to create a RotateAround Lerp would be greatly appreciated. Thank you.
     
  2. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    You can multiply a quat by a vector to rotate it the amount of the quat.

    Code (csharp):
    1.  
    2. Transform trans = /*the camera transform*/;
    3. Vector3 point = /*the point around which we're rotating*/;
    4. Quaternion rot = /*the amount of change in rotation we want to rotate by*/;
    5.  
    6. var v = trans.position - point;
    7. v = rot * v;
    8. trans.position = point + v;
    9.  
    Calculating that rotation will probably be easiest as an 'axis angle'. The axis being defined by whatever control/handle the user is selecting, and the angle is the speed that you rotate by.
     
  4. TxAg03

    TxAg03

    Joined:
    May 20, 2015
    Posts:
    21
    Thank you both for your quick replies.

    Zaflis, I could be wrong, but doesn't Quaternion.Lerp only rotate the object in place and not around a specific point?

    lordofduct, that would work, but how would I get it to do that over a period of time like Lerp does? I like the idea of a slight animation as the camera spins around to the view selected.
     
  5. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    the 'rot' Quaternion should be the 'amount' of change (not an orientation)... so lets say that you want to rotate around Vector3.up, at about 10 degrees per second. You might say:

    Code (csharp):
    1.  
    2. Quaternion rot = Quaternion.AngleAxis(10f * Time.deltaTime, Vector3.up);
    3.  
    And you'd run my original code in 'Update' so that it moved that amount based on some conditional... like:

    Code (csharp):
    1.  
    2. public class RotateSelfOnSpaceHeld : MonoBehaviour
    3. {
    4.    
    5.     public Vector3 PointToRotateAround = Vector3.zero;
    6.     public Vector3 Axis = Vector3.up;
    7.     public float Speed = 10f;
    8.    
    9.     void Update()
    10.     {
    11.        
    12.         if(Input.GetKey(KeyCode.Space))
    13.         {
    14.             var rot = Quaternion.AngleAxis(this.Speed * Time.deltaTime, this.Axis);
    15.             var v = this.transform.position - this.PointToRotateAround;
    16.             v = rot * v;
    17.             this.transform.position = this.PointToRotateAround + v;
    18.         }
    19.        
    20.     }
    21.    
    22. }
    23.  
     
  7. TxAg03

    TxAg03

    Joined:
    May 20, 2015
    Posts:
    21
    john_goren likes this.