Raycast suspension, the friction force makes car fling and and applies a high number forces to the car

so im trying to add friction force to my raycast car to stop sliding but when my car starts to have some velocity to it, the car flings as seen in the vidoe

if i set the friction to 0.2, it flings but not as much, but when the velocity is up to 1, it flings when velocity gets added to the car

Screenshot 2022-10-18 173345

this what happens when the force is 0.2, dont worry about what happen at the end lol

this what happens when the force is 1

heres my code, this is also a server script btw

		-- [ steering force ]
		if raycast then
			local steeringDir : Vector3 = mainBody.CFrame.RightVector
			local worldVelocity : Vector3 = mainBody:GetVelocityAtPosition(worldPosition)
			
			local steeringVelocity = steeringDir:Dot(worldVelocity)
			local velocityChange = -steeringVelocity * friction

			local acceleration = velocityChange / dt
			steeringForce.Force = steeringDir * mass * acceleration
		end

when I print the force, which is print(steeringDir * mass * acceleration) it prints 7374967, -11393829, -7842777 and the first time I printed it, it printed -591772002549760, 17185158201344, 1156724484472832

this is the vidoe that I’m following, skip to 17:58

this code is what he used to move the tires and steer so I’m not sure if this code is suppose to be on the server

1 Like

I think you should check out my PhysicsCharacterController.

It’s also uses raycast suspension and uses VectorForce, but applied on the character instead of a car.

Check out the Running.lua

--Unit XZ Is velocity with Y component equal to zero
    local flatFrictionScalar
    if onGround and isMoving then
        --This equations allows vector forces to stabilize
        --"Decrease flat friction when low speed"
        --Constant friction when above velocity is above 2 usually
        flatFrictionScalar = model:GetAttribute("FlatFriction")*(1.0-math.exp(-2*xzSpeed))
        totalDragForce += -unitXZ*flatFrictionScalar
    end
1 Like

ok ill check it out (300characters)

ok so I was sitting here playing with the code and I kinda fixed it myself

the code now

		-- [ friction force ]
		if raycast then
			local steeringDir : Vector3 = mainBody.CFrame.RightVector

			local steeringVelocity : number = steeringDir:Dot(originVelocity)
			local velocityChange = -steeringVelocity * friction
			
			local force : number = steeringDir * mass * velocityChange
			steeringForce.Force = force
		end

all I did was remove the local acceleration = velocityChange / dt

but there’s a new bug that I need help with

as far as I tested it, it works perfectly but there’s one issue, is that the car only wants to go in 1 direction, to if I rotate it, it will freak out or turn back around

as you see it turns back around

and here when I rotate it the other direction, it floats away real fast

Hey, I know it is a bit late but, how did you detect the raycast for this one?

its fine and sorry for late reply lol, are you talking about the raycast visualizes? or are you talking about adding force to push the car up?

Not the force to push the car up, but what are the origin, and directions did you use for the raycast? Like this

workspace:Raycast(origin, dir, raycastParams)

How did you detect the Raycast?

oh this is what I did

local springMaxLength = (springLength + wheelRadius)
			
local origin = springPoint.WorldPosition
local direction = -carCFrame.UpVector * springMaxLength
local ray = workspace:Raycast(origin, direction, params)

I used attachments and springPoint, is the attachment

Oh, basically the same thing as the suspension. Thanks!

your welcome – eeeeeeeeeeeeechars s s s

1 Like