Multiplayer: Multiple people in-game, but they all control the same character.

I have the following code:

  • using UnityEngine;
  • using System.Collections;
  • public class Network1 : MonoBehaviour {
  • public Transform PlayerPrefab;
  • public string Map1 = “Cliffhanger”;
  • // Use this for initialization
  • void Start () {
  • }
  • // Update is called once per frame
  • void Update () {
  • }
  • void OnGUI() {
  • if (Network.peerType == NetworkPeerType.Disconnected){
  • GUI.Label (new Rect(400, 50, 240, 100), “Host settings”);
  • GUI.Label (new Rect(400, 440, 100, 40), "Map: " + Map1);
  • string IP_Address = GUI.TextField (new Rect(10, 90, 300, 100), “IP Adress of host”);
  • GUI.Label(new Rect(10, 192, 100, 100), “Port of host is by default 25001.”);
  • if (GUI.Button (new Rect(10, 30, 120, 20), “Join a game”)){
  • Network.Connect(“192.168.4.202”, 25001);
  • Debug.Log (“Joined a game!!!”);
  • }
  • if (GUI.Button (new Rect(10, 50, 120, 20), “Host a game”)){
  • GUI.Label (new Rect(270, 200, 100, 80), “Don’t worry if your game says it’s "Not Responding", as it is setting up the server. This process could take up to 30 seconds. Thank you for your patience.”);
  • Network.InitializeServer(32, 25001, true);
  • }
  • if (GUI.Button (new Rect(340, 200, 120, 20), “Cliffhanger”)){
  • Map1 = “Cliffhanger”;
  • }
  • if (GUI.Button (new Rect(340, 222, 120, 20), “Pitchfork”)){
  • Map1 = “Pitchfork”;
  • }
  • else if (Network.peerType == NetworkPeerType.Client){
  • GUI.Label(new Rect(10, 10, 300, 20), “Status: Connected as a Client”);
  • if (GUI.Button (new Rect(10, 30, 120, 20), “Leave lobby”)){
  • Network.Disconnect(200);
  • }
  • }
  • }
  • if (Network.isServer)
  • {
  • GUI.Label (new Rect (30, 30, 600, 1200), “Players connected:\n” + Network.connections);
  • }
  • }
  • void OnServerInitialized(){
  • Transform myTransform = (Transform)Network.Instantiate(PlayerPrefab, new Vector3(-130.1763f, 4, -458.5499f), transform.rotation, 0);
  • }
  • void OnConnectedToServer(){
  • Transform myTransform = (Transform)Network.Instantiate(PlayerPrefab, new Vector3(-130.1763f, 4, -458.5499f), transform.rotation, 0);
  • }
  • }

But what happens when the other person joins the game?
We all have seperate characters, but when they/I look around, it makes me look around as well. When they/I move, it glitches the characters and they all move. I don’t understand how to fix this, and I have been at this for 3 days now. (Edit: I do have the prefab and I don’t think that’s the issue, but I could be wrong.)

Hey,

I haven’t done any networking in Unity yet, but I’m pretty sure that you’ve to check whether the object is instanted by the user who’s currently moving, before you’re actually moving the object.

Maybe this helps:

Patte

Where would I put the “if (networkView.isMine)”? Which script would it go under? I’m using the included FPS controller.