| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using System.Linq; |
| | 5 | |
|
| | 6 | | public class CameraController : MonoBehaviour { |
| | 7 | | #region Singleton |
| | 8 | |
|
| | 9 | | // Singleton instance of the camera controller. |
| 58 | 10 | | public static CameraController Instance { get; private set; } |
| | 11 | |
|
| | 12 | | #endregion |
| | 13 | |
|
| | 14 | | #region Camera Settings |
| | 15 | |
|
| | 16 | | // Determines if mouse input is active for camera control. |
| 1 | 17 | | public bool mouseActive = true; |
| | 18 | |
|
| | 19 | | // Locks user input for camera control. |
| 1 | 20 | | public bool lockUserInput = false; |
| | 21 | |
|
| | 22 | | // Normal speed of camera movement. |
| | 23 | | [SerializeField] |
| 1 | 24 | | private float _cameraSpeedNormal = 100.0f; |
| | 25 | |
|
| | 26 | | // Maximum speed of camera movement. |
| | 27 | | [SerializeField] |
| 1 | 28 | | private float _cameraSpeedMax = 1000.0f; |
| | 29 | |
|
| | 30 | | // Current speed of camera movement. |
| | 31 | | private float _cameraSpeed; |
| | 32 | |
|
| | 33 | | // Horizontal rotation speed. |
| 1 | 34 | | public float _speedH = 2.0f; |
| | 35 | |
|
| | 36 | | // Vertical rotation speed. |
| 1 | 37 | | public float _speedV = 2.0f; |
| | 38 | |
|
| | 39 | | // Current yaw angle of the camera. |
| 1 | 40 | | private float _yaw = 0.0f; |
| | 41 | |
|
| | 42 | | // Current pitch angle of the camera. |
| 1 | 43 | | private float _pitch = 0.0f; |
| | 44 | |
|
| | 45 | | #endregion |
| | 46 | |
|
| | 47 | | #region Orbit Settings |
| | 48 | |
|
| | 49 | | // Determines if the camera should auto-rotate. |
| 1 | 50 | | public bool _autoRotate = false; |
| | 51 | |
|
| | 52 | | // Threat transform for orbit rotation. |
| | 53 | | public Transform target; |
| | 54 | |
|
| | 55 | | // Distance from the camera to the orbit target. |
| | 56 | | [SerializeField] |
| 1 | 57 | | private float _orbitDistance = 5.0f; |
| | 58 | |
|
| | 59 | | // Horizontal orbit rotation speed. |
| | 60 | | [SerializeField] |
| 1 | 61 | | private float _orbitXSpeed = 120.0f; |
| | 62 | |
|
| | 63 | | // Vertical orbit rotation speed. |
| | 64 | | [SerializeField] |
| 1 | 65 | | private float _orbitYSpeed = 120.0f; |
| | 66 | |
|
| | 67 | | // Speed of camera zoom. |
| | 68 | | [SerializeField] |
| 1 | 69 | | private float _zoomSpeed = 500.0f; |
| | 70 | |
|
| | 71 | | // Minimum vertical angle limit for orbit. |
| 1 | 72 | | public float orbitYMinLimit = -20f; |
| | 73 | |
|
| | 74 | | // Maximum vertical angle limit for orbit. |
| 1 | 75 | | public float orbitYMaxLimit = 80f; |
| | 76 | |
|
| | 77 | | // Minimum distance for orbit. |
| 1 | 78 | | private float _orbitDistanceMin = 10f; |
| | 79 | |
|
| | 80 | | // Maximum distance for orbit. |
| | 81 | | [SerializeField] |
| 1 | 82 | | private float _orbitDistanceMax = 20000f; |
| | 83 | |
|
| | 84 | | // Current horizontal orbit angle. |
| 1 | 85 | | private float _orbitX = 0.0f; |
| | 86 | |
|
| | 87 | | // Current vertical orbit angle. |
| 1 | 88 | | private float _orbitY = 0.0f; |
| | 89 | |
|
| | 90 | | #endregion |
| | 91 | |
|
| | 92 | | #region Rendering |
| | 93 | |
|
| | 94 | | // Renderer for the orbit target. |
| | 95 | | public Renderer targetRenderer; |
| | 96 | |
|
| | 97 | | // Renderer for the floor. |
| | 98 | | public Renderer floorRenderer; |
| | 99 | |
|
| | 100 | | // Alpha value for material transparency. |
| | 101 | | public float matAlpha; |
| | 102 | |
|
| | 103 | | #endregion |
| | 104 | |
|
| | 105 | | #region Autoplay Settings |
| | 106 | |
|
| | 107 | | // Speed of camera movement during autoplay. |
| 1 | 108 | | public float autoplayCamSpeed = 2f; |
| | 109 | |
|
| | 110 | | // Duration of horizontal auto-rotation. |
| 1 | 111 | | public float xAutoRotateTime = 5f; |
| | 112 | |
|
| | 113 | | // Duration of vertical auto-rotation. |
| 1 | 114 | | public float yAutoRotateTime = 5f; |
| | 115 | |
|
| | 116 | | // Coroutine for autoplay functionality. |
| | 117 | | private Coroutine autoplayRoutine; |
| | 118 | |
|
| | 119 | | #endregion |
| | 120 | |
|
| | 121 | | #region Camera Presets |
| | 122 | |
|
| | 123 | | // Represents a preset camera position and rotation. |
| | 124 | | [System.Serializable] |
| | 125 | | public struct CameraPreset { |
| | 126 | | public Vector3 position; |
| | 127 | | public Quaternion rotation; |
| | 128 | | } |
| | 129 | |
|
| | 130 | | // Preset camera position for key 4. |
| 1 | 131 | | CameraPreset fourPos = new CameraPreset(); |
| | 132 | |
|
| | 133 | | // Preset camera position for key 5. |
| 1 | 134 | | CameraPreset fivePos = new CameraPreset(); |
| | 135 | |
|
| | 136 | | // Preset camera position for key 6. |
| 1 | 137 | | CameraPreset sixPos = new CameraPreset(); |
| | 138 | |
|
| | 139 | | #endregion |
| | 140 | |
|
| | 141 | | #region Movement |
| | 142 | |
|
| | 143 | | // Mapping of translation inputs to movement vectors. |
| | 144 | | private Dictionary<TranslationInput, Vector3> _translationInputToVectorMap; |
| | 145 | |
|
| | 146 | | // Forward movement vector. |
| 1 | 147 | | Vector3 wVector = Vector3.forward; |
| | 148 | |
|
| | 149 | | // Left movement vector. |
| 1 | 150 | | Vector3 aVector = Vector3.left; |
| | 151 | |
|
| | 152 | | // Backward movement vector. |
| 1 | 153 | | Vector3 sVector = Vector3.back; |
| | 154 | |
|
| | 155 | | // Right movement vector. |
| 1 | 156 | | Vector3 dVector = Vector3.right; |
| | 157 | |
|
| | 158 | | // Angle between forward vector and camera direction. |
| | 159 | | public float forwardToCameraAngle; |
| | 160 | |
|
| 1 | 161 | | public CameraMode cameraMode = CameraMode.FREE; |
| | 162 | |
|
| 1 | 163 | | public int _selectedInterceptorSwarmIndex = -1; |
| 1 | 164 | | public int _selectedThreatSwarmIndex = -1; |
| | 165 | |
|
| | 166 | | private Vector3 _lastCentroid; |
| | 167 | | private Vector3 _currentCentroid; |
| | 168 | | private Vector3 _targetCentroid; |
| | 169 | |
|
| 1 | 170 | | public float _centroidUpdateFrequency = 0.1f; |
| 1 | 171 | | public float _defaultInterpolationSpeed = 5f; |
| | 172 | | [SerializeField] |
| | 173 | | private float _currentInterpolationSpeed; |
| | 174 | |
|
| | 175 | | [SerializeField] |
| 1 | 176 | | private float _iirFilterCoefficient = 0.9f; |
| | 177 | |
|
| | 178 | | private Coroutine _centroidUpdateCoroutine; |
| | 179 | |
|
| | 180 | | #endregion |
| | 181 | |
|
| 1 | 182 | | void SetCameraRotation(Quaternion rotation) { |
| 1 | 183 | | transform.rotation = rotation; |
| 1 | 184 | | _pitch = rotation.eulerAngles.x; |
| 1 | 185 | | _yaw = rotation.eulerAngles.y; |
| 1 | 186 | | } |
| | 187 | |
|
| 28 | 188 | | public void SetCameraSpeed(float speed) { |
| 28 | 189 | | _cameraSpeed = speed; |
| 28 | 190 | | } |
| | 191 | |
|
| 0 | 192 | | public float GetCameraSpeedMax() { |
| 0 | 193 | | return _cameraSpeedMax; |
| 0 | 194 | | } |
| | 195 | |
|
| 28 | 196 | | public float GetCameraSpeedNormal() { |
| 28 | 197 | | return _cameraSpeedNormal; |
| 28 | 198 | | } |
| | 199 | |
|
| 0 | 200 | | public bool IsAutoRotate() { |
| 0 | 201 | | return _autoRotate; |
| 0 | 202 | | } |
| | 203 | |
|
| 0 | 204 | | public void SetAutoRotate(bool autoRotate) { |
| 0 | 205 | | if (autoRotate && !_autoRotate) { |
| 0 | 206 | | _autoRotate = true; |
| 0 | 207 | | autoplayRoutine = StartCoroutine(AutoPlayRoutine()); |
| 0 | 208 | | } else if (!autoRotate && _autoRotate) { |
| 0 | 209 | | _autoRotate = false; |
| 0 | 210 | | StopCoroutine(autoplayRoutine); |
| 0 | 211 | | } |
| 0 | 212 | | } |
| | 213 | |
|
| 0 | 214 | | public static float ClampAngle(float angle, float min, float max) { |
| 0 | 215 | | if (angle < -360F) |
| 0 | 216 | | angle += 360F; |
| 0 | 217 | | if (angle > 360F) |
| 0 | 218 | | angle -= 360F; |
| 0 | 219 | | return Mathf.Clamp(angle, min, max); |
| 0 | 220 | | } |
| | 221 | |
|
| 1 | 222 | | private void Awake() { |
| 2 | 223 | | if (Instance == null) { |
| 1 | 224 | | Instance = this; |
| 1 | 225 | | DontDestroyOnLoad(gameObject); |
| 1 | 226 | | } else { |
| 0 | 227 | | Destroy(gameObject); |
| 0 | 228 | | } |
| | 229 | |
|
| 1 | 230 | | _translationInputToVectorMap = new Dictionary<TranslationInput, Vector3> { |
| | 231 | | { TranslationInput.Forward, wVector }, { TranslationInput.Left, aVector }, |
| | 232 | | { TranslationInput.Back, sVector }, { TranslationInput.Right, dVector }, |
| | 233 | | { TranslationInput.Up, Vector3.up }, { TranslationInput.Down, Vector3.down } |
| | 234 | | }; |
| 1 | 235 | | _currentInterpolationSpeed = _defaultInterpolationSpeed; |
| 1 | 236 | | } |
| | 237 | |
|
| 1 | 238 | | void Start() { |
| 1 | 239 | | fourPos.position = new Vector3(0, 0, 0); |
| 1 | 240 | | fourPos.rotation = Quaternion.Euler(0, 0, 0); |
| 1 | 241 | | fivePos.position = new Vector3(0, 0, 0); |
| 1 | 242 | | fivePos.rotation = Quaternion.Euler(0, 0, 0); |
| 1 | 243 | | sixPos.position = new Vector3(0, 0, 0); |
| 1 | 244 | | sixPos.rotation = Quaternion.Euler(0, 0, 0); |
| | 245 | |
|
| 1 | 246 | | Vector3 angles = transform.eulerAngles; |
| 1 | 247 | | _orbitX = angles.y; |
| 1 | 248 | | _orbitY = angles.x; |
| | 249 | |
|
| 1 | 250 | | UpdateTargetAlpha(); |
| 1 | 251 | | ResetCameraTarget(); |
| | 252 | |
|
| 1 | 253 | | SetCameraMode(CameraMode.FREE); |
| 1 | 254 | | } |
| | 255 | |
|
| 0 | 256 | | public void SnapToSwarm(List<Agent> swarm, bool forceFreeMode = true) { |
| 0 | 257 | | Vector3 swarmCenter = SimManager.Instance.GetSwarmCenter(swarm); |
| 0 | 258 | | SetCameraTargetPosition(swarmCenter); |
| 0 | 259 | | if (forceFreeMode) { |
| 0 | 260 | | SetCameraMode(CameraMode.FREE); |
| 0 | 261 | | } |
| 0 | 262 | | } |
| | 263 | |
|
| 0 | 264 | | public void SnapToNextInterceptorSwarm(bool forceFreeMode = true) { |
| 0 | 265 | | if (SimManager.Instance.GetInterceptorSwarms().Count == 0) { |
| 0 | 266 | | UIManager.Instance.LogActionWarning("[CAM] No interceptor swarms to follow"); |
| 0 | 267 | | return; |
| | 268 | | } |
| | 269 | |
|
| | 270 | | // Set pre-set view 1 |
| 0 | 271 | | _selectedInterceptorSwarmIndex += 1; |
| 0 | 272 | | _selectedThreatSwarmIndex = -1; |
| 0 | 273 | | if (_selectedInterceptorSwarmIndex >= SimManager.Instance.GetInterceptorSwarms().Count) { |
| 0 | 274 | | _selectedInterceptorSwarmIndex = 0; |
| 0 | 275 | | } |
| 0 | 276 | | List<(Agent, bool)> swarm = |
| | 277 | | SimManager.Instance.GetInterceptorSwarms()[_selectedInterceptorSwarmIndex]; |
| 0 | 278 | | string swarmTitle = SimManager.Instance.GenerateInterceptorSwarmTitle(swarm); |
| | 279 | |
|
| | 280 | | // Filter out inactive agents |
| 0 | 281 | | List<(Agent, bool)> activeAgents = swarm.FindAll(tuple => tuple.Item2); |
| 0 | 282 | | List<Agent> activeAgentsList = activeAgents.ConvertAll(tuple => tuple.Item1); |
| 0 | 283 | | Vector3 swarmCenter = SimManager.Instance.GetSwarmCenter(activeAgentsList); |
| 0 | 284 | | SetCameraTargetPosition(swarmCenter); |
| 0 | 285 | | if (forceFreeMode) { |
| 0 | 286 | | SetCameraMode(CameraMode.FREE); |
| 0 | 287 | | } |
| 0 | 288 | | UIManager.Instance.LogActionMessage($"[CAM] Snap to interceptor swarm: {swarmTitle}"); |
| 0 | 289 | | } |
| | 290 | |
|
| 0 | 291 | | public void SnapToNextThreatSwarm(bool forceFreeMode = true) { |
| 0 | 292 | | if (SimManager.Instance.GetThreatSwarms().Count == 0) { |
| 0 | 293 | | return; |
| | 294 | | } |
| 0 | 295 | | _selectedInterceptorSwarmIndex = -1; |
| 0 | 296 | | _selectedThreatSwarmIndex += 1; |
| 0 | 297 | | if (_selectedThreatSwarmIndex >= SimManager.Instance.GetThreatSwarms().Count) { |
| 0 | 298 | | _selectedThreatSwarmIndex = 0; |
| 0 | 299 | | } |
| 0 | 300 | | List<(Agent, bool)> swarm = SimManager.Instance.GetThreatSwarms()[_selectedThreatSwarmIndex]; |
| 0 | 301 | | string swarmTitle = SimManager.Instance.GenerateThreatSwarmTitle(swarm); |
| | 302 | | // Filter out inactive agents |
| 0 | 303 | | List<(Agent, bool)> activeAgents = swarm.FindAll(tuple => tuple.Item2); |
| 0 | 304 | | List<Agent> activeAgentsList = activeAgents.ConvertAll(tuple => tuple.Item1); |
| 0 | 305 | | Vector3 swarmCenter = SimManager.Instance.GetSwarmCenter(activeAgentsList); |
| 0 | 306 | | SetCameraTargetPosition(swarmCenter); |
| 0 | 307 | | if (forceFreeMode) { |
| 0 | 308 | | SetCameraMode(CameraMode.FREE); |
| 0 | 309 | | } |
| 0 | 310 | | UIManager.Instance.LogActionMessage($"[CAM] Snap to threat swarm: {swarmTitle}"); |
| 0 | 311 | | } |
| | 312 | |
|
| 0 | 313 | | public void SnapToCenterAllAgents(bool forceFreeMode = true) { |
| 0 | 314 | | Vector3 swarmCenter = SimManager.Instance.GetAllAgentsCenter(); |
| 0 | 315 | | SetCameraTargetPosition(swarmCenter); |
| 0 | 316 | | if (forceFreeMode) { |
| 0 | 317 | | SetCameraMode(CameraMode.FREE); |
| 0 | 318 | | } |
| 0 | 319 | | UIManager.Instance.LogActionMessage("[CAM] Snap to center all agents"); |
| 0 | 320 | | } |
| | 321 | |
|
| 1 | 322 | | public void SetCameraMode(CameraMode mode) { |
| 2 | 323 | | if (cameraMode == CameraMode.FREE) { |
| 1 | 324 | | if (_centroidUpdateCoroutine != null) { |
| 0 | 325 | | StopCoroutine(_centroidUpdateCoroutine); |
| 0 | 326 | | _centroidUpdateCoroutine = null; |
| 0 | 327 | | } |
| 1 | 328 | | } else { |
| 0 | 329 | | _currentCentroid = _targetCentroid = target.position; |
| 0 | 330 | | } |
| 1 | 331 | | cameraMode = mode; |
| 1 | 332 | | } |
| | 333 | |
|
| 0 | 334 | | private void StartCentroidUpdateCoroutine() { |
| 0 | 335 | | if (_centroidUpdateCoroutine == null) { |
| 0 | 336 | | _centroidUpdateCoroutine = StartCoroutine(UpdateCentroidCoroutine()); |
| 0 | 337 | | } |
| 0 | 338 | | } |
| | 339 | |
|
| 0 | 340 | | public void FollowNextInterceptorSwarm() { |
| 0 | 341 | | SnapToNextInterceptorSwarm(false); |
| 0 | 342 | | StartCentroidUpdateCoroutine(); |
| 0 | 343 | | SetCameraMode(CameraMode.FOLLOW_INTERCEPTOR_SWARM); |
| 0 | 344 | | UIManager.Instance.LogActionMessage("[CAM] Follow next interceptor swarm"); |
| 0 | 345 | | } |
| | 346 | |
|
| 0 | 347 | | public void FollowNextThreatSwarm() { |
| 0 | 348 | | SnapToNextThreatSwarm(false); |
| 0 | 349 | | SetCameraMode(CameraMode.FOLLOW_THREAT_SWARM); |
| 0 | 350 | | StartCentroidUpdateCoroutine(); |
| 0 | 351 | | UIManager.Instance.LogActionMessage("[CAM] Follow next threat swarm"); |
| 0 | 352 | | } |
| | 353 | |
|
| 0 | 354 | | public void FollowCenterAllAgents() { |
| 0 | 355 | | SnapToCenterAllAgents(false); |
| 0 | 356 | | SetCameraMode(CameraMode.FOLLOW_ALL_AGENTS); |
| 0 | 357 | | StartCentroidUpdateCoroutine(); |
| 0 | 358 | | UIManager.Instance.LogActionMessage("[CAM] Follow center all agents"); |
| 0 | 359 | | } |
| | 360 | |
|
| 0 | 361 | | private IEnumerator UpdateCentroidCoroutine() { |
| 0 | 362 | | while (true) { |
| 0 | 363 | | UpdateTargetCentroid(); |
| 0 | 364 | | yield return new WaitForSeconds(_centroidUpdateFrequency); |
| 0 | 365 | | } |
| | 366 | | } |
| | 367 | |
|
| 0 | 368 | | private void UpdateTargetCentroid() { |
| 0 | 369 | | _lastCentroid = _currentCentroid; |
| | 370 | |
|
| 0 | 371 | | if (cameraMode == CameraMode.FOLLOW_INTERCEPTOR_SWARM) { |
| 0 | 372 | | if (_selectedInterceptorSwarmIndex == -1) { |
| 0 | 373 | | _selectedInterceptorSwarmIndex = 0; |
| 0 | 374 | | } |
| 0 | 375 | | if (SimManager.Instance.GetInterceptorSwarms().Count == 0) { |
| 0 | 376 | | return; |
| | 377 | | } |
| 0 | 378 | | _targetCentroid = SimManager.Instance.GetSwarmCenter( |
| | 379 | | SimManager.Instance.GetInterceptorSwarms() [_selectedInterceptorSwarmIndex].ConvertAll( |
| 0 | 380 | | tuple => tuple.Item1)); |
| 0 | 381 | | } else if (cameraMode == CameraMode.FOLLOW_THREAT_SWARM) { |
| 0 | 382 | | if (_selectedThreatSwarmIndex == -1) { |
| 0 | 383 | | _selectedThreatSwarmIndex = 0; |
| 0 | 384 | | } |
| 0 | 385 | | if (SimManager.Instance.GetThreatSwarms().Count == 0) { |
| 0 | 386 | | return; |
| | 387 | | } |
| 0 | 388 | | _targetCentroid = SimManager.Instance.GetSwarmCenter( |
| | 389 | | SimManager.Instance.GetThreatSwarms() [_selectedThreatSwarmIndex].ConvertAll( |
| 0 | 390 | | tuple => tuple.Item1)); |
| 0 | 391 | | } else if (cameraMode == CameraMode.FOLLOW_ALL_AGENTS) { |
| 0 | 392 | | _targetCentroid = SimManager.Instance.GetAllAgentsCenter(); |
| 0 | 393 | | } |
| | 394 | | // Apply IIR filter to adjust interpolation speed |
| 0 | 395 | | float distance = Mathf.Abs(Vector3.Distance(_lastCentroid, _targetCentroid)); |
| 0 | 396 | | float targetSpeed = Mathf.Clamp(distance, 1f, 100000f); |
| 0 | 397 | | _currentInterpolationSpeed = _iirFilterCoefficient * _currentInterpolationSpeed + |
| | 398 | | (1 - _iirFilterCoefficient) * targetSpeed; |
| 0 | 399 | | } |
| | 400 | |
|
| 0 | 401 | | IEnumerator AutoPlayRoutine() { |
| 0 | 402 | | while (true) { |
| 0 | 403 | | float elapsedTime = 0f; |
| 0 | 404 | | while (elapsedTime <= xAutoRotateTime) { |
| 0 | 405 | | _orbitX += Time.unscaledDeltaTime * autoplayCamSpeed * _orbitDistance * 0.02f; |
| 0 | 406 | | UpdateCamPosition(_orbitX, _orbitY); |
| 0 | 407 | | elapsedTime += Time.unscaledDeltaTime; |
| 0 | 408 | | yield return null; |
| 0 | 409 | | } |
| 0 | 410 | | elapsedTime = 0f; |
| 0 | 411 | | while (elapsedTime <= yAutoRotateTime) { |
| 0 | 412 | | _orbitY -= Time.unscaledDeltaTime * autoplayCamSpeed * _orbitDistance * 0.02f; |
| 0 | 413 | | UpdateCamPosition(_orbitX, _orbitY); |
| 0 | 414 | | elapsedTime += Time.unscaledDeltaTime; |
| 0 | 415 | | yield return null; |
| 0 | 416 | | } |
| 0 | 417 | | elapsedTime = 0f; |
| 0 | 418 | | while (elapsedTime <= xAutoRotateTime) { |
| 0 | 419 | | _orbitX -= Time.unscaledDeltaTime * autoplayCamSpeed * _orbitDistance * 0.02f; |
| 0 | 420 | | UpdateCamPosition(_orbitX, _orbitY); |
| 0 | 421 | | elapsedTime += Time.unscaledDeltaTime; |
| 0 | 422 | | yield return null; |
| 0 | 423 | | } |
| 0 | 424 | | elapsedTime = 0f; |
| 0 | 425 | | while (elapsedTime <= yAutoRotateTime) { |
| 0 | 426 | | _orbitY += Time.unscaledDeltaTime * autoplayCamSpeed * _orbitDistance * 0.02f; |
| 0 | 427 | | UpdateCamPosition(_orbitX, _orbitY); |
| 0 | 428 | | elapsedTime += Time.unscaledDeltaTime; |
| 0 | 429 | | yield return null; |
| 0 | 430 | | } |
| 0 | 431 | | yield return null; |
| 0 | 432 | | } |
| | 433 | | } |
| | 434 | |
|
| 0 | 435 | | public void SetCameraTargetPosition(Vector3 position) { |
| 0 | 436 | | target.transform.position = position; |
| 0 | 437 | | UpdateCamPosition(_orbitX, _orbitY); |
| 0 | 438 | | } |
| | 439 | |
|
| 1 | 440 | | void ResetCameraTarget() { |
| | 441 | | RaycastHit hit; |
| 1 | 442 | | if (Physics.Raycast(transform.position, transform.forward, out hit, float.MaxValue, |
| 1 | 443 | | LayerMask.GetMask("Floor"), QueryTriggerInteraction.Ignore)) { |
| 1 | 444 | | target.transform.position = hit.point; |
| 1 | 445 | | _orbitDistance = hit.distance; |
| 1 | 446 | | Vector3 angles = transform.eulerAngles; |
| 1 | 447 | | _orbitX = angles.y; |
| 1 | 448 | | _orbitY = angles.x; |
| 1 | 449 | | UpdateCamPosition(_orbitX, _orbitY); |
| 1 | 450 | | } else { |
| 0 | 451 | | target.transform.position = transform.position + (transform.forward * 100); |
| 0 | 452 | | _orbitDistance = 100; |
| 0 | 453 | | Vector3 angles = transform.eulerAngles; |
| 0 | 454 | | _orbitX = angles.y; |
| 0 | 455 | | _orbitY = angles.x; |
| | 456 | | // UpdateCamPosition(); |
| 0 | 457 | | } |
| 1 | 458 | | } |
| | 459 | |
|
| 0 | 460 | | public void EnableTargetRenderer(bool enable) { |
| 0 | 461 | | targetRenderer.enabled = enable; |
| 0 | 462 | | } |
| | 463 | |
|
| 0 | 464 | | public void EnableFloorGridRenderer(bool enable) { |
| 0 | 465 | | floorRenderer.enabled = enable; |
| 0 | 466 | | } |
| | 467 | |
|
| 0 | 468 | | public void OrbitCamera(float xOrbit, float yOrbit) { |
| 0 | 469 | | if (target) { |
| 0 | 470 | | _orbitX += xOrbit * _orbitXSpeed * _orbitDistance * 0.02f; |
| 0 | 471 | | _orbitY -= yOrbit * _orbitYSpeed * _orbitDistance * 0.02f; |
| | 472 | |
|
| 0 | 473 | | _orbitY = ClampAngle(_orbitY, orbitYMinLimit, orbitYMaxLimit); |
| 0 | 474 | | UpdateCamPosition(_orbitX, _orbitY); |
| 0 | 475 | | } |
| 0 | 476 | | } |
| | 477 | |
|
| 0 | 478 | | public void RotateCamera(float xRotate, float yRotate) { |
| 0 | 479 | | _yaw += xRotate * _speedH; |
| 0 | 480 | | _pitch -= yRotate * _speedV; |
| 0 | 481 | | transform.eulerAngles = new Vector3(_pitch, _yaw, 0.0f); |
| 0 | 482 | | } |
| | 483 | |
|
| 1 | 484 | | private void UpdateCamPosition(float x, float y) { |
| 1 | 485 | | Quaternion rotation = Quaternion.Euler(y, x, 0); |
| | 486 | | RaycastHit hit; |
| | 487 | | // Debug.DrawLine(target.position, transform.position, Color.red); |
| 1 | 488 | | if (Physics.Linecast(target.position, transform.position, out hit, ~LayerMask.GetMask("Floor"), |
| 0 | 489 | | QueryTriggerInteraction.Ignore)) { |
| 0 | 490 | | _orbitDistance -= hit.distance; |
| 0 | 491 | | } |
| 1 | 492 | | Vector3 negDistance = new Vector3(0.0f, 0.0f, -_orbitDistance); |
| 1 | 493 | | Vector3 position = rotation * negDistance + target.position; |
| 1 | 494 | | _orbitDistance = Mathf.Clamp(_orbitDistance, _orbitDistanceMin, _orbitDistanceMax); |
| 1 | 495 | | UpdateTargetAlpha(); |
| | 496 | |
|
| 1 | 497 | | SetCameraRotation(rotation); |
| 1 | 498 | | transform.position = position; |
| 1 | 499 | | } |
| | 500 | |
|
| 0 | 501 | | public void ZoomCamera(float zoom) { |
| 0 | 502 | | _orbitDistance = |
| | 503 | | Mathf.Clamp(_orbitDistance - zoom * _zoomSpeed, _orbitDistanceMin, _orbitDistanceMax); |
| 0 | 504 | | UpdateCamPosition(_orbitX, _orbitY); |
| 0 | 505 | | } |
| | 506 | |
|
| 2 | 507 | | void UpdateTargetAlpha() { |
| 2 | 508 | | matAlpha = (_orbitDistance - _orbitDistanceMin) / (_orbitDistanceMax - _orbitDistanceMin); |
| 2 | 509 | | matAlpha = Mathf.Max(Mathf.Sqrt(matAlpha) - 0.5f, 0); |
| 2 | 510 | | Color matColor = targetRenderer.material.color; |
| 2 | 511 | | matColor.a = matAlpha; |
| 2 | 512 | | targetRenderer.material.color = matColor; |
| 2 | 513 | | } |
| | 514 | |
|
| 0 | 515 | | void UpdateDirectionVectors() { |
| 0 | 516 | | Vector3 cameraToTarget = target.position - transform.position; |
| 0 | 517 | | cameraToTarget.y = 0; |
| 0 | 518 | | forwardToCameraAngle = Vector3.SignedAngle(Vector3.forward, cameraToTarget, Vector3.down); |
| | 519 | |
|
| 0 | 520 | | if (forwardToCameraAngle > -45f && forwardToCameraAngle <= 45f) { |
| 0 | 521 | | _translationInputToVectorMap[TranslationInput.Forward] = Vector3.forward; |
| 0 | 522 | | _translationInputToVectorMap[TranslationInput.Left] = Vector3.left; |
| 0 | 523 | | _translationInputToVectorMap[TranslationInput.Back] = Vector3.back; |
| 0 | 524 | | _translationInputToVectorMap[TranslationInput.Right] = Vector3.right; |
| 0 | 525 | | } else if (forwardToCameraAngle > 45f && forwardToCameraAngle <= 135f) { |
| 0 | 526 | | _translationInputToVectorMap[TranslationInput.Forward] = Vector3.left; |
| 0 | 527 | | _translationInputToVectorMap[TranslationInput.Left] = Vector3.back; |
| 0 | 528 | | _translationInputToVectorMap[TranslationInput.Back] = Vector3.right; |
| 0 | 529 | | _translationInputToVectorMap[TranslationInput.Right] = Vector3.forward; |
| 0 | 530 | | } else if (forwardToCameraAngle > 135f || forwardToCameraAngle <= -135f) { |
| 0 | 531 | | _translationInputToVectorMap[TranslationInput.Forward] = Vector3.back; |
| 0 | 532 | | _translationInputToVectorMap[TranslationInput.Left] = Vector3.right; |
| 0 | 533 | | _translationInputToVectorMap[TranslationInput.Back] = Vector3.forward; |
| 0 | 534 | | _translationInputToVectorMap[TranslationInput.Right] = Vector3.left; |
| 0 | 535 | | } else if (forwardToCameraAngle > -135f && forwardToCameraAngle <= -45f) { |
| 0 | 536 | | _translationInputToVectorMap[TranslationInput.Forward] = Vector3.right; |
| 0 | 537 | | _translationInputToVectorMap[TranslationInput.Left] = Vector3.forward; |
| 0 | 538 | | _translationInputToVectorMap[TranslationInput.Back] = Vector3.left; |
| 0 | 539 | | _translationInputToVectorMap[TranslationInput.Right] = Vector3.back; |
| 0 | 540 | | } |
| 0 | 541 | | } |
| | 542 | |
|
| | 543 | | public enum TranslationInput { Forward, Left, Back, Right, Up, Down } |
| | 544 | |
|
| 0 | 545 | | public void TranslateCamera(TranslationInput input) { |
| 0 | 546 | | if (cameraMode != CameraMode.FREE) { |
| 0 | 547 | | SetCameraMode(CameraMode.FREE); |
| 0 | 548 | | } |
| 0 | 549 | | UpdateDirectionVectors(); |
| 0 | 550 | | target.Translate(_translationInputToVectorMap[input] * Time.unscaledDeltaTime * _cameraSpeed); |
| 0 | 551 | | UpdateCamPosition(_orbitX, _orbitY); |
| 0 | 552 | | } |
| | 553 | |
|
| 28 | 554 | | protected void Update() { |
| 28 | 555 | | if (cameraMode != CameraMode.FREE) { |
| | 556 | | // Use MoveTowards for smoother and more predictable movement |
| 0 | 557 | | _currentCentroid = Vector3.MoveTowards(_currentCentroid, _targetCentroid, |
| | 558 | | _currentInterpolationSpeed * Time.unscaledDeltaTime); |
| 0 | 559 | | SetCameraTargetPosition(_currentCentroid); |
| 0 | 560 | | } |
| 28 | 561 | | } |
| | 562 | | } |
| | 563 | |
|
| | 564 | | public enum CameraMode { FREE, FOLLOW_INTERCEPTOR_SWARM, FOLLOW_THREAT_SWARM, FOLLOW_ALL_AGENTS } |