Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Boost Car's Speed by Certain KpH value

Discussion in 'Physics' started by LaurieGale, Mar 3, 2021.

  1. LaurieGale

    LaurieGale

    Joined:
    Jan 21, 2021
    Posts:
    2
    Hi there,

    I'm creating a game that contains a car (with 4 wheels and a wheel collider for each) that is sped up by a constant number when space is pressed, rather than by holding space.

    I've removed all drag from the car's rigidbody so it remains at a constant speed unless space is pressed. However, I want the car to speed up (like a boost) by a desired speed but I can't get the car to speed up by the speed I want to. I am adding force to the car's rigidbody using AddForce, passing in the the boost force * 3.6. But since the object is a car. I also need to set the motor torque of the car's wheel colliders.

    How would I add a force and set the motor torques of the car's wheel colliders to get the car to speed up by a constant value every time space is pressed?
     
  2. jldooley1

    jldooley1

    Joined:
    Feb 15, 2020
    Posts:
    28
    AddForce() has an override which includes a parameter called ForceMode.

    Code (CSharp):
    1. rb.AddForce(speedChange * transform.forward, ForceMode.VelocityChange);
    With this, you can have the rigidbody's velocity change by a set amount (per call) which ignores mass.

    If instead you want to change to a particular velocity, you can directly alter the velocity of a rigidbody:
    https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html

    As for the wheel torque, I'm not sure what you're trying to achieve there. If you're using the wheels to move the car itself, it may be simpler to move the car as a whole and animate the wheels turning. I recall there being a few Unity projects involving car physics, might be worth checking those out to see how they do it.
     
  3. LaurieGale

    LaurieGale

    Joined:
    Jan 21, 2021
    Posts:
    2
    I've tried using different force modes and VelocityChange increases the speed of the car WAY too much. For example, if I want to increase the car's speed by 10KpH every time space is pressed, I use the following line:

    Code (CSharp):
    1. _rb.AddForce(transform.forward * 10 * 3.6f, ForceMode.VelocityChange);
    But this sends the car off at about 250KpH. The problem that I still have is I don't know how to properly control the speed increase without just messing about with parameter values
     
  4. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    You have 2 problems with that.

    1) Velocity is in m/s. If you want a velocity change of 10 km/h then you want a velocity change of 2.78 m/s, not 36 m/s

    2) When you detect space bar is pressed, set a variable e.g. bool addSpeedBoost then in your FixedUpdate script check if (addSpeedBoost) , add your force boost once and set addSpeedBoost to false. Assuming you are using Input.GetKeyDown (you just want to capture the boost request once). FixedUpdate may happen several times before Update notices changes in Input.

    Leave the wheel colliders alone. If you struggle with stability try applying a smaller boost over more physics cycles.
     
    Last edited: Mar 4, 2021
    LaurieGale likes this.
  5. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,428
    Maybe you're detecting the space bar press with Input.GetKey()? This will trigger the velocity change once per frame where the space bar is detected as pressed.

    If so, you should use Input.GetKeyDown() instead. This will ensure the space bar press is detected only once even if it's kept pressed. Then, every time the space bar is pressed the car will increase its velocity exactly in 10 km/h.