Love you bro. I had a long time issue using Vector Forces, since I couldn’t figure out how to get it to work for vehicles of different weights. I appreciate this
Hey, I know this post is pretty old, but I’m just wondering how you ended up getting it to work. I don’t want you to code the whole thing for me, but if you could just take a look at my code I would really appreciate it (it’s short ). The problem is that it’s uncontrollabley shaking up and down until it flips over.
I’ve tried multiplying the force by the base’s mass, but same thing happens.
local Car = workspace.Car
local Base = Car.PrimaryPart
local WheelNames = {
"FrontRight",
"FrontLeft",
"BackRight",
"BackLeft"
}
local SuspensionMemory = {}
for _, WheelName in pairs(WheelNames) do
SuspensionMemory[WheelName] = 0.5
end
local WheelRadius = 0.5 -- should add as attribute in the car
local RestLength = WheelRadius*2*1.3 -- should add as attribute in the car
local Stiffness = 10 -- maybe add as attribute in vector force?
local Damper = 1 -- maybe add as attribute in vector force?
while true do
local Delta = game:GetService("RunService").Heartbeat:Wait()
local CarCFrame = Base.CFrame -- maybe add inside the next loop but this might be better performance
local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = {Car}
RayParams.FilterType = Enum.RaycastFilterType.Exclude
for _, WheelName in pairs(WheelNames) do
local Attachment: Attachment = Base[WheelName]
local AttachCF = Attachment.WorldCFrame
local StartPos = AttachCF.Position
local Up = AttachCF.UpVector
local Ray_Dir = Up * -(WheelRadius + RestLength) -- ray direction with length of the resting suspension + wheel radius. basically imagine if car is floating, the length of the suspension till bottom of wheel
local Ray_End = StartPos + Ray_Dir -- the position where the ray would end. maybe use for smthn if ray didnt hit?
local Raycast = workspace:Raycast(StartPos, Ray_Dir, RayParams)
local WheelPos = Ray_End + (Up * WheelRadius) -- the postion of the center of tire if the suspension is resting
if Raycast then
local LastLength = SuspensionMemory[WheelName]
WheelPos = Raycast.Position + (Up * WheelRadius)
local Length = (StartPos - WheelPos).Magnitude -- maybe clamp this between max and 0?
local Force_Stiffness = Stiffness * (RestLength - Length) -- rest minus length would be 0 if at full extension, so no forces
local Force_Damper = Damper * (RestLength - Length) / Delta -- hmmm idk what it does really
local Force = (Force_Stiffness + Force_Damper) * 100
Attachment.Spring.Force = Force * Raycast.Normal -- align to surfaceee
SuspensionMemory[WheelName] = Length
else
-- maybe suspension memory should be the max? idk abt forces
Attachment.Spring.Force = Vector3.new(0,0,0)
end
end
end
Here is what my “Car” is looking like, each attachment has a VectorForce named Sprind in it, and its enabled and set to 0, 0, 0 by default.
I also sent you a friend request so that I can share the place with you if you want to take a look for yourself. I apologise if I’m annoying you with work, but I have been trying to do this for a few days. Been scripting for many years, but never tried car physics before. Thanks for any replies!