Ai car fly's out of corners

hello

i have this code for my ai car:

using UnityEngine;
using System.Collections;

public class aiscript : MonoBehaviour {
   
    public Vector3 centerOfMass;
    private Transform[] path;
    public Transform pathGroup;
    public float maxSteer = 15.0f;
    public WheelCollider wheelFL;
    public WheelCollider wheelFR;
    public WheelCollider wheelRL;
    public WheelCollider wheelRR;
    public Transform WheelFLTrans;
    public Transform WheelFRTrans;
    public Transform WheelRLTrans;
    public Transform WheelRRTrans;

    public int currentPathObj;
    public float distFromPath = 20;
    public float maxTorque = 50;
    public float currentSpeed;
    public float topSpeed = 1000;
    public float decellarationSpeed = 10;
    public Renderer breakingMesh;
    public Material idleBreakLight;
    public Material activeBreakLight;
    public bool  isBreaking;
    public bool  inSector;
    public float sensorLength = 5;
    public float frontSensorStartPoint = 5;
    public float frontSensorSideDist = 5;
    public float frontSensorsAngle = 30;
    public float sidewaySensorLength = 5;
    public float avoidSpeed = 10;
    private int flag = 0;
    public bool  reversing = false;
    private float reverCounter = 0.0f;
    public float waitToReverse = 2.0f;
    public float reverFor = 1.5f;
    public float respawnWait = 5;
    private float respawnCounter = 0.0f;
   
    void  Start (){
        rigidbody.centerOfMass = new Vector3 (0, -1f, 0);
        GetPath();
    }
   
    void  GetPath (){
        Transform[] path_objs = pathGroup.GetComponentsInChildren<Transform>();
        path = new Transform[path_objs.Length - 1];
       
        int i = 0;
       
        foreach(Transform path_obj in path_objs){
            if (path_obj != pathGroup)
            {
                path [i] = path_obj;
                i++;
            }
        }
    }
   
   
    void  Update (){
        WheelFLTrans.Rotate(wheelFL.rpm / 60 * 360 * Time.deltaTime,0,0);
        WheelFRTrans.Rotate(wheelFR.rpm / 60 * 360 * Time.deltaTime, 0, 0);
        WheelRRTrans.Rotate(wheelRR.rpm / 60 * 360 * Time.deltaTime, 0, 0);
        WheelRLTrans.Rotate(wheelRL.rpm / 60 * 360 * Time.deltaTime, 0, 0);

   

        if (flag == 0)
            GetSteer();
        Move();
        BreakingEffect ();
        Sensors();
        Respawn ();
    }
   
    void  GetSteer (){
        Vector3 steerVector = transform.InverseTransformPoint(new Vector3(path[currentPathObj].position.x,transform.position.y,path[currentPathObj].position.z));
        float newSteer = maxSteer * (steerVector.x / steerVector.magnitude);
        wheelFL.steerAngle = newSteer;
        wheelFR.steerAngle = newSteer;
        Debug.Log (newSteer);
        if (steerVector.magnitude <= distFromPath){
            currentPathObj++;
            if (currentPathObj >= path.Length)
                currentPathObj = 0;
        }
       
    }
   
    void  Move (){
        currentSpeed = 2*(22/7)*wheelRL.radius*wheelRL.rpm * 60 / 1000;
        currentSpeed = Mathf.Round (currentSpeed);
        if (currentSpeed <= topSpeed && !inSector){
            if (!reversing){
                wheelRL.motorTorque = maxTorque;
                wheelRR.motorTorque = maxTorque;
            }
            else {
                wheelRL.motorTorque = -maxTorque;
                wheelRR.motorTorque = -maxTorque;
            }
            wheelRL.brakeTorque = 0;
            wheelRR.brakeTorque = 0;
        }
        else if (!inSector){
            wheelRL.motorTorque = 0;
            wheelRR.motorTorque = 0;
            wheelRL.brakeTorque = decellarationSpeed;
            wheelRR.brakeTorque = decellarationSpeed;
        }
    }
   
    void  BreakingEffect (){
        if (isBreaking && breakingMesh){
            breakingMesh.material = activeBreakLight;
        }
        else if (breakingMesh) {
            breakingMesh.material = idleBreakLight;
        }
       
    }
   
