- Home /
Compass directions (N, E, S, W)
Hey there,
is there a way to get the directions North, East, South and West depending where the player is looking at?
I tried it with the rotation but then noticed that it changes if the player moves, of course.
Kindest Regards, Matt
You could put an empty gameObject at your "North" and math-ify from that? I put together this but on rereading your question I'm not sure it helps since you don't want the player's "facing" direction:
using UnityEngine;
using System.Collections;
public class FacingDir : $$anonymous$$onoBehaviour {
private Transform myTransform;
private float facingDir;
void Start () {
myTransform = transform;
}
void Update () {
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.F)) {
facingDir = $$anonymous$$athf.Abs(myTransform.eulerAngles.y);
if (facingDir > 360) facingDir = facingDir % 360;
Debug.Log ("Facing: " + facingDir + " degree");
}
}
}
The facingDir, ins$$anonymous$$d of going to debug.log, could be piped into something else that approximates a direction (i.e anything near 0 is North, near 90 is East, near 180 is South, near 270 is West, etc)
Hey guys, me again. Well, as you can see the directions are working well and I tried using four GUITexts to store the N,E,S,W letters. (They change if the player looks into another direction).
But as you can see on the $$anonymous$$imap is that the GUITexts are not only rendered on the $$anonymous$$ainCamera but also on the $$anonymous$$inimap which uses an extra camera.
Do you know a good resolution?
$$anonymous$$indest Regards, $$anonymous$$att
You want to use Layers and the layer mask on the camera:
http://docs.unity3d.com/Documentation/Components/Layers.html
Note when asking a new questions that is technically unrelated to the original one, please open a new question. It keeps UA cleaner as a repository of information, and it likely will get you a better answer. Fewer people look at already answered/older questions.
Thanks. Worked good and is very easy to use.
Okay I will keep that in $$anonymous$$d ;)
Answer by robertbu · Aug 25, 2013 at 07:10 PM
Asssuming your character is on the XZ plane with positive 'Z' as north:
#pragma strict
function Update () {
var v = transform.forward;
v.y = 0;
v.Normalize();
if (Vector3.Angle(v, Vector3.forward) <= 45.0) {
Debug.Log("North");
}
else if (Vector3.Angle(v, Vector3.right) <= 45.0) {
Debug.Log("East");
}
else if (Vector3.Angle(v, Vector3.back) <= 45.0) {
Debug.Log("South");
}
else {
Debug.Log("West");
}
}
Depending your your use, there are other, perhaps more efficient ways.