- Home /
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):
Answer by robertbu · Jul 28, 2014 at 10:17 PM
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);
}
That works! Thank you. However, the more I move away from the 2nd player, the more his name starts to drop down, see here:
Any ideas for that?
Your problem is in how you calc your Rect for your GUI.Label call. To make this code work consistently, you want to anchor the GUI.Label in the bottom middle. Currently you create a Rect that is 20 high, but your code add (moves down) 35. As the world size of the player shrinks, the problem is magnified, bringing the text down. So to start, change the 35 to 20 and then adjust your 'offset' assignment to correctly place the text.
But a real fix is a bit more complicated. You want to size the Rect to the height of the text, not to an arbitrary 20 pixels. For a given display, you can just play with the Rect height until you find the value that works, but for arbitrary displays, you want to dynamically calculate the height. I don't use GUI for my displays, but I believe you want to use GUI.skin.label.CalcSize() to find the height and width of the name, and use that value for the width and height of the Rect.
I am a little confused and unsure of what to put here: GUI.skin(?).CalcSize(?);
and after I have the correct values, should I make the above a variable and replace the Rect's height and width values?
Can't help with the 3d coordinates-to-world conversion but you could use a 3d text with an offset and the character as parent. See: http://hastebin.com/givijomoto.cs
Your answer
Follow this Question
Related Questions
Display only for one player 2 Answers
Problem with display healthbar and playername above player 0 Answers
(C#) Help with Multiplayer Features - Photon Unity Networking 0 Answers
Photon networking; how to check what position/number the player spawned 1 Answer
Problems running client with Photon code on other computers 0 Answers