I want to create a compass for navigation that players can hold and drop.
I don’t know how to use for relative orientation to rotate the needle to point accurately to the north location. I don’t want it to break when rotating the y axis of the compass itself.
I’ve tried to use math.atan2 but can’t get it to work well. When using * cframe.angles it just rotates endlessly, when trying to use cframe.lookAt I can get no where.
[This image shows the rotation of the base part of the needle]
If theres a simpler way that is more efficient I will gladly attempt to do it.
2 Likes
Retrieve the player’s character and its root part (or the camera if you prefer). Then, get the player’s forward direction using the root part’s (or the camera’s) orientation. Use trigonometry to calculate the angle between the player’s forward direction and the north direction. Rotate the needle based on the calculated angle to ensure it points north. Finally, put this in a loop, and you are done.
(let me know if you need further help)
Example code:
local needle = script.Parent
local camera = workspace.CurrentCamera
local function updateCompass()
local orientation = camera.CFrame.LookVector
local angle = math.atan2(orientation.X, orientation.Z)
needle.Rotation = math.deg(angle)
end
game:GetService("RunService").RenderStepped:Connect(updateCompass)