- Home /
Nametags Above Player
Hello. I am trying to work a way to put nametags above players from a string. This then needs to be synced to all players can see it over the network. How would I do that? I'm assuming it's by RPC calls but I am very uncertain how they work at this point. All answers I've found has been outdated with the Network view.
Thank You! - TwoTen
I managed to find this thread wich is very similar. It's just very outdated. http://answers.unity3d.com/questions/311549/need-help-with-creating-nicks-in-multiplayer.html
Answer by Munchy2007 · Jun 18, 2016 at 03:17 PM
My UNET tutorial covers this, amongst other things
http://www.doofah.com/tutorials/networking/unity-5-network-tutorial-part-3/
Thank you very much. I will have a look. I'll accept the answer whenever I have checked it out fully :)
np, if you need any help with it feel free to ask.
So I tried it out and it only shows your own nametag on your own character. It doesn't seem to actually show the other players names.
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class NameTag : NetworkBehaviour
{
[SyncVar(hook = "OnPlayerIDChanged")]
public string playerID;
public Camera playerCam;
public Transform labelHolder;
void Awake()
{
// playerCam = GetComponentInChildren<Camera>();
playerCam.gameObject.SetActive(false);
}
[Command]
void CmdSetPlayerID(string newID)
{
playerID = newID;
}
public override void OnStartLocalPlayer()
{
string myPlayerID = string.Format(UserSession.username);
CmdSetPlayerID(myPlayerID);
playerCam.gameObject.SetActive(true);
}
public override void OnStartClient()
{
OnPlayerIDChanged(playerID);
}
void Update()
{
if (isLocalPlayer)
{
playerCam.transform.rotation = Quaternion.Euler(new Vector3(90, 0, 0));
}
labelHolder.rotation = Quaternion.identity;
}
void OnPlayerIDChanged(string newValue)
{
var text$$anonymous$$esh = labelHolder.Find("Label").GetComponent<Text$$anonymous$$esh>();
text$$anonymous$$esh.text = newValue;
}
}
Thats my code currently. Usersession.username grabs the users username.
I've dropped your code onto the player prefab in my tutorial project (just changed the line where it assigns the name, as I don't have your usersession code), and it works fine.
So it might be something to do with the way you are obtaining the usersession name on remote clients that is causing an error, have you run the game in the editor in both host and client roles (not at the same time obviously) to see if you get any runtime errors?
No errors are being spat out. BUT. When I made the edit client I noticed that my own player ID field is empty in the inspector and the other player (host)'s player ID is actually his correct name just the fact that its not displaying. @$$anonymous$$unchy2007 EDIT: And as host. I can see all players name. In the Player ID field but it does not render. EDIT 2: Found the reason for it not rendering. The LabelCamera gameobject gets disabled on the non local Player. How would i fix it? Edit 3: it's just me being a retard xD. playerCam.gameObject.SetActive(false); That was the problem.
So. After fixing that little issue all is working. Thank you very much and I have to say your site is very useful. I tagged you in a question of $$anonymous$$e. If you have a second please check it out. It's about damage over the network I need help with. Nobody seems to know the answer.
The playerCam, as it's used in my tutorial is a third person camera that follows the player, so it should be disabled on all except the local player.
I'm not sure how you've constructed your player prefab, but you shouldn't need a camera specifically for the label.