- Home /
Compass Bar GUI Help!
I need some help with making a compass gui. Not like the standard circular GUI though. One that scrolls and looks like this: (It's Horrible, sorry. Hopefully you get what I mean)
Anyways, I've tried to do different ways and it never works. Can anyone help please? :)
Thank you!
Answer by robertbu · Jul 25, 2014 at 05:37 AM
Here is a quick example. First you need to use this texture:
It takes the angle from a target object. It is currently setup so that Vector3.forward is north making Vector3.right east, Vector3.left west, and Vector3.back south.
#pragma strict
var target : Transform;
var compassTex : Texture;
private var displayRect : Rect;
private var texRect : Rect = Rect(0,0,3.0/8.0, 1.0);
var factor = 1.0;
function Start() {
displayRect = Rect(Screen.width / 2 - 400, 10, 300, 32);
}
function OnGUI() {
var angle = Mathf.Atan2(target.right.z, target.right.x) * Mathf.Rad2Deg;
texRect.x = 5.0 / 16.0 + angle / 90.0 / 8.0;
GUI.DrawTextureWithTexCoords(displayRect, compassTex, texRect);
}
I designed the long graphic so that the code did not depend on the graphic wrapping. If you are sure that the devices you are going to use support wrap mode for images, then you could significantly shorten this image. Plus I probably made it longer than necessary even for a non-wrapping solution.
This is a quick example with code that is highly dependent on the graphic I've used. It could easily be generalized, but I leave that to you.
You are looking for more solutions to this problem in your other question. This concept (essentially sliding the material offset and the material scale) can be done with a world space object ins$$anonymous$$d of using GUI.DrawTextureWithTexCoords(). You can use $$anonymous$$aterial.mainTextureOffset and $$anonymous$$aterial.mainTextureScale. And for a different look, you could implement a cylinder with directions on the side probably viewed using an Orthographic camera. The cylinder would be rotated to match the camera direction. You could also do it using text output (GUI.Label, GUIText or 3D Text) and modifying the position and visibility of the text based on the rotation. I think the last would be a bit more complicated than what I've done above.
Answer by jmgek · Jul 25, 2014 at 01:53 AM
Just have a plane with that width and height and put that exact texture on it.
That doesn't make it animated though. I want it to move like a compass. That picture was just an example :D
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Gui Text Script 4 Answers
GUI Button Disappearing 1 Answer
GUI.Box NullReferenceException 0 Answers