Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Networking and name above avatar.

Discussion in 'Multiplayer' started by Antonios, Feb 27, 2009.

  1. Antonios

    Antonios

    Joined:
    Jul 28, 2008
    Posts:
    107
    I have used the examples in the networking samples and built a simple project where others are joining. Apart from the prefab of my character I also added in the prefab a 3d text to appear above my chars head like so many games do. However until now I either have the name appear for my char by I don't see it above the other players heads either all the client avatars are having the same name as me. So how I make the names appear correctly? What am I not sending through the code from the networking example, because stream serialize doesn't support string. Any help would be much appreciated.
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    you can convert the string into a format supported by the stream -> you write it as series of byte :)
     
  3. Antonios

    Antonios

    Joined:
    Jul 28, 2008
    Posts:
    107
    So its only a matter of passing the text through the NetworkInterpolatedTransform.cs script then? Too simple hmmm... :D
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    A simple RPC that is run in the setup function (like the onnetwork create or alike) would work as well.
    i would not push it through something that is meant to run regularily
     
  5. Antonios

    Antonios

    Joined:
    Jul 28, 2008
    Posts:
    107
    I did use an rpc into the setup script attached onto the 3d text object which was setting the textmesh to the name of the avatar but I was getting the same name appearing for everyone e.g. I had the name Antonios and I saw the name Antonios above all the chars.
     
  6. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Sounds like you likely don't send the RPC to all other clients then or that you call the RPC BEFORE you actually set the name.
    Alternatively you didn't cache the call or when you don't intend to do that, you forgot to react in your new player connected script on the client accordingly by sending out the rpc.
    The name thats streamed by default is the name that you set in the editor

    That depends on your setup and how you handle it.

    I assume you worked with different machines right?


    the alternative approach is another serialization pipe which handles stuff like name and other general setup for example. set the transfer there to delta compressed and you are all fine as it will only send the data once and thats it.
     
  7. Antonios

    Antonios

    Joined:
    Jul 28, 2008
    Posts:
    107
    Yes worked on different machines. Will give a try once more or go with the serialization. Thanks for the tips and the help.
     
  8. blue2fox

    blue2fox

    Joined:
    Jun 8, 2008
    Posts:
    91
    Hi Antonios. I took this script directly from my game, I hope it will help. You can probably modify it so that it will work for you. Just like you, I added a TextMesh above my characters, then I added this script to my CharacterController. I haven't really taken the time to explain the script, but I'm sure you can figure it out. Good luck.


    Code (csharp):
    1.  
    2. var nameText : TextMesh;
    3.  
    4. private var playerName : String;
    5. private var playerOldName = "";
    6. private var nameTime = 0.0;
    7. private var team : String;
    8. private var nametextTeam : String;
    9. private var camTeam : String;
    10.  
    11.  
    12. function Start()
    13. {
    14.     var raceSelectionObject = GameObject.Find("Race_Selection_Data");
    15.     var raceSelectionData = raceSelectionObject.GetComponent(Selection_Info);
    16.     playerName = raceSelectionData.SendPlayerName();
    17.     team = raceSelectionData.SendSide();
    18.     nameTime =Time.time +1.0;
    19. }
    20.  
    21. function Update()
    22. {
    23.  
    24.     var cam = GameObject.Find("Camera");
    25.    
    26.     playerName = GetComponent(Player_Stats).playerName;
    27.    
    28.     if (playerName != playerOldName  Time.time > nameTime  playerName != null)
    29.     {
    30.         networkView.RPC("DisplayPlayerName", RPCMode.All, playerName);
    31.         playerOldName = playerName;
    32.         nameTime = Time.time + 1.0;
    33.     }
    34.    
    35.    
    36.     if (nameText != null)
    37.     {
    38.         if ((networkView.isMine || gameObject.tag != cam.tag)  nameText != null)
    39.         {
    40.             nameText.renderer.enabled = false;
    41.         }
    42.         else
    43.         {  
    44.             nameText.transform.LookAt(cam.transform);
    45.         }
    46.     }
    47.  
    48. }
    49.  
    50.  
    51. @RPC
    52. function DisplayPlayerName(playerName : String)
    53. {
    54.     nameText.text = playerName;
    55. }
    56.  
    57.  
     
  9. Antonios

    Antonios

    Joined:
    Jul 28, 2008
    Posts:
    107
    Thank you for the help and the code. Much appreciated.