| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | public class Interceptor : Agent { |
| | 6 | | [SerializeField] |
| 16 | 7 | | private float _navigationGain = 3f; // Typically 3-5. |
| | 8 | |
|
| | 9 | | [SerializeField] |
| 16 | 10 | | protected bool _showDebugVectors = true; |
| | 11 | |
|
| | 12 | | private GameObject _missileTrailEffect; |
| 16 | 13 | | private bool _missileTrailEffectAttached = false; |
| | 14 | |
|
| | 15 | | private Coroutine _returnParticleToManagerCoroutine; |
| | 16 | |
|
| | 17 | | private Vector3 _accelerationInput; |
| | 18 | |
|
| 16 | 19 | | private double _elapsedTime = 0; |
| | 20 | |
|
| 0 | 21 | | protected override void Awake() { |
| 0 | 22 | | base.Awake(); |
| 0 | 23 | | SetFlightPhase(FlightPhase.INITIALIZED); |
| 0 | 24 | | } |
| | 25 | |
|
| 14 | 26 | | public override bool IsAssignable() { |
| 14 | 27 | | bool assignable = !HasAssignedTarget(); |
| 14 | 28 | | return assignable; |
| 14 | 29 | | } |
| | 30 | |
|
| 0 | 31 | | protected override void UpdateReady(double deltaTime) { |
| 0 | 32 | | Vector3 accelerationInput = Vector3.zero; |
| 0 | 33 | | Vector3 acceleration = CalculateAcceleration(accelerationInput); |
| 0 | 34 | | GetComponent<Rigidbody>().AddForce(acceleration, ForceMode.Acceleration); |
| 0 | 35 | | } |
| | 36 | |
|
| 0 | 37 | | protected override void FixedUpdate() { |
| 0 | 38 | | base.FixedUpdate(); |
| 0 | 39 | | if (_showDebugVectors) { |
| 0 | 40 | | DrawDebugVectors(); |
| 0 | 41 | | } |
| 0 | 42 | | } |
| | 43 | |
|
| 0 | 44 | | protected override void UpdateBoost(double deltaTime) { |
| 0 | 45 | | if (_missileTrailEffect == null) { |
| 0 | 46 | | AttachMissileTrailEffect(); |
| 0 | 47 | | } |
| 0 | 48 | | UpdateMissileTrailEffect(); |
| | 49 | |
|
| | 50 | | // Calculate the boost acceleration. |
| 0 | 51 | | float boostAcceleration = |
| | 52 | | (float)((staticConfig.BoostConfig?.BoostAcceleration ?? 0) * Constants.kGravity); |
| 0 | 53 | | Vector3 boostAccelerationVector = boostAcceleration * transform.forward; |
| | 54 | |
|
| | 55 | | // Add the PN acceleration to the boost acceleration. |
| 0 | 56 | | Vector3 controllerAcceleration = CalculateAccelerationInput(deltaTime); |
| 0 | 57 | | Vector3 accelerationInput = boostAccelerationVector + controllerAcceleration; |
| | 58 | |
|
| | 59 | | // Calculate the total acceleration. |
| 0 | 60 | | Vector3 acceleration = CalculateAcceleration(accelerationInput); |
| | 61 | |
|
| | 62 | | // Apply the acceleration. |
| 0 | 63 | | GetComponent<Rigidbody>().AddForce(acceleration, ForceMode.Acceleration); |
| 0 | 64 | | } |
| | 65 | |
|
| 0 | 66 | | protected override void UpdateMidCourse(double deltaTime) { |
| 0 | 67 | | UpdateMissileTrailEffect(); |
| | 68 | |
|
| 0 | 69 | | _elapsedTime += deltaTime; |
| 0 | 70 | | Vector3 accelerationInput = CalculateAccelerationInput(deltaTime); |
| | 71 | |
|
| | 72 | | // Calculate and set the total acceleration. |
| 0 | 73 | | Vector3 acceleration = CalculateAcceleration(accelerationInput); |
| 0 | 74 | | GetComponent<Rigidbody>().AddForce(acceleration, ForceMode.Acceleration); |
| 0 | 75 | | } |
| | 76 | |
|
| 0 | 77 | | private Vector3 CalculateAccelerationInput(double deltaTime) { |
| 0 | 78 | | Vector3 accelerationInput = Vector3.zero; |
| 0 | 79 | | float maxNormalAcceleration = CalculateMaxNormalAcceleration(); |
| | 80 | |
|
| 0 | 81 | | if (!HasAssignedTarget()) { |
| | 82 | | // No assigned target: do not generate control input. |
| | 83 | | // Gravity and drag are applied centrally in AerialAgent.CalculateAcceleration, |
| | 84 | | // so returning zero here ensures idle missiles/carriers still experience gravity |
| | 85 | | // and do not artificially hover. |
| 0 | 86 | | _accelerationInput = Vector3.zero; |
| 0 | 87 | | return _accelerationInput; |
| | 88 | | } |
| | 89 | |
|
| 0 | 90 | | UpdateTargetModel(deltaTime); |
| | 91 | |
|
| | 92 | | // Check whether the threat should be considered a miss. |
| 0 | 93 | | SensorOutput sensorOutput = GetComponent<Sensor>().Sense(_target); |
| | 94 | | // TODO(dlovell): This causes trouble with the Fateh 110B (high-speed threats). |
| | 95 | | // if (sensorOutput.velocity.range > 1000f) { |
| | 96 | | // HandleInterceptMiss(); |
| | 97 | | // return Vector3.zero; |
| | 98 | | // } |
| | 99 | |
|
| | 100 | | IController controller; |
| 0 | 101 | | switch (agentConfig.DynamicConfig.FlightConfig.ControllerType) { |
| 0 | 102 | | case Configs.ControllerType.ProportionalNavigation: { |
| 0 | 103 | | controller = new PnController(this, _navigationGain); |
| 0 | 104 | | break; |
| | 105 | | } |
| 0 | 106 | | case Configs.ControllerType.AugmentedProportionalNavigation: { |
| 0 | 107 | | controller = new ApnController(this, _navigationGain); |
| 0 | 108 | | break; |
| | 109 | | } |
| 0 | 110 | | default: { |
| 0 | 111 | | Debug.LogError( |
| | 112 | | $"Controller type {agentConfig.DynamicConfig.FlightConfig.ControllerType} not found."); |
| 0 | 113 | | controller = new PnController(this, _navigationGain); |
| 0 | 114 | | break; |
| | 115 | | } |
| | 116 | | } |
| 0 | 117 | | accelerationInput = controller.Plan(); |
| | 118 | |
|
| | 119 | | // Clamp the normal acceleration input to the maximum normal acceleration. |
| 0 | 120 | | accelerationInput = Vector3.ClampMagnitude(accelerationInput, maxNormalAcceleration); |
| 0 | 121 | | _accelerationInput = accelerationInput; |
| 0 | 122 | | return accelerationInput; |
| 0 | 123 | | } |
| | 124 | |
|
| 0 | 125 | | private void UpdateTargetModel(double deltaTime) { |
| | 126 | | // Skip if target/model/sensor is unavailable. This prevents null dereferences |
| | 127 | | // during phases before assignment or when configs are incomplete. |
| 0 | 128 | | if (_targetModel == null || _target == null) { |
| 0 | 129 | | return; |
| | 130 | | } |
| | 131 | |
|
| 0 | 132 | | var sensorConfig = agentConfig?.DynamicConfig?.SensorConfig; |
| 0 | 133 | | if (sensorConfig == null || sensorConfig.Frequency <= 0f) { |
| 0 | 134 | | return; |
| | 135 | | } |
| | 136 | |
|
| 0 | 137 | | _elapsedTime += deltaTime; |
| 0 | 138 | | float sensorUpdatePeriod = 1f / sensorConfig.Frequency; |
| 0 | 139 | | if (_elapsedTime >= sensorUpdatePeriod) { |
| | 140 | | // TODO: Implement guidance filter to estimate state from the sensor output. |
| | 141 | | // For now, we'll use the threat's actual state. |
| 0 | 142 | | _targetModel.SetPosition(_target.GetPosition()); |
| 0 | 143 | | _targetModel.SetVelocity(_target.GetVelocity()); |
| 0 | 144 | | _targetModel.SetAcceleration(_target.GetAcceleration()); |
| 0 | 145 | | _elapsedTime = 0; |
| 0 | 146 | | } |
| 0 | 147 | | } |
| | 148 | |
|
| 0 | 149 | | private void OnTriggerEnter(Collider other) { |
| | 150 | | // Check if the interceptor hit the floor with a negative vertical speed. |
| 0 | 151 | | if (other.gameObject.name == "Floor" && Vector3.Dot(GetVelocity(), Vector3.up) < 0) { |
| 0 | 152 | | HandleHitGround(); |
| 0 | 153 | | } |
| | 154 | |
|
| | 155 | | // Check if the collision is with another agent. |
| 0 | 156 | | Agent otherAgent = other.gameObject.GetComponentInParent<Agent>(); |
| 0 | 157 | | if (otherAgent != null && otherAgent.GetComponent<Threat>() != null && |
| 0 | 158 | | _target == otherAgent as Threat) { |
| | 159 | | // Check kill probability before marking as hit. |
| 0 | 160 | | float killProbability = otherAgent.staticConfig.HitConfig?.KillProbability ?? 0; |
| | 161 | |
|
| 0 | 162 | | if (Random.value <= killProbability) { |
| | 163 | | // Mark both this agent and the other agent as hit. |
| 0 | 164 | | HandleInterceptHit(otherAgent); |
| 0 | 165 | | otherAgent.HandleTargetIntercepted(); |
| 0 | 166 | | } else { |
| 0 | 167 | | HandleInterceptMiss(); |
| 0 | 168 | | } |
| 0 | 169 | | } |
| 0 | 170 | | } |
| | 171 | |
|
| 0 | 172 | | public override void TerminateAgent() { |
| 0 | 173 | | DetachMissileTrail(); |
| 0 | 174 | | base.TerminateAgent(); |
| 0 | 175 | | } |
| | 176 | |
|
| 0 | 177 | | public void OnDestroy() { |
| 0 | 178 | | if (_returnParticleToManagerCoroutine != null) { |
| 0 | 179 | | StopCoroutine(_returnParticleToManagerCoroutine); |
| 0 | 180 | | } |
| 0 | 181 | | if (_missileTrailEffect != null && ParticleManager.Instance != null) { |
| 0 | 182 | | ParticleManager.Instance.ReturnMissileTrailParticle(_missileTrailEffect); |
| 0 | 183 | | _missileTrailEffect = null; |
| 0 | 184 | | } |
| 0 | 185 | | } |
| | 186 | |
|
| 0 | 187 | | private void AttachMissileTrailEffect() { |
| 0 | 188 | | if (_missileTrailEffect == null) { |
| 0 | 189 | | _missileTrailEffect = ParticleManager.Instance.RequestMissileTrailParticle(); |
| 0 | 190 | | if (_missileTrailEffect != null) { |
| 0 | 191 | | _missileTrailEffect.transform.parent = transform; |
| 0 | 192 | | _missileTrailEffect.transform.localPosition = Vector3.zero; |
| 0 | 193 | | _missileTrailEffectAttached = true; |
| 0 | 194 | | ParticleSystem particleSystem = _missileTrailEffect.GetComponent<ParticleSystem>(); |
| 0 | 195 | | float duration = particleSystem.main.duration; |
| | 196 | |
|
| | 197 | | // Extend the duration of the missile trail effect to be the same as the boost time. |
| 0 | 198 | | if (duration < (staticConfig.BoostConfig?.BoostTime ?? 0)) { |
| 0 | 199 | | ParticleSystem.MainModule mainModule = particleSystem.main; |
| 0 | 200 | | mainModule.duration = staticConfig.BoostConfig?.BoostTime ?? 0; |
| 0 | 201 | | } |
| | 202 | |
|
| 0 | 203 | | _returnParticleToManagerCoroutine = StartCoroutine(ReturnParticleToManager(duration * 2f)); |
| 0 | 204 | | particleSystem.Play(); |
| 0 | 205 | | } |
| 0 | 206 | | } |
| 0 | 207 | | } |
| | 208 | |
|
| 0 | 209 | | private IEnumerator ReturnParticleToManager(float delay) { |
| 0 | 210 | | yield return new WaitForSeconds(delay); |
| 0 | 211 | | if (_missileTrailEffect != null) { |
| 0 | 212 | | ParticleManager.Instance.ReturnMissileTrailParticle(_missileTrailEffect); |
| 0 | 213 | | _missileTrailEffect = null; |
| 0 | 214 | | _missileTrailEffectAttached = false; |
| 0 | 215 | | } |
| 0 | 216 | | } |
| | 217 | |
|
| 0 | 218 | | private void UpdateMissileTrailEffect() { |
| 0 | 219 | | if (_missileTrailEffect == null || !_missileTrailEffectAttached) { |
| 0 | 220 | | return; |
| | 221 | | } |
| | 222 | |
|
| | 223 | | // Get the particle effect duration time. |
| 0 | 224 | | float duration = _missileTrailEffect.GetComponent<ParticleSystem>().main.duration; |
| 0 | 225 | | if (_timeSinceBoost > duration) { |
| 0 | 226 | | DetachMissileTrail(); |
| 0 | 227 | | } |
| 0 | 228 | | } |
| | 229 | |
|
| 0 | 230 | | private void DetachMissileTrail() { |
| 0 | 231 | | if (_missileTrailEffect != null && _missileTrailEffectAttached) { |
| 0 | 232 | | Vector3 currentPosition = _missileTrailEffect.transform.position; |
| 0 | 233 | | _missileTrailEffect.transform.SetParent(null); |
| 0 | 234 | | _missileTrailEffect.transform.position = currentPosition; |
| 0 | 235 | | _missileTrailEffectAttached = false; |
| | 236 | | // Stop emitting particles. |
| 0 | 237 | | ParticleSystem particleSystem = _missileTrailEffect.GetComponent<ParticleSystem>(); |
| 0 | 238 | | particleSystem.Stop(); |
| 0 | 239 | | } |
| 0 | 240 | | } |
| | 241 | |
|
| 0 | 242 | | protected virtual void DrawDebugVectors() { |
| 0 | 243 | | if (_target != null) { |
| | 244 | | // Line of sight. |
| 0 | 245 | | Debug.DrawLine(transform.position, _target.transform.position, new Color(1, 1, 1, 0.15f)); |
| | 246 | |
|
| | 247 | | // Velocity vector. |
| 0 | 248 | | Debug.DrawRay(transform.position, GetVelocity() * 0.01f, new Color(0, 0, 1, 0.15f)); |
| | 249 | |
|
| | 250 | | // Current forward direction. |
| 0 | 251 | | Debug.DrawRay(transform.position, transform.forward * 5f, Color.yellow); |
| | 252 | |
|
| | 253 | | // Pitch axis (right). |
| 0 | 254 | | Debug.DrawRay(transform.position, transform.right * 5f, Color.red); |
| | 255 | |
|
| | 256 | | // Yaw axis (up). |
| 0 | 257 | | Debug.DrawRay(transform.position, transform.up * 5f, Color.magenta); |
| | 258 | |
|
| 0 | 259 | | if (_accelerationInput != null) { |
| 0 | 260 | | Debug.DrawRay(transform.position, _accelerationInput * 1f, Color.green); |
| 0 | 261 | | } |
| 0 | 262 | | } |
| 0 | 263 | | } |
| | 264 | | } |