-
What do you want to achieve?
I am trying to make each turret point atMouse.Target
. -
What is the issue?
When I set the CFrame of the turret’s primary part to faceMouse.Target
, the entire model moves instead of each individual turret.
https://gyazo.com/de578ed8ce9f5bd3b612fbc2d267759a -
What solutions have you tried so far?
I am using WeldConstraints and thought if I welded all the turret’s part to a RootPart, then weld that root part to a turret base, and weld the turret base to the ship RootPart that when I change the CFrame of the turret to faceMouse.Target
, the turret would look at the target. Unfortunately, this did not work.
Here is the code I am using:
-- In a server script.
function WeldPair(x, y)
local weld = Instance.new("WeldConstraint")
weld.Part0 = x
weld.Part1 = y
weld.Parent = x
end
local function weldModel(model, plr)
for _,v in pairs(model:GetDescendants()) do
if v:IsA("BasePart") and not v:FindFirstChildWhichIsA("WeldConstraint") then
v.Anchored = false
WeldPair(model.PrimaryPart, v)
v:SetNetworkOwner(plr)
end
end
end
local function generateShip(player)
for _, turretRoot in pairs(ship.TurretSlots:GetChildren()) do
if turretRoot:IsA("Model") then
local turret = workspace.CompletedTurrets.Turret1:Clone()
turret.Name = "main"
turret.Parent = turretRoot
turret:SetPrimaryPartCFrame(turretRoot.PrimaryPart.CFrame)
WeldPair(turret.PrimaryPart, turret.Middle)
WeldPair(turret.Middle, turretRoot.PrimaryPart)
WeldPair(turretRoot.PrimaryPart, ship.PrimaryPart)
weldModel(turret, player)
end
end
weldModel(ship, player)
end
-- In a local script.
function lookAt(target, eye)
local forwardVector = (eye - target).Unit
local upVector = Vector3.new(0, 1, 0)
local rightVector = forwardVector:Cross(upVector)
local upVector2 = rightVector:Cross(forwardVector)
return CFrame.fromMatrix(eye, rightVector, upVector2)
end
function turretFire()
if clientShip then
mouse.Button1Down:Connect(function()
local target = nil
target = mouse.Target
if (target) and target:IsDescendantOf(workspace.Asteroid) then
for _, turret in pairs(clientShip.TurretSlots:GetChildren()) do
local mainTurret = turret.main
mainTurret.PrimaryPart.CFrame = lookAt(target.Position, mainTurret.PrimaryPart.Position)
end
end
end)
end
end
Any help is appreciated, thanks!