- Home /
rotating minimap compass according to character direction
How would I script in Javascript to rotate a GUITexture using the OnGUI function according to which direction my character is facing? I have a North, south, east, west compass that I am trying to rotate on a minimap to show which direction the character is facing.
Answer by cjmarsh · Jan 20, 2011 at 04:19 AM
Something like this should do the trick: var playerDirection : Vector2 = ( 0, 1 ); var playerTransform : Transform; var deltaRotation : float = 0; var pivotPoint : Vector2; //pivotPoint is the center of your compass texture
function OnGUI() { deltaRotation = Vector2.Angle( playerDirection, Vector2( playerTransform.forward.x, playerTransform.forward.z ) ); GUIUtility.RotateAroundPivot( deltaRotation, pivotPoint ); playerDirection = Vector2( playerTransform.forward.x, playerTransform.forward.z ); //Plus whatever other code you need for the custom control }
It's not perfect as is, but with a little adaptation it should work for you.
when I set the vector2 variables at the beginning, it gives me three errors that lead me to believe that it thinks there should be no comma, and therefore, not have two numbers in the vector2 variable). Why is this?
It gives me these errors:
Assets/Scripts/$$anonymous$$apOpen.js(11,35): BCE0044: expecting ), found ','. (Filename: Assets/Scripts/$$anonymous$$apOpen.js Line: 11)
Assets/Scripts/$$anonymous$$apOpen.js(11,36): UCE0001: ';' expected. Insert a semicolon at the end. (Filename: Assets/Scripts/$$anonymous$$apOpen.js Line: 11)
Assets/Scripts/$$anonymous$$apOpen.js(11,37): BCE0044: expecting EOF, found '1'. (Filename: Assets/Scripts/$$anonymous$$apOpen.js Line: 11)
ok, i got it. apparently, you don't define vector2 variables like that, ins$$anonymous$$d you use "var playerDirection = Vector2 (0, 1)"
I now get this error after I start the game : The referenced script on this Behaviour is missing!
(Filename: ....\Runtime\$$anonymous$$ono\$$anonymous$$onoBehaviour.cpp Line: 1624)
I had some objects that were not in use, I deleted them, but I am using a different script. Thanks anyway.