Search Unity

Multiplayer LAN: Multiple people control multiple characters.

Discussion in 'Multiplayer' started by The-Game-Master, Aug 31, 2014.

  1. The-Game-Master

    The-Game-Master

    Joined:
    Aug 6, 2014
    Posts:
    54
    I have the following code:
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Network1 : MonoBehaviour {
    4. public Transform PlayerPrefab;
    5. public string Map1 = "Cliffhanger";
    6. // Use this for initialization
    7. void Start () {
    8. }
    9. // Update is called once per frame
    10. void Update () {
    11. }
    12. void OnGUI() {
    13. if (Network.peerType == NetworkPeerType.Disconnected){
    14. GUI.Label (new Rect(400, 50, 240, 100), "Host settings");
    15. GUI.Label (new Rect(400, 440, 100, 40), "Map: " + Map1);
    16. string IP_Address = GUI.TextField (new Rect(10, 90, 300, 100), "IP Adress of host");
    17. GUI.Label(new Rect(10, 192, 100, 100), "Port of host is by default 25001.");
    18. if (GUI.Button (new Rect(10, 30, 120, 20), "Join a game")){
    19. Network.Connect("192.168.4.202", 25001);
    20. Debug.Log ("Joined a game!!!");
    21. }
    22. if (GUI.Button (new Rect(10, 50, 120, 20), "Host a game")){
    23. 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.");
    24. Network.InitializeServer(32, 25001, true);
    25. }
    26. if (GUI.Button (new Rect(340, 200, 120, 20), "Cliffhanger")){
    27. Map1 = "Cliffhanger";
    28. }
    29. if (GUI.Button (new Rect(340, 222, 120, 20), "Pitchfork")){
    30. Map1 = "Pitchfork";
    31. }
    32. else if (Network.peerType == NetworkPeerType.Client){
    33. GUI.Label(new Rect(10, 10, 300, 20), "Status: Connected as a Client");
    34. if (GUI.Button (new Rect(10, 30, 120, 20), "Leave lobby")){
    35. Network.Disconnect(200);
    36. }
    37. }
    38. }
    39. if (Network.isServer)
    40. {
    41. GUI.Label (new Rect (30, 30, 600, 1200), "Players connected:\n" + Network.connections);
    42. }
    43. }
    44. void OnServerInitialized(){
    45. Transform myTransform = (Transform)Network.Instantiate(PlayerPrefab, new Vector3(-130.1763f, 4, -458.5499f), transform.rotation, 0);
    46. }
    47. void OnConnectedToServer(){
    48. Transform myTransform = (Transform)Network.Instantiate(PlayerPrefab, new Vector3(-130.1763f, 4, -458.5499f), transform.rotation, 0);
    49. }
    50. }
    But what happens when the other person joins the game?
    We all have separate characters, but when they/I look around, it makes the other people 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.)
    I was told to use "if(NetworkView.isMine())", but where would I put that? I have the default FPS controller.
     
  2. Cha0os

    Cha0os

    Joined:
    Feb 22, 2014
    Posts:
    17
    This is exactly what you need to do. This if-statement needs to surround everything that only the "local" player is supposed to do, so every part of your script that deals with controlling the character.

    Just to clarify:

    Code (CSharp):
    1. void Update(){
    2.   transform.position = transform.position + Vector3.up * Time.deltaTime;
    3. }
    This code is executed on all clients.

    Code (CSharp):
    1. void Update(){
    2.   if(networkView.isMine){
    3.     transform.position = transform.position + Vector3.up * Time.deltaTime;
    4.   }
    5. }
    This code is only executed on the client that instantiated the object.
     
    The-Game-Master likes this.
  3. The-Game-Master

    The-Game-Master

    Joined:
    Aug 6, 2014
    Posts:
    54
    Awesome! Thanks! But if it's the default unity FPS controller, can I modify and redistribute it?