Guest User

CamOrbiting.cs

a guest
Jul 1st, 2025
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.49 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(Camera))]
  4. public class CamOrbiting : MonoBehaviour
  5. {
  6.     public Transform targetMesh;
  7.     public float distance = 10f;
  8.     public float zoomSpeed = 2f;
  9.     public float orbitSpeed = 100f;
  10.     public float panSpeed = 0.01f;
  11.     public float minZoom = 1f;
  12.     public float maxZoom = 100f;
  13.     Vector3 orbitCenter;
  14.     float yaw = 0f;
  15.     float pitch = 20f;
  16.     Camera m_cam;
  17.     void Start()
  18.     {
  19.         m_cam = GetComponent<Camera>();
  20.         if (targetMesh != null)
  21.         {
  22.             orbitCenter = GetMeshBoundsCenter(targetMesh);
  23.         }
  24.     }
  25.  
  26.     void Update()
  27.     {
  28.         HandleOrbit();
  29.         HandlePan();
  30.         HandleZoom();
  31.     }
  32.  
  33.     void HandleOrbit()
  34.     {
  35.         if (Input.GetMouseButton(0)) // rotate with left click
  36.         {
  37.             yaw += Input.GetAxis("Mouse X") * orbitSpeed * Time.unscaledDeltaTime;// unscaled delta time in case you're using it in a paused menu
  38.             pitch -= Input.GetAxis("Mouse Y") * orbitSpeed * Time.unscaledDeltaTime;
  39.             pitch = Mathf.Clamp(pitch, -89f, 89f);
  40.         }
  41.         Quaternion rotation = Quaternion.Euler(pitch, yaw, 0f);
  42.         Vector3 direction = rotation * Vector3.forward;
  43.         transform.position = orbitCenter - direction * distance;
  44.         transform.rotation = rotation;
  45.     }
  46.  
  47.     void HandlePan()
  48.     {
  49.         if (Input.GetMouseButton(2)) // pan with middle mouse button
  50.         {
  51.             float panX = -Input.GetAxis("Mouse X") * panSpeed * m_cam.orthographicSize;
  52.             float panY = -Input.GetAxis("Mouse Y") * panSpeed * m_cam.orthographicSize;
  53.             // move orbit center based on camera right and up
  54.             Vector3 right = transform.right;
  55.             Vector3 up = transform.up;
  56.             orbitCenter += right * panX + up * panY;
  57.         }
  58.     }
  59.  
  60.     void HandleZoom()
  61.     {
  62.         float scroll = Input.GetAxis("Mouse ScrollWheel");
  63.         m_cam.orthographicSize -= scroll * zoomSpeed;
  64.         m_cam.orthographicSize = Mathf.Clamp(m_cam.orthographicSize, minZoom, maxZoom);
  65.     }
  66.  
  67.     Vector3 GetMeshBoundsCenter(Transform target)
  68.     {
  69.         var renderers = target.GetComponentsInChildren<Renderer>();
  70.         if (renderers.Length == 0) return target.position;
  71.         Bounds combinedBounds = renderers[0].bounds;
  72.         for (int i = 1; i < renderers.Length; i++)
  73.         {
  74.             combinedBounds.Encapsulate(renderers[i].bounds);
  75.         }
  76.         return combinedBounds.center;
  77.     }
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment