I have a code that is supposed to act as a compass display in my game the displays either a north, south, east, or west GameObject when the camera is facing a certain way. Here’s the script…
var player : GameObject;
var north : GameObject;
var south : GameObject;
var east : GameObject;
var west : GameObject;
function Update () {
if(player.transform.rotation <= 45 && player.transform.rotation => -45)
{
north.SetActive (true);
south.SetActive (false);
east.SetActive (false);
west.SetActive (false);
}
if(player.transform.rotation <= -45 && player.transform.rotation => -135)
{
north.SetActive (false);
south.SetActive (false);
east.SetActive (false);
west.SetActive (true);
}
if(player.transform.rotation <= -135 && player.transform.rotation => 135)
{
north.SetActive (false);
south.SetActive (true);
east.SetActive (false);
west.SetActive (false);
}
if(player.transform.rotation <= 135 && player.transform.rotation => 45)
{
north.SetActive (false);
south.SetActive (false);
east.SetActive (true);
west.SetActive (false);
}
}
The errors I get look like bracket errors, but I’ve just been learning rotation functions, and have been corrected before. I would show the errors, but there are quite a lot. Help if you can, and if you’d like to see the errors, I’ll put them up.
Edit: Your => should be >=. This will make lots of errors go away. => is for declaration of lambda functions. >= is for greater than or equal to comparisons.
Transform.rotation is a 4D quaternion, which doesn’t use degrees, and has XYZW elements. It’s not a single number, but should not be used unless you understand quaternions anyway. Theoretically you could use Transform.eulerAngles, but since there’s more than one correct way to represent a given 3D rotation, reading a single element is not advisable since the results are unpredictable. In general, track rotation yourself, and treat transform rotation stuff as write-only.
they errors are because you’ve typo’d “rotaion” on line 17, but what Eric is explaining is still going to be a problem
consider
//...
if(Vector3.Dot(transform.forward, Vector3.forward) > 0.8f)
{
//... pointing in the same direction as Vector3.forward (to some tolerance, change the number for that)
off the top of my head I can’t remember which of the Vector3.forward/right/-forward/-right would be “north/south/east/west” but you get the idea.
You’re using “Transform” (i.e. the class) when you should be using “transform” (i.e. the reference to the current gameobject’s transform) on 12, 20 and 28… i’m not sure whether to call 36 “half right” or “all wrong” given that you’ve used both there…
Okay, thanks. I tried it and It sort of works. East appears when I look in that direction, and North appears when I look in that direction, but when I look South or West, it displays North. I know it has to have something to do with how my < and > are set up, but I’m not sure what to change
var player : GameObject;
var north : GameObject;
var south : GameObject;
var east : GameObject;
var west : GameObject;
var yRotation : float = 5.0;
function Update () {
yRotation += Input.GetAxis("Horizontal");
if(player.transform.eulerAngles.y <= 45 && player.transform.eulerAngles.y > -45)
{
north.SetActive (true);
south.SetActive (false);
east.SetActive (false);
west.SetActive (false);
}
if(player.transform.eulerAngles.y <= -45 && player.transform.eularAngles.y > -135)
{
north.SetActive (false);
south.SetActive (false);
east.SetActive (false);
west.SetActive (true);
}
if(player.transform.eulerAngles.y <= -135 && player.transform.eulerAngles.y > 135)
{
north.SetActive (false);
south.SetActive (true);
east.SetActive (false);
west.SetActive (false);
}
if(player.transform.eulerAngles.y <= 135 && player.transform.eulerAngles.y > 45)
{
north.SetActive (false);
south.SetActive (false);
east.SetActive (true);
west.SetActive (false);
}
}