private void DecideObsticleAvoid(bool CanAvoidToLeft, bool CaAvoidToRight, eAIRivalPosition AvoidingRivalPosition) { bool ShouldIBrake = false; bool ShouldISteerLeft = false; bool ShouldISteerRight = false; if (ObstacleAvoidToLeft) { if (AvoidingRivalPosition == eAIRivalPosition.Left) { ShouldISteerLeft = false; } else { ShouldISteerLeft = true; } } if (ObstacleAvoidToRight) { if (AvoidingRivalPosition == eAIRivalPosition.Right) { ShouldISteerRight = false; } else { ShouldISteerRight = true; } } if (ShouldISteerRight && ShouldISteerLeft) { if (AICurrentRoadPosition == eAIRoadPosition.Left) { SteerFactor = ObstacleAvoidFactor; } else if (AICurrentRoadPosition == eAIRoadPosition.Right) { SteerFactor -= ObstacleAvoidFactor; } } else if (ShouldISteerLeft && !ShouldISteerRight) { SteerFactor -= ObstacleAvoidFactor; } else if (ShouldISteerRight && !ShouldISteerLeft) { SteerFactor += ObstacleAvoidFactor; } else { ShouldIBrake = true; } if (ShouldIBrake) { AIHardBraking = true; } }
protected float CalculateSteerFactor() { Steer = 0; IRGKRacer threat = null; float minTime = CollisionAvoidTime; Vector3 threatPositionAtNearestApproach = Vector3.zero; Vector3 ourPositionAtNearestApproach = Vector3.zero; foreach (var other in ThreatRivals) { if (other != this) { float collisionDangerThreshold = this.DedectionRadius; float time = PredictNearestApproachTime(other); if ((time >= 0) && (time < minTime)) { Vector3 ourPos = Vector3.zero; Vector3 hisPos = Vector3.zero; float dist = ComputeNearestApproachPositions(other, time, ref ourPos, ref hisPos); if (dist < collisionDangerThreshold) { minTime = time; threat = other; threatPositionAtNearestApproach = hisPos; ourPositionAtNearestApproach = ourPos; } } } } if (threat != null) { float parallelness = Vector3.Dot(transform.forward, threat.CachedTransform.forward); if (parallelness < -CollisionAvoidAngleCos) { Vector3 offset = threatPositionAtNearestApproach - this.Position; float sideDot = Vector3.Dot(offset, transform.right); Steer = (sideDot > 0) ? -CollisionAvoidFactor : CollisionAvoidFactor; } else if (parallelness > CollisionAvoidAngleCos) { Vector3 offset = threat.Position - this.Position; float sideDot = Vector3.Dot(offset, transform.right); Steer = (sideDot > 0) ? -CollisionAvoidFactor : CollisionAvoidFactor; AIAvoidingRivalAvoidPosition = (sideDot > 0) ? eAIRivalPosition.Right : eAIRivalPosition.Left; AIAvoidingRivalRoadPosition = threat.CurrentRoadPosition; } else { if (this.Speed < threat.Speed || threat.Speed == 0 || gameObject.GetInstanceID() < threat.CachedGameObject.GetInstanceID()) { float sideDot = Vector3.Dot(transform.right, threat.Velocity); Steer = (sideDot > 0) ? (-1 * 1) : 1; } } } else { AIAvoidingRivalAvoidPosition = eAIRivalPosition.NoRival; } return(Steer); }