Currently, i have this compass that points north. But I wan’t to make it point to a part instead.
How would i achieve this?
current code:
HumanoidRootPart = script.Parent.Parent.Parent:FindFirstChild("HumanoidRootPart")
while true do
wait(0)
if HumanoidRootPart then
script.Parent.Parent.ArrowPart.SurfaceGui.Needle.Rotation = -HumanoidRootPart.Orientation.Y
end
end
I spent quite a while getting this to work, so hopefully it works in your case.
local HumanoidRootPart = script.Parent.Parent.Parent:FindFirstChild("HumanoidRootPart")
-- ^ this is the part that it will point to ^
local ArrowPart = script.Parent.Parent
while wait() do
if HumanoidRootPart then
local pos = ArrowPart.Position - HumanoidRootPart.Position
ArrowPart.SurfaceGui.Needle.Rotation = 90 - math.deg(math.atan(pos.X / pos.Z))
end
end
Gradient is the speed at which a slope rises per increment of the x axis.
Gradient is calculated as (y2 - y1) / (x2 - x1)
Arctan gets the angle from a gradient.
Arctan is calculated as x / math.sqrt(1 + x ^ 2) (x being the gradient)
Turning your character doesn’t affect the compass, and sometimes the arrow will randomly flip
And if you wan't to see the edited code
RS.RenderStepped:Connect(function()
if Equipped and CompassGoal.Value and HumanoidRootPart then
local pos = HumanoidRootPart.Position - CompassGoal.Value.Position
ArrowPart.SurfaceGui.Needle.Rotation = 180 - math.deg(math.atan(pos.X / pos.Z))
elseif CompassGoal.Value == nil then
warn("CompassGoal is nil or missing")
end
end)
The reason it doesn’t turn when your player turns is because your using the HumanoidRootPart and the main part of the compass.
It also appears to be switching around the 90/270 deg mark, try removing the 180 - .
First off, the compass needle is relative to your HumanoidRootPart, you would want to switch that to the main part of the compass (so that the needles rotation is relative to the compass and not the player).
Try the following to fix the weird north/south glitch
RS.RenderStepped:Connect(function()
if Equipped and CompassGoal.Value and HumanoidRootPart then
local pos = HumanoidRootPart.Position - CompassGoal.Value.Position
local rot = -math.deg(math.atan2(pos.X, pos.Z)) + 90
if rot < 0 then
rot = rot + 360
end
ArrowPart.SurfaceGui.Needle.Rotation = rot
elseif CompassGoal.Value == nil then
warn("CompassGoal is nil or missing")
end
end)
local RunService = game:GetService("RunService")
local PointPart = workspace:WaitForChild("PointPart") --the part where you want it to point
local Needle = Compass.Needle --The needle
RunService.Heartbeat:Connect(function()
if PointPart then
Needle.CFrame = CFrame.new(Needle.CFrame.p, PointPart.CFrame.p * Vector3.new(1, 0, 1))
end
end)
``` I tested it and it was quite smooth