SHARE
TWEET

Help with Unet

a guest Jan 17th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5.  
  6. public class Client : NetworkBehaviour {
  7.     public GameObject PlayerObject;
  8.     public GameObject PlayerObjectPrefab;
  9.     public NetworkIdentity NI;
  10.  
  11.     void Start ()
  12.     {
  13.         if (!isLocalPlayer) return;
  14.         NI = GetComponent<NetworkIdentity>();
  15.     }
  16.     void Update () {
  17.         if (!isLocalPlayer) return;
  18.         if (Input.GetKeyDown(KeyCode.I))
  19.         {
  20.             SpawnPlayerObject();
  21.         }
  22.         if (PlayerObject != null)
  23.         {
  24.             if (Input.GetKeyDown(KeyCode.W))
  25.             {
  26.                 PlayerObject.transform.Translate(Vector3.forward * 3f);
  27.             }
  28.             if (Input.GetKeyDown(KeyCode.S))
  29.             {
  30.                 PlayerObject.transform.Translate(Vector3.back * 3f);
  31.             }
  32.             if (Input.GetKeyDown(KeyCode.A))
  33.             {
  34.                 PlayerObject.transform.Translate(Vector3.left * 3f);
  35.             }
  36.             if (Input.GetKeyDown(KeyCode.D))
  37.             {
  38.                 PlayerObject.transform.Translate(Vector3.right * 3f);
  39.             }
  40.         }
  41.     }
  42.     void SpawnPlayerObject()
  43.     {
  44.         if (PlayerObject != null) NetworkServer.Destroy(PlayerObject);
  45.         if (isServer)
  46.         {
  47.             PlayerObject = Instantiate(PlayerObjectPrefab);
  48.             NetworkIdentity ObjectIdentity = PlayerObject.GetComponent<NetworkIdentity>();
  49.             ObjectIdentity.localPlayerAuthority = true;
  50.             RpcSpawnPlayerObject(NI);
  51.         }
  52.         else
  53.         {
  54.             PlayerObject = Instantiate(PlayerObjectPrefab);
  55.             NetworkIdentity ObjectIdentity = PlayerObject.GetComponent<NetworkIdentity>();
  56.             ObjectIdentity.localPlayerAuthority = true;
  57.             CmdSpawnPlayerObject( NI);
  58.         }
  59.     }
  60.     [Command]
  61.     void CmdSpawnPlayerObject( NetworkIdentity _PlayerIdentity)
  62.     {
  63.         GameObject Pgo = NetworkServer.FindLocalObject(_PlayerIdentity.netId);
  64.         GameObject newplayerobject = Instantiate(PlayerObjectPrefab);
  65.         Pgo.GetComponent<Client>().PlayerObject = newplayerobject;
  66.         RpcSpawnPlayerObject(_PlayerIdentity);
  67.     }
  68.  
  69.     [ClientRpc]
  70.     void RpcSpawnPlayerObject( NetworkIdentity _PlayerIdentity)
  71.     {
  72.         GameObject Pgo = NetworkServer.FindLocalObject(_PlayerIdentity.netId);
  73.         if (NI != _PlayerIdentity)
  74.         {
  75.             GameObject newplayerobject = Instantiate(PlayerObjectPrefab);
  76.             Pgo.GetComponent<Client>().PlayerObject = newplayerobject;
  77.         }
  78.  
  79.     }
  80. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top