Proper use of Camera WorldToScreenPoint

Hi. So i am trying to move a GUI element on a GUICanvas according to a position of the object in a world.

void UpdateTargetLockPosition(){
targetLockIndicator.transform.position = Camera.main.WorldToScreenPoint(target.transform.position);

When i look straight at the object - everything`s fine, but when i look away - it still on the screen, just moving in a reversed directions.

How can i give a restriction to this element movement or drawing? so i wont see it on the screen if i dont see the 3dobject ?

Thanks !

Be sure to format your pasted code with the 101010 button.

You could avoid displaying the object if the object is not in front of the camera. Take the dot product of the camera forward and a vector pointing from the camera to the object. If the dot product is less than zero, it’s “behind” the camera and you can stop drawing the object.

float dot = Vector3.Dot( (targ.position - cam.position).normalized, cam.forward );
if (dot <= 0) // don't draw ui target marker