Search Unity

How do you effectively make another object your Local Player

Discussion in 'Multiplayer' started by GrantHuie, Aug 4, 2017.

  1. GrantHuie

    GrantHuie

    Joined:
    Jun 21, 2017
    Posts:
    6
    I set it up so you spawn in with a UI and you choose your team and class, once that is done I want you to be able to spawn with that team and class, however I need to make that prefab turn into my local player so I can effect other scripts on it saying only do this to the local player that of course being the prefab so this script will work.


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.Networking;
    5. public class Shoot : NetworkBehaviour {
    6.  
    7.     public int BulletSpeed = 300;
    8.     public GameObject ObjectToSpawn;
    9.     public GameObject SpawnPoint;
    10.     public GameObject Cam;
    11.     public GameObject Player;
    12.     public string nameTag;
    13.  
    14.     void Start() {
    15.         if (!isLocalPlayer) {
    16.             return;  
    17.         } else {
    18.             CmdStarter ();
    19.         }
    20.     }
    21.  
    22.     [Command]
    23.     void CmdStarter() {
    24.         Bullet.shooterName = "" + nameTag;
    25.     }
    26.  
    27.     private void Update() {
    28.         if (!isLocalPlayer) {
    29.             return;  
    30.         }
    31.         if (Input.GetKeyDown (KeyCode.Mouse0)) {
    32.             CmdSpawnMyCrap ();
    33.         }
    34.     }
    35.  
    36.     [Command]
    37.     void CmdSpawnMyCrap() {
    38.         GameObject objectSpawned = (GameObject) Instantiate (ObjectToSpawn);
    39.         objectSpawned.transform.position = SpawnPoint.transform.position;
    40.         objectSpawned.transform.Rotate (Cam.transform.rotation.x, Player.transform.rotation.y, 0f);
    41.         objectSpawned.GetComponent<Rigidbody> ().AddForce (Cam.transform.forward * BulletSpeed, ForceMode.Impulse);
    42.         NetworkServer.Spawn (objectSpawned);
    43.     }
    44.  
    45.  
    46. }
    47.  
     
    Last edited: Aug 4, 2017
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
  3. GrantHuie

    GrantHuie

    Joined:
    Jun 21, 2017
    Posts:
    6
    S*** thats dope, thanks man