Is there anyway to prevent bodythrusts from creating unlimited amounts of speed

Hey guys,
So I think the title is self explanatory. Currently am creating cars with bodythrusts to move forward and back, but the body force creates an extremely high amount of force. Im setting the force to 5000 on the Z axis using scripts. Everything works fine but if u hold w the car speeds up extremely high, and I cant lower that amount down bcz I need good acceleration (lowering it affects acceleration)

If you know how to prevent this or any other alternatives to bodythrusts, please tell me below!

I really don’t know about this but why not use math.clamp() to clamp the speed?

So it’ll be this?

thrust.Force = Vector3.new(0, 0, math.clamp(5000))

I’ve never used this function before.

math.clamp() basically returns the number you originally put if it is between the range of 2 numbers, otherwise it will return the number that is either close to the maximum or minimum number.

Example 1:
math.clamp(5,1,10)
1 is the minimum number and 10 is maximum number.

Returns 5 because it is between the range of these 2 numbers.

Example 2:
math.clamp(1,5,10)
5 is the minimum number and 10 is the maximum number.

Returns 5 because 1 is not in the range, so instead it will round up to the minimum number, or vice versa if the number is more than 10, then it will return 10.

1 Like

No that doesn’t work, to actually stop acceleration you’d have to get the part’s velocity and based on the velocity you’d have to apply an opposite force if it’s above the max speed. Why would you think using a maths function that is irrelevant to physics would stop acceleration?

1 Like

They wanna lower down the speed, and I thought that clamping it somewhere else is fine.

1 Like

Damn, is there anyway to calculate how much force should be applied in the opposite direction?

But still, at least I taught them how to use that math function.

1 Like

You’re using bodythrust so you definitely need to take in account of the mass so basically if you get the linearassemblyvelocity you’d have to calculate the magnitude of the X and Z which basically gives you the speed on the X and Z however to get the actual direction you need to use the linearassemblyvelocity vector giving you the direction to apply the force.

1 Like

Interesting, will get a look at this later today when I get on my pc

Hey, am having some problems with this. So I first added another bodyThrust named SpeedControl inside the car, then I set its force using scripting like this:

speedControl.Force = Vector3.new(0, 0, -platform.AssemblyLinearVelocity.Z * 100)

It works originally, but once you turn the car around 180d, it actually adds up the force and accelerates the car extremely fast. Could you help me with this?
https://streamable.com/jpf9gv

You also have to take into account the X axis. Or your calculations were wrong.

I did that too and its still the same. Could you provide me a formula?

You could also see that I took the negative counterpart of the linear velocity to make sure am pushing in reverse.

Show me your current script as the one you provided was too vague.

It’s easier to just use BodyVelocity. You can code acceleration yourself, since velocity can be controlled by acceleration.

local  BodyVelocity = Instance.new("BodyVelocity", Car) -- an example
BodyVelocity.MaxForce = Vector3.new(1, 0, 1) * 1000000 -- stops the body velocity from influencing the Y axis and sets a big enough force to push the car

local CarAcceleration = 25 -- in studs per second per second (studs/s^2, will go at this speed in one second)
local CarDeceleration = 50 -- studs/s^2 (will slow down this much speed in one second)
local CarDragConstant = 10 -- the drag constant of the car used to slow down the car in a neutral state (higher value means the car will slow down faster and vice versa)
local MaxSpeed = 50 -- studs/s (max speed of car)

function RenderStepped(dt)
    if Forward then
        -- the car is going forward
        BodyVelocity.Velocity += CarDirectionVector.Unit * CarAcceleration * dt
   elseif Back then
        -- the car is going backwards
        BodyVelocity.Velocity -= CarDirectionVector.Unit * CarDeceleration * dt
    else
        -- the car is in a neutral state
        BodyVelocity.Velocity -= BodyVelocity.Velocity * CarDragConstant * dt
    end
    if (BodyVelocity.Velocity * Vector3.new(1, 0, 1)).Magnitude > MaxSpeed then
        -- car is going faster than the max speed
        BodyVelocity.Velocity = BodyVelocity.Velocity.Unit * MaxSpeed
    end
end

the above is just some pseudo code, just to show how this would be done using body velocity.

2 Likes

Hm, I didn’t use body velocity earlier bcz it was stopping my springs but I forgot that I can disable it on the y axis :woozy_face:

Also, in the last if that checks if it’s going faster than the max speed, what if the car is falling down? I’ll try it when I get on my pc sometime later.

Edit: nvm I didn’t see the last if properly, thanks

Though there’s a downside to that since if you do it that way you can’t have other bv’s due to it conflicting. Furthermore, you wouldn’t be able to use centripetal force for the turning making the turning seem highly unrealistic. That’s if you are using a scripted suspension chassis.

You are able use BodyVelocity and BodyGyro on the same part.

Yeah but that’s just not really realistic.