Unity HDRP Product Visualizer 1.0.0
Interactive 3D product showcase built with Unity 6 and HDRP
Loading...
Searching...
No Matches
OrbitCamera.cs
Go to the documentation of this file.
1using UnityEngine;
2
7
28public class OrbitCamera : MonoBehaviour
29{
30 // ------------------------------------------------------------------
31 // Inspector fields
32 // ------------------------------------------------------------------
33
36
40 public Transform target;
41
44 public float distance = 3f;
45
47
50
52 public float autoRotateSpeed = 20f;
53
56 public bool autoRotate = true;
57
59
62
64 public float mouseSensitivity = 200f;
65
67 public float minVerticalAngle = -20f;
68
70 public float maxVerticalAngle = 70f;
71
73
76
78 public float scrollSensitivity = 5f;
79
81 public float minDistance = 1.5f;
82
84 public float maxDistance = 8f;
85
87
88 // ------------------------------------------------------------------
89 // Private state
90 // ------------------------------------------------------------------
91
92 private float _yaw;
93 private float _pitch = 20f;
94 private bool _isDragging;
95 private Vector2 _lastMousePos;
96
97 // ------------------------------------------------------------------
98 // Unity lifecycle
99 // ------------------------------------------------------------------
100
101 void Start() => UpdatePosition();
102
106 void LateUpdate()
107 {
108 HandleMouseInput();
109
110 if (autoRotate && !_isDragging)
111 _yaw += autoRotateSpeed * Time.deltaTime;
112
113 UpdatePosition();
114 }
115
116 // ------------------------------------------------------------------
117 // Private helpers
118 // ------------------------------------------------------------------
119
121 void HandleMouseInput()
122 {
123 if (Input.GetMouseButtonDown(0))
124 {
125 _isDragging = true;
126 _lastMousePos = Input.mousePosition;
127 }
128
129 if (Input.GetMouseButtonUp(0))
130 _isDragging = false;
131
132 if (_isDragging)
133 {
134 Vector2 delta = (Vector2)Input.mousePosition - _lastMousePos;
135 _yaw += delta.x * mouseSensitivity * Time.deltaTime;
136 _pitch -= delta.y * mouseSensitivity * Time.deltaTime;
137 _pitch = Mathf.Clamp(_pitch, minVerticalAngle, maxVerticalAngle);
138 _lastMousePos = Input.mousePosition;
139 }
140
141 float scroll = Input.GetAxis("Mouse ScrollWheel");
142 if (Mathf.Abs(scroll) > 0.001f)
143 {
144 distance -= scroll * scrollSensitivity;
145 distance = Mathf.Clamp(distance, minDistance, maxDistance);
146 }
147 }
148
150 void UpdatePosition()
151 {
152 if (target == null) return;
153 Quaternion rotation = Quaternion.Euler(_pitch, _yaw, 0f);
154 transform.position = target.position + rotation * (Vector3.back * distance);
155 transform.LookAt(target.position);
156 }
157
158 // ------------------------------------------------------------------
159 // Public API
160 // ------------------------------------------------------------------
161
165
168
170 public void ResetView()
171 {
172 _yaw = 0f;
173 _pitch = 20f;
174 distance = 3f;
175 }
176}
Spherical-coordinate camera that orbits around a target Transform.
float minVerticalAngle
Minimum pitch (vertical) angle in degrees.
bool IsAutoRotating
Returns true when auto-rotation is active.
float distance
Current distance from target in world units.
float autoRotateSpeed
Degrees per second of automatic yaw rotation.
void ResetView()
Resets yaw, pitch and distance to their default showcase values.
float maxVerticalAngle
Maximum pitch (vertical) angle in degrees.
float scrollSensitivity
Scroll-wheel zoom sensitivity (world units per scroll unit).
void ToggleAutoRotate()
Flips the auto-rotation state.
float mouseSensitivity
Mouse sensitivity in degrees per pixel-second.
float maxDistance
Farthest allowed distance from the target.
float minDistance
Closest allowed distance to the target.
bool autoRotate
Whether the camera is currently auto-rotating.
Transform target
The point the camera orbits around.