protected void Initialize_AI() { CachedReversePoint = transform.FindChild("_ReversePoint"); if (CachedReversePoint == null) { Debug.Log("Reverse Point Missing!"); } if (myTransform == null) { myTransform = transform.GetComponent <Transform>(); if (myTransform == null) { myTransform = transform.parent.GetComponent <Transform>(); } } AICurrentRoadPosition = eAIRoadPosition.UnKnown; DedectionFrequencyOriginal = DedectionFrequency; }
protected void Initialize_AI() { if (myTransform == null) { myTransform = transform.GetComponent <Transform>(); if (myTransform == null) { myTransform = transform.parent.GetComponent <Transform>(); } } AICurrentRoadPosition = eAIRoadPosition.UnKnown; AIOldRoadPosition = eAIRoadPosition.UnKnown; DedectionFrequencyOriginal = DedectionFrequency; ForwardSensorDistanceOriginal = ForwardSensorDistance; if (RandomizeBehaviors) { AILevelBehavior = (eAIProLevel)UnityEngine.Random.Range(0, 2); AIStateBehavior = (eAIBehavior)UnityEngine.Random.Range(0, 2); } BuildAICoefficiencies(); CachedDistancePoint = transform.FindChild("_DistancePoint"); CachedReversePoint = transform.FindChild("_ReversePoint"); if (CachedDistancePoint == null) { Debug.Log(RGKMessages.DistanceCheckerMissing); } if (CachedReversePoint == null) { Debug.Log("Reverse Point Missing!"); } }
/// <summary> /// Determinates which waypoint is next /// </summary> private void CalculateRoute() { if (AIWillAvoid) { WillReCalculate = true; } WayPointItem oWPItem; if (WillReCalculate) { oWPItem = waypoints[currentWaypoint].GetComponent <WayPointItem>() as WayPointItem; SoftBrakeSpeed = oWPItem.SoftBrakeSpeed; HardBrakeSpeed = oWPItem.HardBrakeSpeed; float LeftWideOfWP = oWPItem.LeftWide; float RightWideOfWP = oWPItem.RightWide; IsBraking = false; ProbableWaypointPosition = new Vector3(waypoints[currentWaypoint].position.x, transform.position.y, waypoints[currentWaypoint].position.z); RelativeWaypointPosition = transform.InverseTransformPoint(ProbableWaypointPosition); ////Calculate Escape Points //NextWPDis = Vector3.Distance(transform.position, ProbableWaypointPosition); //EscapeDistance = Mathf.Sin(RelativeWaypointPosition.x * Mathf.Deg2Rad) * NextWPDis; //EscapeDistance = ThinkNewEscapePoint(LeftWideOfWP, RightWideOfWP, currentWaypoint, EscapeDistance, RelativeWaypointPosition.x, AIOldRoadPosition); WillReCalculate = false; } ProbableWaypointPositionNew = new Vector3(waypoints[currentWaypoint].position.x, transform.position.y, waypoints[currentWaypoint].position.z + UnityEngine.Random.Range(0.3f, 2)); RelativeWaypointPositionNew = transform.InverseTransformPoint(ProbableWaypointPositionNew); if (UseSteerSmoothing && UseLerp) { if (Mathf.Abs(RelativeWaypointPositionNew.x / RelativeWaypointPositionNew.magnitude) < SteerSmoothingReleaseCoef) { RGKSteerFirst = (RelativeWaypointPositionNew.x / RelativeWaypointPositionNew.magnitude); } else { float RGKSteerLast = (RelativeWaypointPositionNew.x / RelativeWaypointPositionNew.magnitude); if ((RGKSteerLast < 0 && RGKSteerFirst > 0) || (RGKSteerFirst < 0 && RGKSteerLast > 0)) { RGKSteerFirst = 0; } RGKSteerFirst = Mathf.Lerp(RGKSteerFirst, RGKSteerLast, Time.deltaTime * UnityEngine.Random.Range(SteerSmoothingLow / 10, SteerSmoothingHigh / 10)); } } else if (UseSteerSmoothing && !UseLerp) { if (Mathf.Abs(RelativeWaypointPositionNew.x / RelativeWaypointPositionNew.magnitude) < SteerSmoothingReleaseCoef) { RGKSteerFirst = (RelativeWaypointPositionNew.x / RelativeWaypointPositionNew.magnitude) * Time.deltaTime * UnityEngine.Random.Range(SteerSmoothingLow, SteerSmoothingHigh); } else { RGKSteerFirst = (RelativeWaypointPositionNew.x / RelativeWaypointPositionNew.magnitude); } } else { RGKSteerFirst = (RelativeWaypointPositionNew.x / RelativeWaypointPositionNew.magnitude); } if (RelativeWaypointPositionNew.z < 0 && !ReverseLockedbyObstacle) { AIReversing = true; IsReversing = AIReversing; } if (AIReversing) { if (!isSteeringLocked) { if (RGKSteer > 0) { RGKSteerCorrectionFactor = 1; } else { RGKSteerCorrectionFactor = -1; } if (ReverseTimeRoadPosition == eAIRoadPosition.Right) { RGKSteerCorrectionFactor *= -1; } isSteeringLocked = true; } RGKSteer = RGKSteerCorrectionFactor; if (RelativeWaypointPositionNew.z > 25) { RGKThrottle = 0; isSteeringLocked = false; AIReversing = false; IsReversing = AIReversing; } } else { ReverseTimeRoadPosition = AICurrentRoadPosition; RGKSteer = (RGKSteerFirst + SteerFactor); AIReversing = false; } if (RelativeWaypointPositionNew.z > 25) { ReverseLockedbyObstacle = false; //recover from reverselock } if (Mathf.Abs(RGKSteer) < 1.5f) { if (!AIReversing) { if (UseThrottleBonus) { float thrBonus = ThrottleBonus; if (RandomizeBonusUsage) { thrBonus = UnityEngine.Random.Range(1f, ThrottleBonus); } RGKThrottle = (RelativeWaypointPositionNew.z / RelativeWaypointPositionNew.magnitude - Mathf.Abs(RGKSteer) / 2) * thrBonus; } else { RGKThrottle = (RelativeWaypointPositionNew.z / RelativeWaypointPositionNew.magnitude - Mathf.Abs(RGKSteer) / 2); } } else { RGKThrottle = -1; } if (SoftBrakeSpeed > 0) { if (CarSpeedKm > SoftBrakeSpeed && CarSpeedKm < HardBrakeSpeed) { RGKThrottle = AISoftBrakeFactor; IsBraking = true; } else if (CarSpeedKm > HardBrakeSpeed) { RGKThrottle = AIHardBrakeFactor; IsBraking = true; } } } else { RGKThrottle = 0.0f; } if (ReverseLockedbyObstacle) { if (RGKSteer > 0) { RGKSteer = 1; } else { RGKSteer = -1; } isSteeringLocked = true; RGKThrottle = 1; } float innerNextWPCoef = UnityEngine.Random.Range(nextWPCoef, nextWPCoef + 3); if (RelativeWaypointPositionNew.magnitude <= innerNextWPCoef) { currentWaypoint++; if (currentWaypoint >= waypoints.Count) { currentWaypoint = 0; } WillReCalculate = true; } if (ShowNavDebugLines) { Debug.DrawLine(base.transform.position, ProbableWaypointPosition, Color.cyan); //Debug.DrawLine(base.transform.position, ProbableWaypointPositionNew, Color.yellow); } }
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); }
protected float ThinkNewEscapePoint(float WideLeft, float WideRight, int CurrentWaypointIndex, float CalculatedEscapeDistance, float CurrentRelativePisiton, eAIRoadPosition AIRoadPosition) { NewEscape = 0; float Wide = 0; CalcEscape = CalculatedEscapeDistance; //Determinate where we are to current waypoint if (CalculatedEscapeDistance > 0) //we're at left { AICurrentRoadPosition = eAIRoadPosition.Left; Wide = WideLeft; } else if (CalculatedEscapeDistance < 0) //we're at Right { AICurrentRoadPosition = eAIRoadPosition.Right; Wide = WideRight; } //Cases if (AIWillAvoid && !AIWillEscape) //Avoiding side and front collisions based steering { if (AIRoadPosition == eAIRoadPosition.Left && AIAvoidingRivalRoadPosition == eAIRoadPosition.Left) { if (AIAvoidingRivalAvoidPosition == eAIRivalPosition.Left) { NewEscape = UnityEngine.Random.Range(0.1f, Wide / 2); } else { NewEscape = UnityEngine.Random.Range(Wide / 2, Wide); } } else if (AIRoadPosition == eAIRoadPosition.Right && AIAvoidingRivalRoadPosition == eAIRoadPosition.Right) { if (AIAvoidingRivalAvoidPosition == eAIRivalPosition.Left) { NewEscape = UnityEngine.Random.Range(Wide / 2, Wide); } else { NewEscape = UnityEngine.Random.Range(0.1f, Wide / 2); } } else { if (AIAvoidingRivalAvoidPosition == eAIRivalPosition.Left) { NewEscape = UnityEngine.Random.Range(Wide / 2, Wide); } else { NewEscape = UnityEngine.Random.Range(0.1f, Wide / 2); } } WriteToConsole("AVOIDING!"); } else if (AIWillAvoid && AIWillEscape) //Faster car and avoiding collision into front car { WriteToConsole("I NEED TO ESCAPE!"); UseSideCorrection = false; if (AIRoadPosition == eAIRoadPosition.Left) { if (RightSensorOK) { NewEscape = UnityEngine.Random.Range(0.2f, WideRight); WriteToConsole("LEFT - ESCAPING RIGHT"); } else if (LeftSensorOK) { NewEscape = UnityEngine.Random.Range(WideLeft - 1, WideLeft); WriteToConsole("LEFT - ESCAPING LEFT"); } } else { if (RightSensorOK) { NewEscape = UnityEngine.Random.Range(0.2f, WideLeft); WriteToConsole("RIGHT - ESCAPING LEFT"); } else if (LeftSensorOK) { NewEscape = UnityEngine.Random.Range(WideRight - 1, WideRight); WriteToConsole("RIGHT - ESCAPING RIGHT"); } } AIWillEscape = false; AIWillAvoid = false; } else //No avoid or no drafting, so continue to best course { UseSideCorrection = true; if (Wide <= Mathf.Abs(AIEscapeFactor)) { NewEscape = UnityEngine.Random.Range(0.2f, Wide); WriteToConsole("NO ESCAPE - WIDE CALC"); } else { NewEscape = UnityEngine.Random.Range(0.5f, AIEscapeFactor); WriteToConsole("NO ESCAPE - ESCAPE FACTOR"); } //if (AICurrentRoadPosition == eAIRoadPosition.Right && NewEscape < 0) //{ // NewEscape *= -1; // WriteToConsole("RIGHht - NO ESCAPE - NEW ESC<0"); //} //else if (AICurrentRoadPosition == eAIRoadPosition.Left && NewEscape > 0) //{ // NewEscape *= -1; // WriteToConsole("RIGHht - NO ESCAPE - NEW ESC>0"); //} } if (CurrentWaypointIndex == 0) //Game Start so we've to find where're we { NewEscape = CalculatedEscapeDistance; if (Mathf.Abs(CalculatedEscapeDistance) > Wide) { NewEscape = UnityEngine.Random.Range(0.2f, Wide); } AIOldRoadPosition = AICurrentRoadPosition; } else { if (Mathf.Abs(CurrentRelativePisiton) < 35 || !AIWillAvoid && !AIWillEscape) { if (AICurrentRoadPosition != AIOldRoadPosition) { //Stay at right as possible if (AIOldRoadPosition == eAIRoadPosition.Right && AICurrentRoadPosition == eAIRoadPosition.Left) { AICurrentRoadPosition = eAIRoadPosition.Right; WriteToConsole("STAYING RIGHT"); } //Stay at left as possible if (AIOldRoadPosition == eAIRoadPosition.Left && AICurrentRoadPosition == eAIRoadPosition.Right) { AICurrentRoadPosition = eAIRoadPosition.Left; WriteToConsole("STAYING LEFT"); } if (AICurrentRoadPosition == eAIRoadPosition.Left) { AIOldRoadPosition = eAIRoadPosition.Left; } if (AICurrentRoadPosition == eAIRoadPosition.Right) { AIOldRoadPosition = eAIRoadPosition.Right; } } } else { AIOldRoadPosition = AICurrentRoadPosition; } } if (UseSideCorrection) { if (AICurrentRoadPosition == eAIRoadPosition.Right && NewEscape > 0) { NewEscape *= -1; } if (AICurrentRoadPosition == eAIRoadPosition.Left && NewEscape < 0) { NewEscape *= -1; } } return(NewEscape); }
/// <summary> /// Determinates which waypoint is next /// </summary> private void CalculateRoute() { //if (AIWillAvoid) WillReCalculate = true; WayPointItem oWPItem; IsBraking = false; if (WillReCalculate) { oWPItem = waypoints[currentWaypoint].GetComponent <WayPointItem>() as WayPointItem; SoftBrakeSpeed = oWPItem.SoftBrakeSpeed; HardBrakeSpeed = oWPItem.HardBrakeSpeed; float LeftWideOfWP = oWPItem.LeftWide; float RightWideOfWP = oWPItem.RightWide; ProbableWaypointPosition = new Vector3(waypoints[currentWaypoint].position.x, transform.position.y, waypoints[currentWaypoint].position.z); RelativeWaypointPosition = transform.InverseTransformPoint(ProbableWaypointPosition); //Calculate Escape Points NextWPDis = Vector3.Distance(transform.position, ProbableWaypointPosition); EscapeDistance = Mathf.Sin(RelativeWaypointPosition.x * Mathf.Deg2Rad) * NextWPDis; EscapeDistance = ThinkNewEscapePoint(LeftWideOfWP, RightWideOfWP, currentWaypoint, EscapeDistance, RelativeWaypointPosition.x, AIOldRoadPosition); WillReCalculate = false; } ProbableWaypointPositionNew = new Vector3(waypoints[currentWaypoint].position.x, transform.position.y, waypoints[currentWaypoint].position.z + EscapeDistance); RelativeWaypointPositionNew = transform.InverseTransformPoint(ProbableWaypointPositionNew); if (!AIWillEscape) { if (UseSteerSmoothing && UseLerp) { if (Mathf.Abs(RelativeWaypointPositionNew.x / RelativeWaypointPositionNew.magnitude) < SteerSmoothingReleaseCoef) { RGKSteerFirst = (RelativeWaypointPositionNew.x / RelativeWaypointPositionNew.magnitude); } else { if (SteerSmoothingMode == eSteerSmoothingMode.Basic) { float RGKSteerLast = (RelativeWaypointPositionNew.x / RelativeWaypointPositionNew.magnitude);// *Time.deltaTime * UnityEngine.Random.Range(SteerSmoothingLow, SteerSmoothingHigh); //*2 if ((RGKSteerLast < 0 && RGKSteerFirst > 0) || (RGKSteerFirst < 0 && RGKSteerLast > 0)) { RGKSteerFirst = 0; } RGKSteerFirst = Mathf.Lerp(RGKSteerFirst, RGKSteerLast, Time.deltaTime * UnityEngine.Random.Range(SteerSmoothingLow / 10, SteerSmoothingHigh / 10)); } else { float currentFPS = RaceManager.WorkingFPS; if (currentFPS <= 30) { RGKSteerFirst = (RelativeWaypointPositionNew.x / RelativeWaypointPositionNew.magnitude) * Time.deltaTime * UnityEngine.Random.Range(FPSBasedSmoothingdata.m_0_30FPS.Min, FPSBasedSmoothingdata.m_0_30FPS.Max); } else if (currentFPS > 30 && currentFPS <= 45) { RGKSteerFirst = (RelativeWaypointPositionNew.x / RelativeWaypointPositionNew.magnitude) * Time.deltaTime * UnityEngine.Random.Range(FPSBasedSmoothingdata.m_30_45FPS.Min, FPSBasedSmoothingdata.m_30_45FPS.Max); } else if (currentFPS > 45 && currentFPS <= 60) { RGKSteerFirst = (RelativeWaypointPositionNew.x / RelativeWaypointPositionNew.magnitude) * Time.deltaTime * UnityEngine.Random.Range(FPSBasedSmoothingdata.m_45_60FPS.Min, FPSBasedSmoothingdata.m_45_60FPS.Max); } else if (currentFPS > 60) { RGKSteerFirst = (RelativeWaypointPositionNew.x / RelativeWaypointPositionNew.magnitude) * Time.deltaTime * UnityEngine.Random.Range(FPSBasedSmoothingdata.m_60_NFPS.Min, FPSBasedSmoothingdata.m_60_NFPS.Max); } } } } else if (UseSteerSmoothing && !UseLerp) { if (Mathf.Abs(RelativeWaypointPositionNew.x / RelativeWaypointPositionNew.magnitude) < SteerSmoothingReleaseCoef) { RGKSteerFirst = (RelativeWaypointPositionNew.x / RelativeWaypointPositionNew.magnitude) * Time.deltaTime * UnityEngine.Random.Range(SteerSmoothingLow, SteerSmoothingHigh); } else { RGKSteerFirst = (RelativeWaypointPositionNew.x / RelativeWaypointPositionNew.magnitude); } } else { RGKSteerFirst = (RelativeWaypointPositionNew.x / RelativeWaypointPositionNew.magnitude); } } else { //Mor sharp turns when tries escape RGKSteerFirst = (RelativeWaypointPositionNew.x / RelativeWaypointPositionNew.magnitude); } if (RelativeWaypointPositionNew.z < 0 && !ReverseLockedbyObstacle) { AIReversing = true; IsReversing = AIReversing; } if (AIReversing) { if (!isSteeringLocked) { if (RGKSteer > 0) { RGKSteerCorrectionFactor = 1; } else { RGKSteerCorrectionFactor = -1; } if (ReverseTimeRoadPosition == eAIRoadPosition.Right) { RGKSteerCorrectionFactor *= -1; } isSteeringLocked = true; } RGKSteer = RGKSteerCorrectionFactor; if (RelativeWaypointPositionNew.z > 25) { RGKThrottle = 0; isSteeringLocked = false; AIReversing = false; IsReversing = AIReversing; } } else { ReverseTimeRoadPosition = AICurrentRoadPosition; RGKSteer = (RGKSteerFirst + SteerFactor); AIReversing = false; } if (RelativeWaypointPositionNew.z > 25) { ReverseLockedbyObstacle = false; //recover from reverselock } if (Mathf.Abs(RGKSteer) < 1.5f) { if (!AIReversing) { RGKThrottle = RelativeWaypointPositionNew.z / RelativeWaypointPositionNew.magnitude - Mathf.Abs(RGKSteer) / 2 * AISpeedFactor; } else { RGKThrottle = -1; } if (SoftBrakeSpeed > 0) { if (CarSpeedKm > SoftBrakeSpeed && CarSpeedKm < HardBrakeSpeed) { RGKThrottle *= AISoftBrakeFactor; IsBraking = true; } else if (CarSpeedKm > HardBrakeSpeed) { RGKThrottle *= AIHardBrakeFactor; IsBraking = true; } } if (AISoftBraking) { RGKThrottle *= AISoftBrakeFactor; IsBraking = true; } else if (AIHardBraking) { RGKThrottle *= AIHardBrakeFactor; IsBraking = true; } } else { RGKThrottle = 0.0f; } if (ReverseLockedbyObstacle) { if (RGKSteer > 0) { RGKSteer = 1; } else { RGKSteer = -1; } isSteeringLocked = true; RGKThrottle = 1; } float innerNextWPCoef = UnityEngine.Random.Range(nextWPCoef, nextWPCoef + 3); if (RelativeWaypointPositionNew.magnitude < innerNextWPCoef) { currentWaypoint++; if (currentWaypoint >= waypoints.Count) { currentWaypoint = 0; } WillReCalculate = true; } if (ShowNavDebugLines) { Debug.DrawLine(base.transform.position, ProbableWaypointPosition, Color.cyan); Debug.DrawLine(base.transform.position, ProbableWaypointPositionNew, Color.yellow); } }