- Home /
Target Indicators Display On Both Sides
Hi everyone,
My main goal is simply to make a target indicator that will be shown in the UI and keep tracking the enemy off screen (not while in front of the camera).
the problem is that whenever I look the opposite direction, the indicator shows up at the wrong position with -1 values (example: if the target located 2f lower than the player at normal direction, it’ll show up 2f higher than the player at the opposite direction).
I pretty much understand why it’s happening but can’t figure out how to deal with it, my best solution so far was to turn the indicator on/off whenever my player would cross the 90 angle with the target or looking at the target.
What I need is for the player to be able to follow the indicator at the opposite direction to the correct position with the normal values.
My Code:
public Transform target;
public Image DeImage;
void Update() {
Vector3 targetDir = transform.position - target.position;
float angel = Vector3.Angle(transform.forward, targetDir);
DeImage.enabled = Mathf.Abs(angel) > 100 && Mathf.Abs(angel) < 130;
if (Mathf.Abs(angel) > 100 && Mathf.Abs(angel) < 130) //temporary solution, to be removed
{
Vector3 viewPosition = Camera.main.WorldToScreenPoint(target.position);
DeImage.transform.position = Vector3.MoveTowards(DeImage.transform.position, viewPosition, 100);
DeImage.transform.localPosition = new Vector3(Mathf.Clamp(DeImage.transform.localPosition.x, -910, 910), Mathf.Clamp(DeImage.transform.localPosition.y, -490, 490), 0);
}
}