From c84fa9d70130bb44f59073aa7371f3ad5dbf4652 Mon Sep 17 00:00:00 2001 From: NomarCub Date: Thu, 12 Dec 2019 12:52:48 +0100 Subject: [PATCH] Less frequent overtake attempts minor fixes --- Assets/Scripts/Cars/CarController.cs | 24 ++++++++++++++++-------- Assets/Scripts/Cars/DistanceSensor.cs | 11 ++++------- Assets/Scripts/PhysicsCalc.cs | 2 +- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/Assets/Scripts/Cars/CarController.cs b/Assets/Scripts/Cars/CarController.cs index a377387..d525347 100644 --- a/Assets/Scripts/Cars/CarController.cs +++ b/Assets/Scripts/Cars/CarController.cs @@ -19,7 +19,6 @@ public class CarController : MonoBehaviour private void Awake() { ID = Interlocked.Increment(ref currentID); - maxVelocity = ID % 3 == 0 ? FastestCarVelocity : SlowestCarVelocity; } public float maxVelocity = 4.33f; @@ -80,6 +79,7 @@ private void Start() sensorTransform = transform.Find(Strings.distanceSensor); sensor = gameObject.GetComponentInChildren(); shortestPath = Dijkstra.Instance.CalculateShortestPath(Graph.Instance, source, destination); + maxVelocity = ID % 3 == 0 ? FastestCarVelocity : SlowestCarVelocity; speedLimit = maxVelocity; } @@ -183,7 +183,12 @@ void steerTowards(Vector3 position) internal bool TryOvertake(CarController otherCar) { - if (isInJunction || cantGo() || overtakeInfo.state != OvertakeInfo.State.None) + if (maxVelocity < otherCar.maxVelocity + 4 + || otherCar.velocity < SlowestCarVelocity * 0.5 + || isInJunction + || cantGo() + || overtakeInfo.state != OvertakeInfo.State.None + || overtakeInfo.lastTried + 0.5f > Time.fixedTime) return false; Node currentNode = shortestPath[currentNodeIndex]; @@ -205,7 +210,7 @@ internal bool TryOvertake(CarController otherCar) var rayFrom = gameObject.transform.position + leftDir * laneDistance; rayFrom.y = rayHeight; RaycastHit hit; - float rayDist = Mathf.Min(minOvertakeDist + FastestCarVelocity * (minOvertakeDist / maxVelocity), distanceToJunction); + float rayDist = minOvertakeDist + FastestCarVelocity * (minOvertakeDist / maxVelocity); Debug.DrawRay(rayFrom - leftDir * 0.1f, forwardDir * rayDist, Color.red, 0.5f, false); if (Physics.Raycast(rayFrom, forwardDir, out hit, rayDist)) { @@ -221,14 +226,15 @@ internal bool TryOvertake(CarController otherCar) overtakeInfo = new OvertakeInfo() { - state = OvertakeInfo.State.KeepLeft, - otherCar = otherCar, car = this, + otherCar = otherCar, forwardDir = forwardDir, rightDir = rightDir, leftDir = leftDir, rightLane = rightLane, - leftLane = leftLane + leftLane = leftLane, + lastTried = Time.fixedTime, + state = OvertakeInfo.State.KeepLeft }; return true; @@ -249,6 +255,8 @@ public State state } public CarController car; public CarController otherCar; + + public float lastTried = 0; public Vector3 rightLane; public Vector3 leftLane; public Vector3 forwardDir; @@ -271,7 +279,7 @@ void Overtake() + overtakeInfo.forwardDir * 6; Debug.DrawRay(transform.position, dest - transform.position, Color.blue, 0.5f); steerTowards(dest); - if (PhysicsCalc.IsBehind( + if (overtakeInfo.otherCar == null || PhysicsCalc.IsBehind( transform.position, overtakeInfo.forwardDir, overtakeInfo.otherCar.transform.position + overtakeInfo.forwardDir * 2)) @@ -288,7 +296,7 @@ void Overtake() Debug.DrawRay(transform.position, dest - transform.position, Color.blue, 0.5f); steerTowards(dest); - if (PhysicsCalc.IsToRight( + if (overtakeInfo.otherCar == null || PhysicsCalc.IsToRight( transform.position, overtakeInfo.forwardDir, overtakeInfo.leftLane + overtakeInfo.rightDir * laneDistance * 0.8f)) diff --git a/Assets/Scripts/Cars/DistanceSensor.cs b/Assets/Scripts/Cars/DistanceSensor.cs index d9a5eb3..3b67e6d 100644 --- a/Assets/Scripts/Cars/DistanceSensor.cs +++ b/Assets/Scripts/Cars/DistanceSensor.cs @@ -30,15 +30,12 @@ private void Update() { carIsInBrakingDistance = carRigidbody.velocity.magnitude > otherCar.GetComponent().velocity.magnitude || Vector3.Distance(car.transform.position, otherCar.transform.position) < 4; + var otherController = otherCar.GetComponent(); - if (carController.maxVelocity > otherController.maxVelocity + 4 - && otherController.maxVelocity > CarController.SlowestCarVelocity * 0.5) + if (carController.TryOvertake(otherController)) { - if (carController.TryOvertake(otherController)) - { - otherCar = null; - return; - } + otherCar = null; + return; } } diff --git a/Assets/Scripts/PhysicsCalc.cs b/Assets/Scripts/PhysicsCalc.cs index c10b0f7..974325b 100644 --- a/Assets/Scripts/PhysicsCalc.cs +++ b/Assets/Scripts/PhysicsCalc.cs @@ -8,7 +8,7 @@ public static float SlowDownDistance(float from, float to) return 0; float deltaV = from - to; - const float brakeDeceleration = 4.5f; + const float brakeDeceleration = 4f; float deltaT = deltaV / brakeDeceleration; return to * deltaT + deltaT * deltaV / 2; }