Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Math formula to rotate a gui based on where the camera is looking?

I've been trying to make a dynamic - map gui, which rotates based on where your camera is looking.

Getting where my camera is pointing in 3d space is easy, but I can't figure out the correct math formula to convert Vector3 to degrees/radians.

Any help would be most appreciated!

2 answers

✓ Accepted answer

This page should be helpful here.

Assuming you only care about the direction they're facing, and not whether their looking up or down, you only need to care about their Heading:

local sx, sy, sz, m00, m01, m02, m10, m11, m12, m20, m21, m22 = game.Players.LocalPlayer.Head.CFrame:components()
heading = math.atan2(m02, m22) --This is in radians!

game.Players.LocalPlayer.PlayerGui.ScreenGui.Minimap.Rotation = math.deg(heading)
--That line of course requires some modification, since a heading of `0` is pointing along the x-axis, which may not be what you want.

1 comment

0

Fantastic, thanks a lot adark.

Although this question already has a verified answer, I thought I would also give my solution for other users.

Rather than using the head, as demonstrated by adark, you can use the Camera object to get the direction as specified in the question, here's how:

local cameraLookVector = workspace.CurrentCamera.CoordinateFrame.lookVector
game.Players.LocalPlayer.PlayerGui.ScreenGui.Minimap.Rotation = math.deg(math.atan2(cameraLookVector.Y, cameraLookVector.X))

This will now make the Minimap rotate with the Camera, rather than just the Head while only needed one variable as oppose to 12! You may need to add -180 to 180 degrees onto this angle to properly orientate the Minimap.

5 comments

0

I tried something similar to this, but I must have made a mistake, Thanks!

0

You could just change the "game.Players.LocalPlayer.Head.CFrame:components()" to "game.Workspace.CurrentCamera.CoordinateFrame:components()" and it will work with the camera.

0

I suppose, however having all those variables can be quite a handful to understand and organise, I hoped my example gave another insight into achieving the desired effect.

0

Wait, in your code, I think the line should be 'workspace.CurrentCamera.CoordinateFrame.lookVector' unless it's a property I didn't know about. xD

0

Oops! You're right! It's all fixed now.

This discussion is preserved as it was. New answers, comments, and votes are closed.