Name display problem

I’m using the Photon Viking Demo (from the Asset Store) and a Name display script I found online. The script is doing as it should, but the only problem is that the name of other players does not stay fixed on top of them. I suspect this is an issue with the Camera.main… in the code but I am not sure how to fix it.

Here is the code:

  void OnGUI() {
    	Vector3 offset = new Vector3(0, 1, 0); // height above the target position
    		
        Vector3 point = Camera.main.WorldToScreenPoint(transform.position + offset);
    	point.y = Screen.height - point.y;
    		
    	GUI.Label (new Rect (point.x - 15, point.y + 35, 200, 20), GetComponent<PhotonView> ().owner.name);
    	}

And here is a pic of what is going on (The 2nd player “Nick” is directly behind “John” but his name is still displayed on screen):

alt text

WorldToScreenPoint() projects the point on the screen regardless if the world position is in front or behind the camera. To fix, you can check the ‘z’ of the converted position. If it is negative, it is behind the camera, and therefore don’t print the name:

if (point.z > 0.0f) {
        GUI.Label (new Rect (point.x - 15, point.y + 35, 200, 20), GetComponent<PhotonView> ().owner.name);
}