Max car speed ?

Hi
My problem with my old script is the max speed. When i reach the max speed of my car, I can’t make a dash “Like nitro” because i reach max speed. How to fix this ? One more thing, I go from 0 to 100 just in seconds. How to make it slow a bet down before reaching the max speed ?

using UnityEngine;
using System.Collections;

public class carSpeed : MonoBehaviour
{
    
    public float speed = 10;
    public int myspeed; // H/Km
    private Rigidbody rb;
    private Vector3 movs;

    void Start()
    {
              rb = GetComponent<Rigidbody>();
    }

    void Update() {
   
        rb.AddForce(movs * speed);
        float yspeed = rb.velocity.magnitude * 3.6f ;
        myspeed = (int)yspeed;

        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        movs = new Vector3(moveHorizontal, 0.0f, moveVertical);

        float maxVelocity = 20f;
        rb.AddForce(movs * speed);
        rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxVelocity);

        if (Input.GetKey(KeyCode.V)) {
            rb.AddForce(movs * speed * 20); // dash or nitro
        }
       
    } // end update

}

For nitro, don’t clamp your velocity while nitro is active. For acceleration: one option is to increase drag proportionally to the velocity; higher velocity, higher drag, lower acceleration. You could also scale speed the inverse of velocity, less forward force is then applied at higher speed meaning less acceleration .

1 Like

Add inertia to your H and V inputs in the Input Manager as well.

1 Like

I tried but…errors showed up. Maybe i didn’t understand you good.
Can you adjust my script ?

In “Input Manager” ? How and why ? People talking about inertia with codes !

Notice the Horizontal/turning Gravity is set to 3.It takes a bit to go from 0 to 1 or 0 to -1. Snap makes it got to zero when switching from positive or negative or vice versa. Now note that your Vertical/forward is set with 0 Gravity so it goes to 1 immediately instead of tweening to 1 as under inertia.

1 Like

Thank you for this information.
But still you didn’t answer my questions. what about the max speed and nitro ? can you please tell me what to do ? I didn’t understand mccaber point

He answered you. It is now up to you to interpret the answer according to your scheme. I would tell you the exact same thing. You want code I am available at 35 an hour.

Create a bool that checks whether or not your using nitro. Set it to true when the V key is pressed and false when the V key is released.

As Mccaber said, don’t clamp the velocity when nitro is active so :

float maxVelocity = nitroActive ? Mathf.Infinity : 20f;

Instead of Mathf.Infinity you could choose to use a higher value like 100.

OK. what about the second issue.

I think that you’ll have an easier time if you start from scratch, tbh.

Are you looking to do a sim, an arcade racer, a kart game, a sled game, what?

Will the game have gears?

Will the player have control of shifting gears?

Lastly, I hate to say but you’re going to need some math to get your max speed.

The force you add is going to have to be a moving number that is determined by the car’s velocity, the rpm’s, and the track conditions. Then when you’re moving from a starting position you also want to account for pealing out.

Give me a second, I’m looking up some math you could use.

EDIT: Okay, you’re going to want to apply force as f(x) = 3√ ( x ).

Now let me figure out how to plug everything in. You’ll need to change around the formula to determine what force is needed to maintain a given top speed, then you’re going to fidget with the graph to have it accelerate and top out the way you want to.

Also, if you’re doing gears you’ll basically want a different graph for each gear, or change the scaling of the graph at least. Each gear though would look like the line moves up and to the right the higher it goes.

I may need a little time though…

1 Like

You have more patience than I do. I usually define a maxSpeed and divide currSpeed by maxSpeed to get a number between 0 and 1 and the apply that as drag on the rb. For sideslip I get the relative x velocity and apply a negative force per frame that takes the relative velocity and multiplies it by the mass and drag and that eliminates sideslip. If I want a power-up I just add a value to the maxSpeed and let the rb.drag algorithm remain the same. The OP seems like he does not want to do the work to understand. The Lord helps those who help themselves.