- Home /
how to check if an object is facing an other
hello everyone, I am trying to do a targeting system that targets an object when you click on it and then it adds a GUI image to signal its location. its all working great yet there is a problem where the GUI texture appears both on the target and in its opposite direction (so if the GUI texture centered when you are facing an objetc, when you turn 180 deg you will see the GUI texture). I tried the following to fix the problem yet it did not work:
if (target != null){
GUI.Box(new Rect(10, 10, 150, 30), "TARGET:" + target.name);
GUI.Box(new Rect(10, 50, 150, 30), "DIST. TO TARGET:" + distToTarget);
//here is where it draws the texture(what is in the if statement is what i tried)
if ((target.position.x - transform.position.x) * Mathf.Sin(transform.eulerAngles.y) > 0 ||
(target.position.y - transform.position.y) * Mathf.Cos(transform.eulerAngles.y) > 0)
GUI.DrawTexture(new Rect(targetScreenPos.x - imageScale.x / 2, Screen.height - targetScreenPos.y - imageScale.x / 2
, imageScale.x, imageScale.y), guiImageTexture, ScaleMode.ScaleToFit, true);
}
so long storry short, all i got is the GUI texture to blink. thanks a lot in advanced.
Answer by robertbu · Nov 09, 2014 at 03:16 PM
Try this:
Vector3 dir = target.position - transform.position;
if (Vector3.Dot(dir, transform.forward) > 0.0f) {
GUI.DrawTexture(new Rect(targetScreenPos.x - imageScale.x / 2, Screen.height - targetScreenPos.y - imageScale.x / 2, imageScale.x, imageScale.y), guiImageTexture, ScaleMode.ScaleToFit, true);
}
Answer by The-Game-Master · Nov 09, 2014 at 04:24 PM
Try using a raycast. it should be something like this: if(Physics.raycast(Vector3.forward)){ }
I'm not sure that this is exactly how it goes, but you could try it. you could ask around. Then you'd need to check the GameObject Tag and you'd be fine.
Your answer
![](https://koobas.hobune.stream/wayback/20220613171046im_/https://answers.unity.com/themes/thub/images/avi.jpg)