Search Unity

[SOLVED] Glitching Player

Discussion in 'Multiplayer' started by daawsomest, Sep 13, 2014.

  1. daawsomest

    daawsomest

    Joined:
    Dec 4, 2013
    Posts:
    8
    UPDATE: Solved by observing the NetworkPlayer script instead of the transform.

    Hello,

    I am using Photon to try and create a FPS game. I am having a problem where the other player is position below (under ground) and to the side of where he actually is. The glitching player will quickly move to where it should be, but then quickly glitches through the floor back into its original position.

    I must be something to do with the one of the following:
    • My NetworkPlayer script. (Updates position and disables components on remote players).
    • The way I am instantiating the player.
    • The settings in the PhotonView
    I am currently using the FPSInputController, CharacterMotor & MouseLook script from Unity's standard assets. I have wrote multiple versions of my own InputContoller and MouseLook scripts aswell as used scripts I found on the internet.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. /// <summary>
    5. /// Updates player positions on the network.
    6. /// </summary>
    7. public class NetworkPlayer : Photon.MonoBehaviour {
    8.  
    9.     public float lerpTime = 5.0f;
    10.     public GameObject cameraObject;
    11.  
    12.     private Vector3 realPosition = Vector3.zero;
    13.     private Quaternion realRotation = Quaternion.identity;
    14.  
    15.     void Awake()
    16.     {
    17.         if(photonView.isMine)
    18.         {
    19.             //Is local player, enable camera and movement input.
    20.             GetComponent<MouseLook>().enabled = true;
    21.             GetComponent<CharacterController>().enabled = true;
    22.             GetComponent<CharacterMotor>().enabled = true;
    23.             GetComponent<FPSInputController>().enabled = true;
    24.             cameraObject.SetActive(true);
    25.  
    26.         }
    27.         else
    28.         {
    29.             //Is remote player, disable camera and movement input.
    30.             GetComponent<MouseLook>().enabled = false;
    31.             GetComponent<CharacterController>().enabled = false;
    32.             GetComponent<CharacterMotor>().enabled = false;
    33.             GetComponent<FPSInputController>().enabled = false;
    34.             cameraObject.SetActive(false);
    35.         }
    36.     }
    37.  
    38.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    39.     {
    40.         if (stream.isWriting)
    41.         {
    42.             //Send position and rotation to others.
    43.             stream.SendNext(transform.position);
    44.             stream.SendNext(transform.rotation);
    45.         }
    46.         else
    47.         {
    48.             //Recieve position and rotation from others.
    49.             this.realPosition = (Vector3) stream.ReceiveNext();
    50.             this.realRotation = (Quaternion) stream.ReceiveNext();
    51.         }
    52.     }
    53.  
    54.     void Update()
    55.     {
    56.         if(!photonView.isMine)
    57.         {
    58.             //Update other players positions.
    59.             transform.position = Vector3.Lerp(transform.position, this.realPosition, Time.deltaTime * lerpTime);
    60.             transform.rotation = Quaternion.Lerp(transform.rotation,this. realRotation, Time.deltaTime * lerpTime);
    61.         }
    62.     }
    63. }
    64.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. /// <summary>
    5. /// Connects game to server and manages lobby.
    6. /// </summary>
    7. public class MultiplayerManager : MonoBehaviour {
    8.  
    9.     //Available lobby windows
    10.     enum Windows{None, Lobby, CreateRoom, Join}
    11.  
    12.     private Windows currentWindow = Windows.None; //Stores window which is currently open.
    13.  
    14.     private Rect lobbyRect = new Rect(0, 0, 500, 500);
    15.     private Rect createRoomRect = new Rect(0, 0,300, 200);
    16.     private Rect joinRect = new Rect(0, 0, 300, 200);
    17.     private Vector2 scrollPosition = Vector2.zero;
    18.  
    19.     private string roomName = "Room";
    20.     private int maxPlayers = 10;
    21.     private bool isVisible = true;
    22.  
    23.     void OnJoinedRoom()
    24.     {
    25.         //Spawn player.
    26.         GameObject player = PhotonNetwork.Instantiate("Player", new Vector3(0, 5, 15), Quaternion.identity, 0);
    27.      
    28.     }
    29.  
    30.     void Start()
    31.     {
    32.         //Initialize rectange sizes.
    33.         lobbyRect = new Rect(Screen.width / 2 - lobbyRect.width / 2, Screen.height / 2 - lobbyRect.height / 2,
    34.                              lobbyRect.width, lobbyRect.height);
    35.      
    36.         createRoomRect = new Rect(Screen.width / 2 - createRoomRect.width / 2, Screen.height / 2 - createRoomRect.height / 2,
    37.                                   createRoomRect.width, createRoomRect.height);
    38.      
    39.         joinRect = new Rect(Screen.width / 2 - joinRect.width / 2, Screen.height / 2 - joinRect.height / 2,
    40.                             joinRect.width, joinRect.height);
    41.  
    42.         //Connect to photon server.
    43.         PhotonNetwork.ConnectUsingSettings("0.1");
    44.  
    45.         //Display lobby.
    46.         currentWindow = Windows.Lobby;
    47.     }
    48.  
    49.     void OnGUI()
    50.     {
    51.         GUILayout.Label("Status: " + PhotonNetwork.connectionStateDetailed.ToString());
    52.         GUILayout.Label("Ping: " + PhotonNetwork.GetPing());
    53.      
    54.         if(PhotonNetwork.connectionStateDetailed == PeerState.JoinedLobby)
    55.         {
    56.             if(currentWindow == Windows.Lobby)
    57.             {
    58.                 lobbyRect = GUI.Window(0, lobbyRect, DrawLobby, "Lobby");
    59.             }
    60.          
    61.             else if(currentWindow == Windows.CreateRoom)
    62.             {
    63.                 createRoomRect = GUILayout.Window(1, createRoomRect, DrawCreateRoom, "Create Room");
    64.             }
    65.          
    66.             else if(currentWindow == Windows.Join)
    67.             {
    68.                 joinRect = GUILayout.Window(1, joinRect, DrawJoin, "Join");
    69.             }
    70.         }
    71.     }
    72.  
    73.     void DrawLobby(int windowID)
    74.     {
    75.         scrollPosition = GUILayout.BeginScrollView(scrollPosition);
    76.      
    77.         for (int i = 0; i < PhotonNetwork.GetRoomList().Length; i++)
    78.         {
    79.             if(GUILayout.Button(PhotonNetwork.GetRoomList()[i].name + " " + PhotonNetwork.GetRoomList()[i].playerCount + "/" + PhotonNetwork.GetRoomList()[i].maxPlayers))
    80.             {
    81.                 currentWindow = Windows.None;
    82.                 PhotonNetwork.JoinRoom(PhotonNetwork.GetRoomList()[i].name);
    83.             }
    84.         }
    85.      
    86.         GUILayout.EndScrollView();
    87.      
    88.         GUILayout.BeginHorizontal();
    89.         if(GUILayout.Button("Create Room"))
    90.         {
    91.             currentWindow = Windows.CreateRoom;
    92.         }
    93.      
    94.         if(GUILayout.Button("Join"))
    95.         {
    96.             currentWindow = Windows.Join;
    97.         }
    98.      
    99.         if(GUILayout.Button("Exit"))
    100.         {
    101.             print("Exit");
    102.             Application.Quit();
    103.         }
    104.         GUILayout.EndHorizontal();
    105.     }
    106.  
    107.     void DrawCreateRoom(int windowID)
    108.     {
    109.         //TODO: Need to check if room already exists and parsing
    110.         GUILayout.BeginVertical();
    111.         GUILayout.BeginHorizontal();
    112.         GUILayout.Label("Room Name:");
    113.         roomName = GUILayout.TextField(roomName);
    114.         GUILayout.EndHorizontal();
    115.      
    116.         GUILayout.BeginHorizontal();
    117.         GUILayout.Label("Max Players:");
    118.         maxPlayers = int.Parse(GUILayout.TextField(maxPlayers.ToString(), 2));
    119.      
    120.         GUILayout.Label("Visible:");
    121.         isVisible = GUILayout.Toggle(isVisible, "");
    122.      
    123.         GUILayout.EndHorizontal();
    124.      
    125.         GUILayout.BeginHorizontal();
    126.         if(GUILayout.Button("Cancel"))
    127.         {
    128.             currentWindow = Windows.Lobby;
    129.         }
    130.      
    131.         if(GUILayout.Button("Create"))
    132.         {
    133.             currentWindow = Windows.None;
    134.             PhotonNetwork.CreateRoom(roomName, isVisible, true, maxPlayers);
    135.         }
    136.         GUILayout.EndHorizontal();
    137.         GUILayout.EndVertical();
    138.     }
    139.  
    140.     void DrawJoin(int windowID)
    141.     {
    142.         GUILayout.BeginHorizontal();
    143.         GUILayout.Label("Room Name:");
    144.         roomName = GUILayout.TextField(roomName);
    145.         GUILayout.EndHorizontal();
    146.      
    147.         GUILayout.BeginHorizontal();
    148.         if(GUILayout.Button("Cancel"))
    149.         {
    150.             currentWindow = Windows.Lobby;
    151.         }
    152.      
    153.         if(GUILayout.Button("Join"))
    154.         {
    155.             currentWindow = Windows.None;
    156.             PhotonNetwork.JoinRoom(roomName);
    157.         }
    158.         GUILayout.EndHorizontal();
    159.     }
    160. }
    161.  


     
    Last edited: Sep 13, 2014