Car wheels not stopping when I brake

Hi guys

Im creating a kart game and when I stop the kart the wheel dont stop and the kart takes a very long time to stop and reverse
https://im4.ezgif.com/tmp/ezgif-4-6dbac2aab2.gif

I have a wheel colider on each wheel

using System.Collections;
using UnityEngine;

public class Car : MonoBehaviour
{
    public float maxTorque = 5000f;
    public float speed = 5000f;


    public Transform centerofMass;

    public WheelCollider[] wheelColliders = new WheelCollider[4];
    public Transform[] tireMeshes = new Transform[4];

    private Rigidbody m_rigidBody;

    void Start()
    {
        m_rigidBody = GetComponent<Rigidbody>();
        m_rigidBody.centerOfMass = centerofMass.localPosition;
    }

    void Update()
    {
        UpdateMeshesPositions();
    }

    void FixedUpdate()
    {
        float steer = Input.GetAxis ("Horizontal");
        float accelrate = Input.GetAxis ("Vertical");

        float finalAngle = steer * 45f;
        wheelColliders [0].steerAngle = finalAngle;
        wheelColliders [1].steerAngle = finalAngle;

        for (int i = 0; i < 4; i++)
        {
            wheelColliders[i].motorTorque = accelrate * speed * maxTorque;        }
    }

    void UpdateMeshesPositions()
    {
        for(int i = 0; i < 4; i++)
        {
            Quaternion quat;
            Vector3 pos;
            wheelColliders[i].GetWorldPose(out pos, out quat);

            tireMeshes[i].position = pos;
            tireMeshes[i].rotation = quat;
        }
    }
}

bump

Your damping may be better at around 100 then the springs will keep all wheel colliders on the ground better. It may be all too stiff.
What is the RigidBody mass?

I have my RigidBody mass set to 100
I tried putting the damping to around 100 but that didnt change anything put it even less and that didnt change anything ether

We can use the drag, when you stop the accelerator adds the drag to 1 and out until you see how far you want the car to stop. When you load the accelerator it adds the drag to zero by default again.

Im not quite sure what u mean by this can you show me what I need to change Thanks

Yes, of course. Rigidbody.drag.

private void Update(){

    Rigidbody rb = GetComponent<Rigidbody> ();  

    if (Input.GetKey (KeyCode.B))
    {        
        rb.drag = 1;
    }
    else{
        rb.drag = 0;
    }  
}
1 Like

Since the torque break does not work properly use drag to compensate.

thanks ill give this a try

I hate to be annoying but where should I add this?

I figured you were at an elementary stage so just gave the specifics for an Update function.

Now considering your code you have posted above, just copy the if /else part with {} and paste that into your Update function. You will also have to change my ‘rb’ variable name to what you already have established in Start() ‘m_rigidBody’.

It could be in any script that has an Update() function because they all get called each frame but would need to use a different method of getting at the rigidBody. Just depends on how you construct your code. You need to understand all this because no one will write all your code for you.

The ‘B’ keyboard button will be your brakes. If you want to use a joystick button or have auto brakes or whatever, that is an entirely different subject and more complex.

I hope you understand things a little more now.

Ok thanks for the info

Yeah I’m very new to coding so coding just really confuses me

Hopefully I can fix this problem soon cause its driving me crazy

We all had to start somewhere. You will suddenly have an ‘I get it’ moment when the light bulb switches on in your head then you will be coding all kinds of stuff. :sunglasses:

Try to increase the “dampingRate” and set the motorTorque to “1” when it’s braking or not accelerating.