- Home /
How to make a 2D Horizontal Compass?
Hi guys.
I'm trying to make a compass system similar to the compass the player sees later in Half-Life 2: Episode 2, which is the horizontal bar at the bottom of the screen with the texture that scrolls as the player looks around. I've been spending all day trying to figure out how to replicate this in Unity to no solution.
Does anybody know how to make this?
Answer by celadon · Mar 15, 2011 at 12:13 AM
I did one that is like this, but it is 3d - I made a cylinder, wrapped it with a texture, stuck it way down below the terrain with a camera aimed at it that was set to clip everything except the cylinder's layer.
There is a very simple script on the cylinder that rotates it in tandem with the main camera.
The end result is a rectangular strip where the cardinal directions scroll left or right as the main camera turns.
here's the script: function Start () { mainCamera = GameObject.FindWithTag("MainCamera"); }
function Update () { normRot = Mathf.Abs(mainCamera.transform.eulerAngles.y); if (normRot > 360) { normRot = normRot % 360; } transform.eulerAngles.y = normRot - 180; // because we look at the "back"
}
Hey, That exactly what i needed.. but when im adding the script to my scene im getting an error msg : unknown identifier: 'mainCamera' my camera that looking at my compass called 'CompassCamera' what exactly do i need to change in the script so it'll work?
cheers..
The 2 variables need to be defined before the start function. and you should tag your "$$anonymous$$ain Camera" as "$$anonymous$$ainCamera".
var mainCamera : GameObject; <----------var1
var normRot : float; <-----------var2
function Start () {
mainCamera = GameObject.FindWithTag("$$anonymous$$ainCamera");
}
function Update () {
normRot = $$anonymous$$athf.Abs(mainCamera.transform.eulerAngles.y);
if (normRot > 360) {
normRot = normRot % 360;
}
transform.eulerAngles.y = normRot - 180; // because we look at the "back"
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613112838im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer
dynamic GUI Texture.. clipping and rotation HUD 1 Answer
script to create gui when detection collision between cube and first controler person ?? 1 Answer
WorldToScreenPoint returns the same value when facing the opposite direction. 0 Answers