Hello there. I’ve got an lerping issue today.
I’m trying to make a turret that is mouse controlled.
This is how the hierarchy looks like:
This is how the turret looks
The problem I’m facing is that when I sit in the seat, half of the model flies away.
Video -
What I want it to do is to simply behave like a normal turret.
Here is my code that’s located in the ClientScript [Localscript] in the PlayerGui:
local TurretsFolder = script.TurretsFolder
local EventsFolder = script.EventsFolder
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local RS = game:GetService("RunService")
local Track
track = RS.RenderStepped:connect(function()
for i,v in pairs(TurretsFolder.Value:GetChildren()) do
if Mouse.Hit.p.Y < 0 then
local Position = CFrame.new(v.Body.TurretBase.CFrame.p, Vector3.new(Mouse.hit.p.X,1,Mouse.hit.p.Z))
local BasePosition = CFrame.new(v.Base.PrimaryPart.CFrame.p,Vector3.new(Mouse.hit.p.X,v.Base.PrimaryPart.CFrame.Y,Mouse.hit.p.Z))
local LerpPosition = v.Body.TurretBase.CFrame:Lerp(Position,0.15)
local BaseLerpPosition = v.Base.PrimaryPart.CFrame:Lerp(BasePosition,0.15)
v.Base:SetPrimaryPartCFrame(BaseLerpPosition)
v.Base.Connection.Weld.C1 = LerpPosition
end
if Mouse.hit.p.Y > 0 then
local pos = CFrame.new(v.Body.TurretBase.CFrame.p,Vector3.new(1,Mouse.hit.p.Y,Mouse.hit.p.Z))
local basepos = CFrame.new(v.Base.PrimaryPart.CFrame.p,Vector3.new(Mouse.hit.p.X,v.Base.PrimaryPart.CFrame.Y,Mouse.hit.p.Z))
local lerppos = v.Body.TurretBase.CFrame:lerp(pos,0.15)
local baselerppos = v.Base.PrimaryPart.CFrame:lerp(basepos,0.15)
v.Base:SetPrimaryPartCFrame(baselerppos)
v.Base.Connection.Weld.C1 = lerppos
end
end
end)
My ServerScript [Script] that’s located in the model:
for i,v in pairs(script.Parent.Parent.Turrets:GetDescendants()) do
if v:IsA('BasePart') then
local Turret = v.Parent.Parent
if v.Parent.Name == 'Base' then
if v.Name == 'Connection' then
local CC = Instance.new('Weld')
CC.Part0 = Turret.Body.MainBase
CC.Part1 = v
CC.C0 = Turret.Body.MainBase.CFrame:Inverse() * v.CFrame
CC.Parent = v
else
local CC = Instance.new('Weld')
CC.Part0 = v
CC.Part1 = Turret.Base.Connection
CC.C0 = v.CFrame:Inverse() * Turret.Base.Connection.CFrame
CC.Parent = v
end
elseif v.Parent.Name == 'Body' then
if v.Name == 'MainBase' then
local CC = Instance.new('Weld')
CC.Part0 = v
CC.Part1 = Turret.Body.TurretBase
CC.C0 = v.CFrame:Inverse() * Turret.Body.TurretBase.CFrame
CC.Parent = v
elseif v.Name ~= 'TurretBase' then
local CC = Instance.new('Weld')
CC.Part0 = Turret.Body.TurretBase
CC.Part1 = v
CC.C0 = Turret.Body.TurretBase.CFrame:Inverse() * v.CFrame
CC.Parent = v
end
elseif v.Parent.Name == 'ConnectingBody' then
local CC = Instance.new('Weld')
CC.Part0 = Turret.Body.TurretBase
CC.Part1 = v
CC.C0 = Turret.Body.TurretBase.CFrame:Inverse() * v.CFrame
CC.Parent = v
end
end
end
for i,v in pairs(script.Parent.Parent.Turrets:GetDescendants()) do
if v:IsA('BasePart') then
v.Anchored = false
end
end
local function Sit(SeatWeld)
if SeatWeld:IsA('Weld') then
local Character = SeatWeld.Part1.Parent
local Player = game.Players:GetPlayerFromCharacter(Character)
local CCS = script.ClientScript:Clone()
CCS.Parent = Player.PlayerGui
CCS.Disabled = false
end
end
script.Parent.Parent.Seat.ChildAdded:Connect(Sit)
I can provide more information if you need it.
Thanks in advance!!