As mostly explained in the title, I have the following script for rotating a turret to the players mouse, I want to incorporate checks that will check if the turret can face the position of the players mouse and if not to be able to detect that and do some custom logic
script:
local platform = script.Parent.Platform
local turret = script.Parent
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr, mousePosition)
if mousePosition then
local platformPos = platform.CFrame.Position
local turretPos = turret.Turret.CFrame.Position
local deltaX = mousePosition.X - platformPos.X
local deltaY = mousePosition.Z - platformPos.Z
local angle1 = math.atan2(-deltaX, -deltaY)
platform.HingeConstraint.TargetAngle = math.deg(angle1)
local deltaZ = mousePosition.Y - turretPos.Y
local angle2 = math.atan2(-deltaZ, math.sqrt(deltaX^2 + deltaY^2))
turret.LiftMain.HingeConstraint.TargetAngle = math.deg(angle2)
end
end)