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
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
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
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
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