Multiplayer Fps photon Issue

I am using photon for my multiplayer fps that I am currently working on and I am having trouble with when two players with cameras spawn in, they control each other. I have read a little about disabling a camera then re enabling it but that does not seem to be working for me. Any idea on what I should do? Thanks.

You need to check photonView.IsMine and when it’s false, disable the camera and movement controls for that object. An ideal place to check this is in the OnPhotonInstantiate callback.

Is there any way you could show me a quick example of how the code looks? Sorry im still new to the multiplayer thing. I appreciate it.

Here is my code for creating the player.

using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class GameSetupController : MonoBehaviourPunCallbacks
{
public Camera MainCamera;
SpawnSpot[ ] spawnSpots;
void Start()
{

spawnSpots = GameObject.FindObjectsOfType();
CreatePlayer();
}
private void CreatePlayer()
{
if (spawnSpots == null)
{
Debug.LogError("WHAT THE HECK ");
return;
}

SpawnSpot mySpawnSpot = spawnSpots[Random.Range (0, spawnSpots.Length)];
Debug.Log(“Creating Player”);

GameObject myPlayerGO = (GameObject) PhotonNetwork.Instantiate(Path.Combine(“PhotonPrefabs”, “PhotonPlayer”), mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0 );

myPlayerGO.GetComponent().enabled = true;
myPlayerGO.GetComponent().enabled = true;
myPlayerGO.GetComponentInChildren().enabled = true;
}

}

Why do you need the camera as part of the player itself? Just have 1 camera and attach it to the correct player object client side when it spawns in.

How exactly would I go about this? Thanks for the help/

You would put the camera in the scene instead of on the player prefab.

Then, on the player object, you would implement the OnPhotonInstantiate callback and in that method, you would query photonView.IsMine. If it is true locate the camera using FindObjectOfType and then parent it to the player game object. (You could use Camera.main instead of FindObjectOfType, if you’ve tagged it as such).

This method ensures there is only ever one camera in the scene and it is attached to the local player object.

1 Like