Hello, I’m making a cannon that aims at the mouse:
Only problem is I don’t know to adjust the weld to aim at the mouse. This script below doesn’t really work.
Script:
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local boat = workspace:WaitForChild("Boat")
local cannon = boat:WaitForChild("Cannon")
local origin = cannon:WaitForChild("Aim"):WaitForChild("Weld")
local barrel = cannon:WaitForChild("Barrel")
mouse.Move:Connect(function()
local c0 = CFrame.new(origin.C0.Position, mouse.Hit.Position) * CFrame.Angles(0, -math.pi/2, 0)
origin.C0 = c0
end)
I managed to make it work somewhat. Now it can turn left and right accurately, but it can’t go up and down. Also it sort of leans when I want it to be completely straight.
Updated script:
local pivot = aim.CFrame
local lookAtCFrame = CFrame.lookAt(pivot.Position, mouse.Hit.Position)
local rotatedCFrame = lookAtCFrame * CFrame.Angles(0, -math.pi/2, 0)
weld.C1 = rotatedCFrame.Rotation
New script:
local pivot = aim.CFrame
local lookAtCFrame = CFrame.lookAt(pivot.Position, mouse.Hit.Position)
local rotatedCFrame = lookAtCFrame * CFrame.Angles(0, -math.pi/2, 0)
local x, y, z = rotatedCFrame:ToEulerAnglesYXZ()
weld.C1 = CFrame.Angles(x, y, 0)