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

How Can I Fix This Problem With GUI Rotation?

Asked by
8391ice 91
8 years ago

I am trying to create a radar GUI for the player to use while he or she is navigating. The radar has an icon on it that is supposed to represent the player and it is shaped like an arrow. The arrow is supposed to point in whatever direction that the player is facing.

I have created a script that does this, but with a little problem; when the icon rotates, after it hits about 90 degrees, it'll rotate in the backwards direction. I noticed that when the player is rotating, the Rotation property of any part can only reach 180 degrees while the Rotation property of the GUI can reach 360 degrees. This probably has to do with why the GUI icon does this.

How can I change this script to make it so that the GUI icon continues to rotate past the 90 degree limit and properly conforms to the player's rotation?

while true do
    r = script.Parent.Parent.Parent.Parent.Parent.Parent.Character.Torso.Rotation.Y
    wait(.01)
    script.Parent.Rotation = -r --I made this negative because when I tested it without the negative, the icon rotated backwards to the player's rotation.
end

1 answer

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

Instead of using the Y component of the rotation, you should use the heading of the character. To calculate this all you need to do is take the arc tangent of the x and z components of the Torso's lookVector. This will give you the heading in radians so you'll have to convert to degrees.

local direction = part1.CFrame.lookVector
local heading = math.atan2(direction.x, direction.z)
heading = math.deg(heading)

Now you have the angle, all you have to do is apply it to the GUI.

0
Thank you very much, this worked beautifully! I'd never have been able to come up with this seeing as how I am not experienced with lookVector. 8391ice 91 — 8y
0
No problem :) BlackJPI 2658 — 8y
Ad

Answer this question