- Home /
Show Photon username based on Distance
I'm working on an online FPS using PUN+. One of the things in the game is a GUI Label hovering over a player. What I'm trying to do is make it so that when the local player comes within range of another player, the nametag turns on. And obviously, when he backs away, it turns back off.
I was able to get this working with Vector3.Distance
, but I have code for when one player kills another player. When the player that was just killed respawns, his nametag doesn't show when you get close to him. Is there an easy way to fix this?
I don't want to use an RPC for this because I'm trying to keep the amount of messages sent at a minimum and instead use the PhotonView to sync names.
Any help would be much appreciated.
Here's my code for the nametag:
void OnGUI()
{
GUI.skin = skin;
PhotonPlayer ownerOfName = this.photonView.owner;
if (showNameText) //Is the nametag showing?
{
Vector3 offset = new Vector3(0, 2, 0);
Vector3 point = Camera.main.WorldToScreenPoint(transform.position + offset);
point.y = Screen.height - point.y;
if (!dead) //Is the player alive?
{
GUIStyle colorText = new GUIStyle(skin.GetStyle("Label"));
if (PhotonNetwork.player.GetTeam() != ownerOfName.GetTeam() || PhotonNetwork.room.customProperties["gm"].Equals("Free for All") ||
PhotonNetwork.room.customProperties["gm"].Equals("Night Hunt")) //If we're not on the same team or we're in Free-for-All
{
colorText.normal.textColor = Color.red; //Then this guy is our enemy
}
else //If he IS on our side
{
colorText.normal.textColor = Color.green; //Then he's with us
}
//Ignore this part
if (ownerOfName.customProperties["vip"].Equals("Yes"))
{
GUI.Label(new Rect(point.x - 35, point.y - 1, 200, 30), ownerOfName.name + " (VIP)", colorText);
}
else
{
GUI.Label(new Rect(point.x - 35, point.y - 1, 200, 30), ownerOfName.name, colorText); //Show the player's name
}
}
else //If the player is dead
{
GUI.Label(new Rect(point.x - 35, point.y - 1, 200, 30), ownerOfName.name + " -dead-", "label");
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Problem with PhotonView.RPC 1 Answer
Photon networking doesn't sync 0 Answers
Photon Networking Instantiating Problem 0 Answers