Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
hiccup111 231 Moderation Voter
8 years ago

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

Log in to vote
3
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
8 years ago

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.
0
Fantastic, thanks a lot adark. hiccup111 231 — 8y
Ad
Log in to vote
2
Answered by
Rhyles 80
8 years ago

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.

0
I tried something similar to this, but I must have made a mistake, Thanks! hiccup111 231 — 8y
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. Tkdriverx 514 — 8y
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. Rhyles 80 — 8y
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 Tkdriverx 514 — 8y
0
Oops! You're right! It's all fixed now. Rhyles 80 — 8y

Answer this question