Reposting because of terrible formatting.
I’m editing a compass script to create a directional guide towards a player’s position. My goal is to have the compass point at the players location when looking towards the player.
However I’m having problems with getting the players position to be the center point, which is comes from the [dev.Middle] line. I don’t really have much experience with rotating GUI elements and the standard math library. Any help or step in the right direction is much appreciated.
local target = game.Workspace.King1:WaitForChild("HumanoidRootPart")
local player = game.Players.LocalPlayer
local myPos = player.Character:WaitForChild("HumanoidRootPart")
local camera = workspace.CurrentCamera
local dev = script.Parent
local pointRelativeToCamera = camera.CFrame:pointToObjectSpace(target.Position)
local relativeVector = (pointRelativeToCamera*Vector3.new(1, 0, 1)).unit
local units = {
[dev.Middle ] = math.atan2(relativeVector.Z, relativeVector.X)/4
--[[
[dev.N ] = -math.pi * 4/4;
[dev.E ] = -math.pi * 2/4;
[dev.S ] = math.pi * 0/4;
[dev.W ] = math.pi * 2/4;
--]]
}
local lastY = 0
function restrictAngle(angle)
if angle < -math.pi then
return angle + math.pi*2
elseif angle > math.pi then
return angle - math.pi*2
else
return angle
end
end
while true do
local delta = wait(1/30)
local look = camera.CoordinateFrame.lookVector
local look = Vector3.new(look.x, 0, look.z).unit
local lookY = math.atan2(look.z, look.x)
local difY = restrictAngle(lookY - lastY)
lookY = restrictAngle(lastY + difY*delta*smoothness)
lastY = lookY
for unit, rot in pairs(units) do
rot = restrictAngle(lookY - rot)
if math.sin(rot) > 0 then
local cosRot = math.cos(rot)
local cosRot2 = cosRot*cosRot
unit.Visible = true
unit.Position = UDim2.new(0.5 + cosRot*0.6, unit.Position.X.Offset, 0, 3)
unit.Size = UDim2.new(0, 10, 0.1, 0)
else
unit.Visible = false
end
end
end```