How to make car physics better?

I’m making a racing game and most of my testers say the Roblox physics make it hard. My goal is to make it somewhat like Mario kart’s physics, but I don’t know where to start. Also, sometimes it the wheels act weird when teleporting like in this video:

How the cars move:

coroutine.wrap(function()
	while true do
		task.wait()
		
		if info.BoostTime > 0 then
			seat.Throttle = 1
		end
		
		if base.Orientation.Z >= 10 then
			bodyGyro2.MaxTorque = Vector3.new(50000, 0, 50000)
		elseif base.Orientation.X >= 5 then
			bodyGyro2.MaxTorque = Vector3.new(50000, 0, 50000)
		elseif base.Orientation.Z <= -60 then
			bodyGyro2.MaxTorque = Vector3.new(50000, 0, 50000)
		else
			bodyGyro2.MaxTorque = Vector3.new(0, 0, 0)
		end
		
		if info.Stunned == false then
			
			frontLeft.PartB2.SteeringConstraint.TargetAngle = steerAngle * seat.Steer * 2 + seat.Throttle
			frontRight.PartA2.SteeringConstraint.TargetAngle = steerAngle * seat.Steer * 2 + seat.Throttle
			
			frontLeft.Wheel.WheelConstraint.AngularVelocity = info.Speed * seat.Throttle
			frontRight.Wheel.WheelConstraint.AngularVelocity = info.Speed * -seat.Throttle

			BackLeft.Wheel.WheelConstraint.AngularVelocity = info.Speed * seat.Throttle
			BackRight.Wheel.WheelConstraint.AngularVelocity = info.Speed * -seat.Throttle
		else
			frontLeft.Wheel.WheelConstraint.AngularVelocity = 0
			frontRight.Wheel.WheelConstraint.AngularVelocity = 0

			BackLeft.Wheel.WheelConstraint.AngularVelocity = 0
			BackRight.Wheel.WheelConstraint.AngularVelocity = 0
		end
		

		bodyGyro.MaxTorque = Vector3.new(7000000, 0, 0)
		
		if base.Orientation.Z > 10 then
			bodyGyro.P = 25000
			bodyGyro.D = 1000
			bodyGyro.MaxTorque = Vector3.new(5000000, 0, 0)
			bodyGyro.CFrame = CFrame.new(base.Position, base.Position + base.CFrame.LookVector)
		else
			bodyGyro.MaxTorque = Vector3.new(0, 0, 0)
			bodyGyro.P = 0
			bodyGyro.CFrame = CFrame.new(base.Position, base.Position + Vector3.new(0, 0, 1))
		end
	end
end)()
1 Like

So I’m going to assume this is a client-sided script and the player has network ownership of their vehicle.

I see quite a few inefficiencies in your code. First off, I would use a Run Service event instead of a while true loop. I would recommend using RunService.PreSimulation, but you can read about the order from the Task Scheduler | Documentation - Roblox Creator Hub.

For any math you have, I would calculate that once and store it in a local variable, it’s a small thing but could yield some slight performance benefits.

I would also have variables for each of the WheelConstraints and SteeringConstraints defined outside your loop, no reason to access them every time.

You might be better off using standard motor and spring constraints to simulate a regular car instead of manually coding a ton of constraints every frame. You do have some custom logic you would want to add, but it might feel a little smoother than your current system and is something you should consider.

1 Like

It’s actaully a normal script stored in the character if that helps. I was gonna use run service when i realized it was a while loop.

1 Like

I also am very bad at constraints, so the code is probably jank

1 Like

I recommend learning about hinges, they can be real useful here. Also alignorientation would be great.

2 Likes

Yes, I second this. A motor, align orientation, any kind of standard wheel will help you. I might be able to steal you a car from a game I have that is setup, you could also just look at the roblox template cars.

The delay and lag would be heavily improved if you used Network Ownership | Documentation - Roblox Creator Hub. Give the client ownership of their car and do all the code from a local script. This is less of a suggestion, more of a mandatory requirement for anything the player controls directly. All forms of characters for the player should be owned by them to reduce delay and input lag.

Alright thanks for the help. :smiley: