Search Unity

Third Party Multiplayer Fps photon Issue

Discussion in 'Multiplayer' started by wrolin, May 16, 2020.

  1. wrolin

    wrolin

    Joined:
    Oct 6, 2018
    Posts:
    4
    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.
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    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.
     
  3. wrolin

    wrolin

    Joined:
    Oct 6, 2018
    Posts:
    4
    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.
     
  4. wrolin

    wrolin

    Joined:
    Oct 6, 2018
    Posts:
    4
    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<SpawnSpot>();
    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<PlayerMovement>().enabled = true;
    myPlayerGO.GetComponent<Movement>().enabled = true;
    myPlayerGO.GetComponentInChildren<Camera>().enabled = true;
    }


    }
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
  6. wrolin

    wrolin

    Joined:
    Oct 6, 2018
    Posts:
    4
    How exactly would I go about this? Thanks for the help/
     
  7. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    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<Camera> 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.
     
    tobiass likes this.