- Home /
Horizontal scrolling style compass
How do I setup a horizontal scrolling style compass that turns with my player?
Answer by getyour411 · Sep 01, 2013 at 05:27 AM
I've seen a few requests for this and the other day came across a code snippet that gave me an idea on how to add this to my game. The result is below. There's probably a more efficient way to get some of that out of OnGUI(), if I find it or see suggestions for improvement I'll update this:
/// <summary>
/// Simple horizontal style compass for 3D world
/// Attach this to your Player. 0 degrees is North.
/// Adjust if your orientation is different by changing the letters.
/// Use/modify anyway you like.
/// </summary>
using UnityEngine;
using System.Collections;
public class QM_Compass : MonoBehaviour {
// Feel free to set these all to private once you are done tweaking:
private Transform myTransform;
public int facingDir;
public int degreeOffset;
void Start () {
myTransform = transform;
}
// Minor adjustments from 0,90,180,270 due to Font width
// Adjust letter offset to match your Font size/width
void OnGUI() {
if(degreeOffset > -85 && degreeOffset < 90) {
GUI.Label( new Rect((Screen.width/2)-degreeOffset*2,
(Screen.height)-50,
180,
25),
"N");
}
if(degreeOffset > 5 && degreeOffset < 180) {
GUI.Label( new Rect((Screen.width/2)-degreeOffset*2+180,
(Screen.height)-50,
180,
25),
"E");
}
if((facingDir > 95 && degreeOffset> 95) || (facingDir < 276 && degreeOffset < -90)) {
GUI.Label( new Rect((Screen.width/2)-facingDir*2+360,
(Screen.height)-50,
180,
25),
"S");
}
if((facingDir > 186 && degreeOffset < -5)) {
GUI.Label( new Rect((Screen.width/2)-facingDir*2+540,
(Screen.height)-50,
180,
25),
"W");
}
GUI.Box( new Rect((Screen.width/2)-180,
(Screen.height)-65,
360,
35),
"Heading");
}
void Update () {
facingDir = (int)Mathf.Abs(myTransform.eulerAngles.y);
if (facingDir > 360) facingDir = facingDir % 360;
degreeOffset = facingDir;
if(degreeOffset > 180) degreeOffset = degreeOffset - 360;
}
}
hi i am using your script right now but i would like to make the compass fit to any resolution how do i do that
Answer by DaiMangouDev · Sep 04, 2015 at 10:18 PM
This is really old but .
This can work for you , a paid tool. https://www.assetstore.unity3d.com/en/#!/content/33770
Answer by R0RAGS · Sep 14, 2017 at 07:02 PM
hi i am using your script right now but i would like to make the compass fit to any resolution how do i do that.