Skip to content

Commit

Permalink
Prevent threats from hitting the ground
Browse files Browse the repository at this point in the history
  • Loading branch information
tryuan99 committed Oct 17, 2024
1 parent 3c27f39 commit 73b846c
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Assets/Scripts/Threats/FixedWingThreat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,27 @@ private Vector3 CalculateAccelerationInput(SensorOutput sensorOutput) {

// Clamp the normal acceleration input to the maximum normal acceleration
float maxNormalAcceleration = CalculateMaxNormalAcceleration();
accelerationInput = Vector3.ClampMagnitude(accelerationInput, maxNormalAcceleration);

// Avoid the ground when close to the surface and too low on the glideslope
float altitude = GetPosition().y;
float sinkRate = -GetVelocity().y; // Sink rate is opposite to climb rate
float distanceToTarget = sensorOutput.position.range;
float groundProximityThreshold =
Mathf.Abs(GetVelocity().y) * 5f; // Adjust threshold as necessary
if (sinkRate > 0 && altitude / sinkRate < distanceToTarget / GetSpeed()) {
// Evade upward normal to the velocity
Vector3 upwardsDirection = Vector3.Cross(transform.forward, transform.right);

// Blend between the calculated acceleration input and the upward acceleration
float blendFactor = 1 - (altitude / groundProximityThreshold);
accelerationInput.y =
Vector3
.Lerp(accelerationInput, upwardsDirection * CalculateMaxNormalAcceleration(),
blendFactor)
.y;
}

accelerationInput = Vector3.ClampMagnitude(accelerationInput, maxNormalAcceleration);
_accelerationInput = accelerationInput;
return accelerationInput;
Expand Down

0 comments on commit 73b846c

Please sign in to comment.