In the script below, the car sometimes will brake too fast after overlapping a waypoint. It will reverse and go back to the same waypoint. Fix this by making the car to continue folowing the subsequent waypoints if the player is not there: using UnityEngine;
public class CarController : MonoBehaviour
{
public float maxSpeed = 7f;
public float motorTorque = 150f;
public float brakeTorque = 3000f;
public float maxSteeringAngle = 35f;
public WheelCollider frontLeftWheel;
public WheelCollider frontRightWheel;
public WheelCollider rearLeftWheel;
public WheelCollider rearRightWheel;
public Transform target;
public WaypointManager waypointManager;
public int currentWaypointIndex = 0;
public float coneRadius = 5f;
public float coneAngle = 45f;
public float followDistance = 5f;
public bool debugDrawCone = false;
private Rigidbody rb;
private bool withinCone = false;
public float rotationSpeed = 5f;
void Start()
{
rb = GetComponent();
waypointManager = FindObjectOfType();
}
void Update()
{
CheckWithinCone();
if (withinCone)
{
FollowTarget();
}
else
{
FollowWaypoints();
}
}
void CheckWithinCone()
{
Vector3 directionToTarget = target.position - transform.position;
float distanceToTarget = directionToTarget.magnitude;
if (distanceToTarget < coneRadius)
{
float angleToTarget = Vector3.Angle(transform.forward, directionToTarget);
if (angleToTarget < coneAngle)
{
withinCone = true;
}
else
{
withinCone = false;
}
}
else
{
withinCone = false;
}
}
void FollowTarget()
{
Vector3 directionToTarget = target.position - transform.position;
float distanceToTarget = directionToTarget.magnitude;
if (distanceToTarget < followDistance)
{
frontLeftWheel.brakeTorque = brakeTorque * 0.5f;
frontRightWheel.brakeTorque = brakeTorque * 0.5f;
rearLeftWheel.motorTorque = 0f;
rearRightWheel.motorTorque = 0f;
}
else
{
frontLeftWheel.brakeTorque = 0f;
frontRightWheel.brakeTorque = 0f;
// adjust steering based on target position
frontLeftWheel.steerAngle = Mathf.Clamp(Vector3.SignedAngle(transform.forward, directionToTarget, Vector3.up), -maxSteeringAngle, maxSteeringAngle);
frontRightWheel.steerAngle = frontLeftWheel.steerAngle;
// adjust motor torque based on speed and acceleration
float acceleration = (maxSpeed - rb.velocity.magnitude) / rearLeftWheel.radius;
rearLeftWheel.motorTorque = Mathf.Clamp(motorTorque, 0f, acceleration);
rearRightWheel.motorTorque = Mathf.Clamp(motorTorque, 0f, acceleration);
}
transform.LookAt(target);
}
void FollowWaypoints()
{
// Get the current waypoint
Transform currentWaypoint = waypointManager.waypoints[currentWaypointIndex];
// Check if the car has reached the current waypoint
float distanceToWaypoint = Vector3.Distance(transform.position, currentWaypoint.position);
if (distanceToWaypoint < 1f)
{
// Increment the currentWaypointIndex to move on to the next waypoint
currentWaypointIndex++;
if (currentWaypointIndex >= waypointManager.waypoints.Length)
{
currentWaypointIndex = 0;
}
}
// Move towards the current waypoint
Vector3 directionToWaypoint = currentWaypoint.position - transform.position;
Quaternion targetRotation = Quaternion.LookRotation(directionToWaypoint);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * rotationSpeed);
// Apply torque to the rear wheels to move the car forward
rearLeftWheel.motorTorque = motorTorque;
rearRightWheel.motorTorque = motorTorque;
}
void OnDrawGizmosSelected()
{
if (debugDrawCone)
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, coneRadius);
Gizmos.DrawLine(transform.position, transform.position + Quaternion.Euler(0, coneAngle, 0) * transform.forward * coneRadius);
Gizmos.DrawLine(transform.position, transform.position + Quaternion.Euler(0, -coneAngle, 0) * transform.forward * coneRadius);
}
}
}