Hello!
I was creating a turret system however when I started testing this weird thing happened.
As you can see the turret isn’t actually facing the mouse. I double checked to make sure the lookvector for the mouse was correct and it seems correct but it clearly isn’t.
Here’s my code:
local Players = game:GetService("Players")
local plr = Players.LocalPlayer
local RunService = game:GetService("RunService")
local mouse = plr:GetMouse()
local FlakCannon = workspace.FlakCannonPrototype
--Predefined Variables
mouse.TargetFilter = FlakCannon
--Parts
local gun = FlakCannon:WaitForChild("Gun")
--Axis Constraints
local maxX = math.atan(math.rad(20))
local minX = math.atan(math.rad(-20))
local maxY = math.atan(math.rad(28))
local minY = math.atan(math.rad(-10))
local maxZ = math.atan(math.rad(20))
local minZ = math.atan(math.rad(-20))
--TestRay
local TestRay = Instance.new("Part")
TestRay.Color = Color3.new(1, 0, 0)
TestRay.Transparency = 0.5
TestRay.Parent = workspace
TestRay.CanCollide = false
TestRay.Anchored = true
local function RotateTurretToMouse()
local hit = mouse.Hit
local hitPos = hit.p
local gunPos = gun.Position
local MouseLookVector = (gunPos - hitPos).Unit
local lookVectorX = math.clamp(MouseLookVector.X, minX, maxX)
local lookVectorY = math.clamp(MouseLookVector.Y, minY, maxY)
local lookVectorZ = math.clamp(MouseLookVector.Z, minZ, maxZ)
local lookVector = Vector3.new(lookVectorX, lookVectorY, lookVectorZ)
local lookPoint = gunPos + lookVector * 100000
gun.CFrame = CFrame.lookAt(gunPos, lookPoint)
TestRay.CFrame = CFrame.new(gunPos, lookPoint)
TestRay.Size = Vector3.new(0.1, 0.1, 100)
end
RunService.RenderStepped:Connect(function()
RotateTurretToMouse()
end)
