Trying to build 2D compass

FYI - first post, total unity noob. I’ve put a few days behind trying to figure out how to get a 2D NSEW compass to work, and I’m really struggling. I figured that a clever way to do it would be to put an invis cube off the map, and have the compass always point at that as my “northern” point, but I can’t really figure out how to get the compass to spin and whatnot. I have seen a lot of talk about the horizontal bar type compass, but that’s not really what I was wanting to do. Any suggestions? I know some basic JS and C#, i’m just having an issue implementing. Thanks!

The best way is to use GUI.DrawTexture to draw the compass needle - the GUI system allows texture rotation:

var compass: Texture2D; // compass image
var needle: Texture2D;  // needle image (same size of compass)
var r = Rect(10, 10, 200, 200); // rect where to draw compass
var angle: float; // angle to rotate the needle

function OnGUI(){

    GUI.DrawTexture(r, compass); // draw the compass...
    var p = Vector2(r.x+r.width/2,r.y+r.height/2); // find the center
    var svMat: Matrix4x4 = GUI.matrix; // save gui matrix
    GUIUtility.RotateAroundPivot(angle,p); // prepare matrix to rotate
    GUI.DrawTexture(r,needle); // draw the needle rotated by angle
    GUI.matrix = svMat; // restore gui matrix
}

There are several ways to calculate the angle, but a general algorithm is the following: find the direction vector and zero its vertical component, find the FromToRotation from the north direction to the direction vector, and use ToAngleAxis to get the angle - for instance:

    var dirVector = destinationTransform.position - player.position;
    dirVector.y = 0; // remove the vertical component, if any
    var rot = Quaternion.FromToRotation(northVector, dirVector);
    var angle: float; // angle is what we want
    var axis: Vector3; // but an axis variable must be provided as well
    rot.ToAngleAxis(angle, axis); // get the angle

This will make the needle point to the destination object; if you want the needle to point the north, use the “invisible cube” you’ve mentioned as the destination object, and the player.forward direction as the northVector (northVector is the N direction of the compass body, while dirVector is the needle direction).