My script involves a chassis that has wheels. The wheels that I have modeled are supposed to weld each other, and then weld the visual wheels to the chassis wheels, and then supposed to align with the chassis wheels so it looks like the visual wheels are moving and turning. The welding works, but for some reason it doesn’t align properly and it aligns very weirdly
As the car goes forward, the wheels rotate along weirdly like the center of origin is the car origin, but all the origins are centered, so that’s not the case.
Code:
local vBody = script.Parent.VisualBody
local rigbody = script.Parent.rig.RealBase
local visFR = vBody.FRWheel.FRRim
local visFL = vBody.FLWheel.FLRim
local visRR = vBody.RRWheel.RRRim
local visRL = vBody.RLWheel.RLRim
--for _,v in pairs(vBody:GetDescendants()) do
-- if v:IsA("BasePart") then
-- v.Transparency = 1
-- end
--end
local function weldall(arraytouse)
local array = {}
for i,v in pairs(arraytouse) do
if v:IsA("BasePart") then
table.insert(array, v)
end
end
for i,v in pairs(arraytouse) do
if v:IsA("BasePart") then
local weld = Instance.new("WeldConstraint",v)
weld.Part0 = array[i]
weld.Part1 = array[i+1]
end
end
end
local function weldparts(p1,p2)
local weld = Instance.new("WeldConstraint",p1)
weld.Part0 = p1
weld.Part1 = p2
end
--destroy welds first
for _,v in pairs(vBody:GetDescendants()) do
if v:IsA("WeldConstraint") or v:IsA("Motor6D") or v:IsA("Weld") then
v:Destroy()
end
end
--set position of rig
rigbody.Parent:SetPrimaryPartCFrame(CFrame.new((visFL.Position.X+visRL.Position.X)/2, visFL.Position.Y + 1 , (visFL.Position.Z + visFR.Position.Z)/2))
rigbody.Size = Vector3.new(math.abs(visFL.Position.X - visRL.Position.X),1, math.abs(visFL.Position.Z - visFR.Position.Z))
--weld everything
local fr_b1 = script.Parent.rig.FR.base1
local fl_b1 = script.Parent.rig.FL.base1
local rr_b1 = script.Parent.rig.RR.base1
local rl_b1 = script.Parent.rig.RL.base1
local mainB = rigbody
fr_b1.Position = mainB.Position + Vector3.new(mainB.Size.X/2,0,mainB.Size.Z/2)
fl_b1.Position = mainB.Position + Vector3.new(mainB.Size.X/2,0,-mainB.Size.Z/2)
rr_b1.Position = mainB.Position + Vector3.new(-mainB.Size.X/2,0,mainB.Size.Z/2)
rl_b1.Position = mainB.Position + Vector3.new(-mainB.Size.X/2,0,-mainB.Size.Z/2)
rigbody.CFrame *= CFrame.Angles(0,math.rad(180),0)
weldall(vBody.LDoor:GetDescendants())
weldall(vBody.RDoor:GetDescendants())
weldparts(vBody.LDoor.LDoor, vBody.VisualBody.MainBody)
weldparts(vBody.RDoor.RDoor, vBody.VisualBody.MainBody)
weldall(vBody.VisualBody:GetDescendants())
weldparts(vBody.VisualBody.MainBody, rigbody)
weldall(visFL.Parent:GetDescendants())
weldall(visFR.Parent:GetDescendants())
weldall(visRL.Parent:GetDescendants())
weldall(visRR.Parent:GetDescendants())
weldparts(visFL,rigbody.Parent.FL.wheel)
weldparts(visFR,rigbody.Parent.FR.wheel)
weldparts(visRL,rigbody.Parent.RL.wheel)
weldparts(visRR,rigbody.Parent.RR.wheel)
visFL.Parent:SetPrimaryPartCFrame(rigbody.Parent.FL.wheel.CFrame)
Code involving the aligning of the wheels is at the bottom
“visFL.Parent:SetPrimaryPartCFrame(rigbody.Parent.FL.wheel.CFrame)”