    void  Sensors (){
        flag = 0;
        float avoidSenstivity = 0;
        Vector3 pos;
        RaycastHit hit;
        Vector3 rightAngle = Quaternion.AngleAxis(frontSensorsAngle,transform.up) * transform.forward;
        Vector3 leftAngle = Quaternion.AngleAxis(-frontSensorsAngle,transform.up) * transform.forward;
       
       
       
        pos = transform.position;
        pos += transform.forward*frontSensorStartPoint;
       
        //BRAKING SENSOR
       
        if (Physics.Raycast(pos,transform.forward,out hit,sensorLength)){
            if (hit.transform.tag != "Terrain"){
                flag++;
                wheelRL.brakeTorque = decellarationSpeed;
                wheelRR.brakeTorque = decellarationSpeed;
                Debug.DrawLine(pos,hit.point,Color.red);
            }
        }
        else {
            wheelRL.brakeTorque = 0;
            wheelRR.brakeTorque = 0;
        }
       
       
        //Front Straight Right Sensor
        pos += transform.right*frontSensorSideDist;
       
        if (Physics.Raycast(pos,transform.forward,out hit,sensorLength)){
            if (hit.transform.tag != "Terrain"){
                flag++;
                avoidSenstivity -= 1;
                Debug.Log("Avoiding");
                Debug.DrawLine(pos,hit.point,Color.white);
            }
        }
        else if (Physics.Raycast(pos,rightAngle,out hit,sensorLength)){
            if (hit.transform.tag != "Terrain"){
                avoidSenstivity -= 0.5f;
                flag++;
                Debug.DrawLine(pos,hit.point,Color.white);
            }
        }
       
       
        //Front Straight left Sensor
        pos = transform.position;
        pos += transform.forward*frontSensorStartPoint;
        pos -= transform.right*frontSensorSideDist;
       
        if (Physics.Raycast(pos,transform.forward,out hit,sensorLength)){
            if (hit.transform.tag != "Terrain"){
                flag++;
                avoidSenstivity += 1;
                Debug.Log("Avoiding");
                Debug.DrawLine(pos,hit.point,Color.white);
            }
        }
        else if (Physics.Raycast(pos,leftAngle,out hit,sensorLength)){
            if (hit.transform.tag != "Terrain"){
                flag++;
                avoidSenstivity += 0.5f;
                Debug.DrawLine(pos,hit.point,Color.white);
            }
        }
       
        //Right SideWay Sensor
        if (Physics.Raycast(transform.position,transform.right,out hit,sidewaySensorLength)){
            if (hit.transform.tag != "Terrain"){
                flag++;
                avoidSenstivity -= 0.5f;
                Debug.DrawLine(transform.position,hit.point,Color.white);
            }
        }
       
       
        //Left SideWay Sensor
        if (Physics.Raycast(transform.position,-transform.right,out hit,sidewaySensorLength)){
            if (hit.transform.tag != "Terrain"){
                flag++;
                avoidSenstivity += 0.5f;
                Debug.DrawLine(transform.position,hit.point,Color.white);
            }
        }
       
        pos = transform.position;
        pos += transform.forward*frontSensorStartPoint;
        //Front Mid Sensor
        if (avoidSenstivity == 0){
           
            if (Physics.Raycast(pos,transform.forward,out hit,sensorLength)){
                if (hit.transform.tag != "Terrain"){
                    if (hit.normal.x < 0 )
                        avoidSenstivity = -1;
                    else
                        avoidSenstivity = 1;
                    Debug.DrawLine(pos,hit.point,Color.white);
                }
            }
        }
       
       
        if (rigidbody.velocity.magnitude < 2 && !reversing){
            reverCounter += Time.deltaTime;
            if (reverCounter >= waitToReverse){
                reverCounter = 0;
                reversing = true;
            }
        }
        else if (!reversing){
            reverCounter = 0;
        }
       
       
        if (reversing){
            avoidSenstivity *= -1;
            reverCounter += Time.deltaTime;
            if (reverCounter >= reverFor){
                reverCounter = 0;
                reversing = false;
            }
        }
       
       
        if (flag != 0)
            AvoidSteer (avoidSenstivity);
       
       
    }
   
   
    void  AvoidSteer ( float senstivity  ){
        wheelFL.steerAngle = avoidSpeed*senstivity;
        wheelFR.steerAngle = avoidSpeed*senstivity;
       
    }
   
   
    void  Respawn (){
        if (rigidbody.velocity.magnitude < 2){
            respawnCounter += Time.deltaTime;
            if (respawnCounter >= respawnWait){
                if (currentPathObj == 0){
                    transform.position = path[path.Length-1].position;
                }
                else{
                    transform.position = path[currentPathObj-1].position;
                }
                respawnCounter = 0;
                transform.localEulerAngles.Set (transform.localEulerAngles.x, 0, transform.localEulerAngles.z);
            }
        }
    }
}

but when i go to a sharp turn my car isnt slowing down so it fly’s out of the corner
So my idea was that i putt a box collider or something in front of the corner and when he sees that it breaks
do you guys have any idea how to do that?
or have another solution

hope you can help

Driving a car with AI is not a trivial task. As far as I know the most common approach is to set waypoints through the road and set brake and throttle values for each waypoint. This way, the AI knows how much to brake. My real advice would be to search for steering algorithms, choose one that fits for your needs and improve on it.

ok thanks for responding

right now i addes this code

        if (Physics.Raycast(pos,transform.forward,out hit,sensorLength)){
            if (hit.transform.tag == "brake"){
                flag++;
wheelRL.motorTorque= 0;
                wheelRR.motorTorque= 0;
                wheelRL.brakeTorque = 20;
                wheelRR.brakeTorque = 20;
                Debug.DrawLine(pos,hit.point,Color.red);
            }
        }
        else {
            wheelRL.brakeTorque = 0;
            wheelRR.brakeTorque = 0;
        }

but for some reason
when it hits the trigger infront of the corner the car spins around

but it should just brake

I don’t really understand how your car moves from the code piece you attached, maybe it is because you don’t set
wheelRL.motortorque to 0. That can create a spinning effect but I’m just guessing.

but i set the wheelRL motortorque to 0 in that code

I don’t see why you’re doing Car AI this way. Use an engine/drivetrain script and then just send control values to the vehicle.

vehicle.steer = x
vehicle.accel = x

Then you have exact control over the vehicle. MotorTorque != max wheel speed, and the car will keep accelerating infinitely at 20 motor torque or so. Same with brake torque.

Yes you do, I missed it because of the indentation. Anyway it is really not possible to understand why your car is spinning from this code. You should tell us more.