So recently, I had this glitch with the roblox jeep that causes the vehicle to fly into the air, or fly into the ground. The reason why this happens is because when the vehicle is moving forward the vehicle moves forward using velocity it uses lookvector. So if the vehicle is tilting upward, then the vehicle is going to want to go into the air, or if the vehicle is tilting down, then the vehicle is going is to go into the ground (If the vehicle is accelerating forward)
How can I keep this from happening? I can’t apply a force to the wheels because the wheels are going to be rotating, plus I would rather apply a force to the main vehicle chassis.
I have no more words to describe the help I need. If someone tell me what I can do to fix this, then no words would describe how happy I feel.
Here is the jeep:
Here are the lines of code that I have changed:
Line 160: local rotVelocity = humanoidRootPart.CFrame:vectorToWorldSpace(Vector3.new(movement.Y * stats.Speed.Value / 12.5, 0, -humanoidRootPart.RotVelocity.Y * 5 * movement.Y)) -- Originally 12.5 was 50, but since this is a modified jeep, it's going to be different.
Line 122: wheelWeld.C0 = CFrame.new(0, -math.min(thrusterHeight, stats.Height.Value * 2) + (wheelWeld.Part1.Size.Y / 2), 0) -- Originally * 2 was * 0.8, But you know why ;)...
Here is line of code that moves the vehicle: 154, and 155: local velocity = humanoidRootPart.CFrame.lookVector * movement.Y * stats.Speed.Value humanoidRootPart.Velocity = humanoidRootPart.Velocity:Lerp(velocity, 0.1)
I think there might be a few opportunities to simplify what’s going on here.
Assuming a variable weight distribution over the front and rear axle we know that accelerating or decelerating at given rate means that the weight on the front and rear axle changes the tilt of the car body. If this is the case, why are you choosing to move the car using the lookvector of the car body?
In an ideal scenario the engine turns the wheels forward, the wheels push backward on the road surface and, in reaction, the road surface pushes back in a forward direction. This means that rather than the velocity being dependent upon the lookvector of the carbody, the speed, or velocity vector is determined by handling the longitudinal and lateral forces separately in conjunction with the magnitude of your Engineforce.
TLDR: The direction of your Engineforce can be determined using the slope between the front and rear axles. This means that the body of your car will no longer determine its heading. The overall directional velocity of your car takes many factors into account; wheel force, braking force, drag etc. This means that if we were to ignore resistance forces like this we would find our car accelerating to infinite speeds; hence your car taking off like a plane.
Make a frame for the bottom of the car if you can. The frame of the car is always on the same plane as both axles, whereas the body is placed upon the shocks to provide stabilization. The frame provides you with a rigid body in which you could apply velocity. This could be a transparent part, or a visible one, as long as the axles always remain in the same position in respect to the frame (A constraint or something of the sort).
You could average the position of the front and back wheels and take them away from each other to get a vector pointing from the middle of the rear axel to the middle of the front axel:
And then the part where it does the setting of the velocity to humanoidRootPart and does the lerp, just keep that as it originally was. So all you’re doing is swapping out the lookVector part really. I don’t know what the movement.Y term is intended to do, but if you have any issues try adding it back in.
And make sure to define the wheels or swap out those variables if you already have the wheels somewhere in the script.
How come I can’t reverse though? I can move forward. But even if I press ‘S’ I move forwards instead of backwards. Plus I still fly in the air / do weird things.
Possibly because you are defining the lookVector to always be the forward-facing vector. In that case, you may want to consider negating it under the conditions where the vehicle is supposed to reverse, aka, when S is pressed.
Apologies, I made an assumption that your speed value would be negative for going backwards. As @WoolHat suggests, if you’re reversing and your stats.Speed.Value is a positive then you’ll want to negate it.
I think that’s what movement.Y did in the original script, so you can add that same term back in and it should fix it.