SoFunction
Updated on 2025-03-07

Unity: Enable the camera to rotate around a certain point for one week

Execute in the Update function:

if (turnAround)
  {
    (, ,  * rotateSpeed);
    degree +=  * rotateSpeed;
    if (degree >= 360)
    {
      turnAround = false;        
      degree = 0;
     }
   }

Supplement: The unity camera rotates and zooms in and out around an object

The script uses an object as the center point

To control the camera to rotate and zoom around the object, the script can be hung on the camera, and the target is the object to be viewed

public Transform Camera2;
public GameObject target;
private float dis;
public float xSpeed = 200, ySpeed = 200, mSpeed = 10;                   //Movement speedpublic float yMinLimit = -50, yMaxLimit = 50;                           //The minimum maximum limit on the Y-axis movement of the camerapublic float distance = 7, minDistance = 2, maxDistance = 30;           //The distance between the camera and the target objectpublic bool needDamping = true;                                         //Damping is turned on by defaultfloat damping = 5.0f;                                                   //The default damping is 5.0Fpublic float x = 0.0f;                                                  //X axispublic float y = 0.0f;                                                  //Y axis// Use this for initialization
void Start() {
    instance = this;
    Camr = ;
    Vector3 angles = ;
    x = ;
    y = ;  
}
private void Update()
{
}
void LateUpdate()
{
            // Use the left mouse button to move the object            if ((1))
            {
                x += ("Mouse X") * xSpeed * 0.02f;
                y -= ("Mouse Y") * ySpeed * 0.02f;
                y = ClampAngle(y, yMinLimit, yMaxLimit);
            }
            distance -= ("Mouse ScrollWheel") * mSpeed;
            distance = (distance, minDistance, maxDistance);
            Quaternion rotation = (y, x, 0.0f);
            Vector3 disVector = new Vector3(0.0f, 0.0f, -distance);
            Vector3 position = rotation * disVector + ;
            //adjust the camera
            if (needDamping)
            {
                 = (, rotation,  * damping);
                 = (, position,  * damping);
            }
            else
            {
                 = rotation;
                 = position;
            }   
}
/// <summary>
/// Control of rotation angle/// </summary>
/// <param name="angle">Rotation angle</param>/// <param name="min">Minimum angle</param>/// <param name="max">Maximum angle</param>/// &lt;returns&gt;&lt;/returns&gt;
static float ClampAngle(float angle, float min, float max)
{
    if (angle &lt; -360)
        angle += 360;
    if (angle &gt; 360)
        angle -= 360;
    return (angle, min, max);
}
void MoveCameraToTarget()
{
    if (target != null)
    {
        ();
         = (, , 5 * );
        dis = (, );
        if (dis &lt; 1.5f)
        {
            Camr = ;
            CancelInvoke();
        }
    }
}

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.