- Home /
Target Indicators Display On Two Sides
Ok, so I didn't really know how to word that question. Here is what's happening. I am using a very crude, but simple way to add target indicators to the game I'm working on. The problem is, while they display on the enemies, they are also displaying on the OPPOSITE side. Here are some images:
Here's facing front
And here's facing back
And here is the oh so complicated code:
using UnityEngine;
using System.Collections;
public class TargetIndicator : MonoBehaviour {
public Transform target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 wantedPos = Camera.main.WorldToViewportPoint(target.position);
transform.position = wantedPos;
}
}
I can't seem to figure out why there are two sets of indicators. Oh also, I should mention the GUI texture is a child of each enemy with the code displayed above attached to the Texture. It feels very crude and slammed together, but it's really simple and works fro what I need, if I could keep it that way, then it'd be great.
The theory that I have as to why it's doing this, is because the script is attached to the texture, which is attached to the enemy. Is it possible that the enemy is displaying the same texture, just with the coordinates inverted?
Anyhow, both sets of indicators move, and as the actual enemies pass by me, the other set of indicators passes by as well, heading in the opposite direction of the enemies. It's pretty strange.
Any and all help is deeply appreciated.
Answer by robertbu · May 06, 2014 at 10:33 PM
If I understand you correctly, this will fix your issue:
void Update () {
Vector3 wantedPos = Camera.main.WorldToViewportPoint(target.position);
transform.position = wantedPos;
renderer.enabled = wantedPosition.z > 0.0f;
}
That is, when doing a WorldToViewportPoint() the returned 'z' value indicates the distance in front of the camera. Negative values are behind the camera, and positive values are in front of the camera. So this code turns off the renderer if the the position is behind the camera.
Close, but no cigar. This requires that there be a renderer attached to the object. I am simply using a GUI Texture. I can try playing around with it, maybe add a sprite renderer and mess with that, see if it will fix the issue, but if there is anything else you can think of, I'd love to hear.
EDIT Fixed it, just had to make a slight change. Thanks so much robert :)
if you could update your post to display the correct answer, or should I say the $$anonymous$$ORE correct answer lol. Ins$$anonymous$$d of using
renderer.enabled = worldPos.z > 0.0f;
I used:
this.GetComponent<GUITexture>().enabled = worldPos.z > 0.0f;
Because there was no renderer attached to the object, it was just a straight GUITexture