I have written a script with the Photon Networking package, but I am having some problems.
Here is the script:
using UnityEngine;
using System.Collections;
public class NetworkManager : Photon.MonoBehaviour {
// Use this for initialization
void Start () {
PhotonNetwork.ConnectUsingSettings("alpha 0.1");
}
void OnGUI() {
GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString());
}
void OnJoinedLobby() {
PhotonNetwork.JoinRandomRoom();
}
void OnPhotonRandomJoinFailed() {
PhotonNetwork.CreateRoom(null);
}
void OnJoinedRoom() {
GameObject Soldier = PhotonNetwork.Instantiate( "fpscontroller", Vector3.one, Quaternion.identity, 0 );
}
}
I am using this script from the tutorial on how to make animations using Mecanim.
When I build the game and run it, when I use the WASD controls to move the character, it moves both of them and they glitch a lot, sometimes one flies high up in the air and starts falling below the terrain.
Help would be much appreciated. If there is anything you would like or need to know to answer this question, then please ask me in the comment section below.
The prefab you instantiate as player avatar most likely has the control script attached. Unless you de-activate this for avatars of remote players, any input you do will be sent to all of those scripts and move all avatars alike.
It might be brief, but the Marco Polo Tutorial for Photon Unity Networking shows (one way) how to solve this:
You will have the very same issue in both, PUN and Unity’s networking, as they work the same way.
It might also help to set the photon view setting “Observe Option” to unreliable. Also, what are you doing to smooth the messages? You will need to lerp between messages to make it look more smooth. Please note the “photonView.isMine”, this is how to prevent other instances from creating the interference tobiass is talking about.
void update() {
if (!photonView.isMine) {
transform.position = Vector3.Lerp(transform.position, this.net_CorrectPlayerPos, Time.deltaTime * 5);
transform.rotation = Quaternion.Lerp(transform.rotation, this.net_CorrectPlayerRot, Time.deltaTime * 5);
}
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting){
// We own this player: send the others our data
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
} else {
// Network player, receive data
this.net_CorrectPlayerPos = (Vector3)stream.ReceiveNext();
this.net_CorrectPlayerRot = (Quaternion)stream.ReceiveNext();
}
}