Insure that the player can't increase speed when over the speed limit

We’re working with a space shuttle in zero-G that can only thrust forward.

How do I go about insuring that the player can’t just accelerate infinitly but instead can only thrust up to a certain top speed and also without limiting the possible speed the shuttle could end up in (If perhaps it gets hit by an asteroid or something).

I’ve tried so many things but for some reason the results don’t make sense to me. Like for one instance the shuttle didn’t thrust past the top speed but when I slightly turned and kind if drifted in space I accelerated way beyond my top speed, only moving to the left but not actually forward. I can kind of guess why it happened but I can’t easily comprehend the problem to think of a solution.

If you don’t want to clamp the velocity, what you need is some kind of friction, even though that doesn’t make much sence in space ,heh. The easiest thing to do would probably be to define a friction value between zero and one (~percentage) and multiply your speed with (1 - friction). For example, 10% friction:

float friction = 0.1f; // = 10%

void FixedUpdate() {
    speed *=  (1 - friction);
}

This will make you lose 10% of your current speed in every physics frame. Make sure you put it in FixedUpdate() and play around with the percentage. You will need to increase your acceleration to counteract the friction

Well if you want to make sure that your object will not increase over speed limit is that do an if else statement so we can go something like this

float speed = 10.0f;
float speedLimit = 15.0f;

if(speed > speedLimit){
//reduce speed here
}

It’s not the actual code okay. So what i am telling you is the logic behind it. Or there’s another way of doing it. You can use Mathf.Clamp. Use it on the rigidbody.velocity values after the force is applied to determine speed caps.

It should be something like this

const float MaxSpeed = *Something*;
const float regularAcceleration = *Acceleration*;

if(velocity >= MaxSpeed){
    velocity = MaxSpeed
    acceleration = 0;

}else
     acceleration = regularAcceleration ;