Raycast suspension acting weird and starts bouncing out of control when player is on top of car

I’m working on a raycast suspension and I got it to work but when the player stands on top of it, the car starts bouncing out of control

here’s a vidoe

here’s my code just in case

local worldPosition : Vector3 = attachment.WorldPosition
local vectorForce : VectorForce = attachment.VectorForce
		
local rayDirection : Vector3 = -mainBody.CFrame.UpVector * (springLength + wheelRadius)
local raycast = workspace:Raycast(worldPosition, rayDirection, rayParams)

local springLength = 3
local wheelRadius = 0.5
local stiffness = 2000
local damping = 15

-- [ suspension force ]
if raycast then
   local springDir : Vector3 = mainBody.CFrame.UpVector
   local worldVelocity : Vector3 = mainBody:GetVelocityAtPosition(worldPosition)

   local rayDistance : number = (worldPosition - raycast.Position).Magnitude
   local offset : number = (springLength - rayDistance)

   local velocity : number = springDir:Dot(worldVelocity)
   local force : number = (offset * stiffness) - (velocity * damping)

   vectorForce.Force = springDir * force
end

any thoughts on why its doing this and how I can fix it?

heres a Image of what my car model looks like

this is the vidoe that Im following along with

3 Likes

Have you tried setting the Model’s PrimaryPart’s NetworkOwnership to nil (the server) and running the script on the server? This will make the server run the physics. And then once you are seated and driving the car, set the NetworkOwnership to the driver and run the script on the client.

2 Likes

Well, the problem would most likely be the weight of the player. The player has a weight, and the car gets dragged down.

2 Likes

That wouldn’t cause the car to flip though, it would just sink if that was the only problem.

2 Likes

That’s what I do with my cars, even the ones that aren’t raycast suspension based (I’ve toyed with many types of vehicle systems)

2 Likes

okay, I haven’t done that but I now did it and it seemed to fixed the problem

2 Likes

also its a little wiggling, how would I fix that?

EDIT:

2 Likes

Well, wiggling issues that I’ve ran into with raycast suspension is usually based on the loop that you use to raycast. Could you send that?

2 Likes

this is the loop that I am using to raycast

runService.Heartbeat:Connect(function(dt)

2 Likes

Try using RunService.Stepped, as that is ran every Physics frame. So you would be applying physics when physics are calculated.

2 Likes

okay I tried that and it really made my car bouncy and it start spinning and flew away

EDIT: actually it did that cause I made the springLength higher but I changed it back and the car is still wobbly

2 Likes

Ok so this is going to sound redundant but I’ve seen it work better than using RunService.Heartbeat:Connect, but try using

while true do
delta = RunService.Heartbeat:Wait
—Rest of the code
end

See if that corrects the wiggling. If that doesn’t work, keep this loop and try messing with your physics calculations for the required force.

2 Likes

kk. so I made my damping higher and it seems to stopped the wiggling

2 Likes

I also have other problem but I think it happens when I set the springlength to 5, and when the rays hit a block, the car spins out of control and flys upwards

any ideas how to avoid that?

2 Likes

May I see the finished product? Sorry, it’s just so satisfying looking at things hover.

2 Likes

When you get your offset, I was thinking that your offset starts to go negative. Try math.abs() in order to keep it as a positive offset.

2 Likes

lol yes, it seems to be working really good now

1 Like

it doesn’t seem to go negative

2 Likes

That’s so sick! Glad I could help man! :slight_smile:

2 Likes

yeah thank you so much, and would you know any other solution to the car spinning and flying up in the air?

if not, it should be fine, I am now able to move on to the steering and moving the car

2 Likes