I was trying to make a multiplayer game using Photon-PUN, but there is an issue which I am facing.
Player1[the player which created the room] instantiated in the game then Player2.
The problem is that only Player2 can see Player1, Player1 can’t see Player2
You can see that both the players are looking at each other but only Player2[which joined later] can see Player1 but Player1 can’t see Player2
I am also encountering this warning error in the console whenever I move the Player2(player in Build).
Received OnSerialization for view ID 2001. We have no such PhotonView! Ignore this if you're joining or leaving a room. State: Joined
Source Code
NetworkManager (It connect to the server and create lobby and load lobby)
using UnityEngine;
using Photon.Pun;
using UnityEngine.SceneManagement;
public class NetworkManager : MonoBehaviourPunCallbacks
{
void Start()
{
Connect();
}
void Update()
{
}
public void Connect(){
PhotonNetwork.ConnectUsingSettings();
if(PhotonNetwork.IsConnected){
Debug.Log("Server Connected");
}
}
public override void OnConnectedToMaster(){
PhotonNetwork.JoinLobby();
}
public override void OnJoinedLobby()
{
SceneManager.LoadScene(1);
}
}
RoomManager (It create room via createInput field and join room via joinInput and load the game scene)
using Photon.Pun;
using UnityEngine;
using TMPro;
public class RoomManager : MonoBehaviourPunCallbacks
{
[SerializeField] private TMP_InputField createInput;
[SerializeField] private TMP_InputField joinInput;
void Awake(){
PhotonNetwork.AutomaticallySyncScene = true;
}
public void CreateRoom(){
PhotonNetwork.CreateRoom(createInput.text);
}
public void JoinRoom(){
PhotonNetwork.JoinRoom(joinInput.text);
}
public override void OnJoinedRoom()
{
PhotonNetwork.LoadLevel(2);
}
}
GameManager (This instantiate the player)
using UnityEngine;
using Photon.Pun;
public class GameManager : MonoBehaviourPunCallbacks
{
[SerializeField] private GameObject player;
[SerializeField] private float minX, maxX, minY, maxY;
void Start(){
Vector2 randomPosition = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
PhotonNetwork.Instantiate(player.name, randomPosition, Quaternion.identity);
}
}
PlayerController (This control the player movement)
using UnityEngine;
using Photon.Pun;
public class PlayerController : MonoBehaviourPunCallbacks
{
public float speed;
public float rotSpeed;
void Start()
{
if(!photonView.IsMine){
GetComponentInChildren<Camera>().enabled = false;
GetComponentInChildren<AudioListener>().enabled = false;
}
}
void Update()
{
if(photonView.IsMine){
if(Input.GetKey(KeyCode.W)){
gameObject.transform.position += transform.forward * speed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.S)){
gameObject.transform.position += -transform.forward * speed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.A)){
gameObject.transform.Rotate(0, -rotSpeed * Time.deltaTime, 0);
}
if(Input.GetKey(KeyCode.D)){
gameObject.transform.Rotate(0, rotSpeed * Time.deltaTime, 0);
}
}
}
}