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