Advertisement
Munchy2007

Unet5TutPt3NetworkPlayerV4

Feb 16th, 2016
8,428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.06 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.Networking;
  4.  
  5. public class NetworkPlayer : NetworkBehaviour {
  6.     [SyncVar(hook = "OnPlayerIDChanged")] public string playerID;
  7.     Camera playerCam;
  8.     Transform labelHolder;
  9.  
  10.     void Awake()
  11.     {
  12.         playerCam = GetComponentInChildren<Camera>();
  13.         playerCam.gameObject.SetActive(false);
  14.         labelHolder = transform.Find("LabelHolder");
  15.     }
  16.  
  17.     [Command]
  18.     void CmdSetPlayerID(string newID)
  19.     {
  20.         playerID = newID;
  21.     }
  22.  
  23.     public override void OnStartLocalPlayer ()
  24.     {
  25.         string myPlayerID = string.Format("Player {0}", netId.Value);
  26.         CmdSetPlayerID(myPlayerID);
  27.         playerCam.gameObject.SetActive(true);
  28.     }
  29.  
  30.     public override void OnStartClient ()
  31.     {
  32.         OnPlayerIDChanged(playerID);
  33.     }
  34.  
  35.     void Update()
  36.     {
  37.         if(isLocalPlayer)
  38.         {
  39.             playerCam.transform.rotation = Quaternion.Euler(new Vector3(90,0,0));
  40.         }
  41.  
  42.         labelHolder.rotation = Quaternion.identity;
  43.     }
  44.  
  45.     void OnPlayerIDChanged(string newValue)
  46.     {
  47.         var textMesh = labelHolder.Find("Label").GetComponent<TextMesh>();
  48.         textMesh.text = newValue;
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement