Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Players stop moving once they get connoted in same scene with Photon

Discussion in 'VR' started by JD_FVTC, Sep 28, 2020.

  1. JD_FVTC

    JD_FVTC

    Joined:
    Oct 20, 2017
    Posts:
    54
    2 players get connected but once connected in "Room for 2" scene they no longer move nor do their hands move or update.

    What might the issue be? Do I need a photon View on the hands?
    Why would my movement be stopped once loaded into the "Room for 2" scene?

    This is step by step what happens

    Player One Load game.
    Player one can move and hands move with controllers.
    OvrPlayerController default GO is used. I don't instantiate a player prefab for loading screen
    Player one clicks Play
    Room for 1 scene is created.
    Player one Instantiates - Player Prefab
    PlayerManagerMultiPlayer - Adds <MyOvrPlayerController> to prefab if photonview.ismine.
    Player 1 can move and hands update position

    Player 2 loads game
    Player 2 can move and hands move with controllers.
    Player 2 clicks Play.
    Room for 2 scene is created
    Player 2 Instantiates
    PlayerManagerMultiPlayer - Adds <MyOvrPlayerController> to prefab if photonview.ismine.
    Player 2 Cant Move
    Player 2 hands don't update or move
    Player 2 Can see player 1 when he gets loaded.

    Player 1 gets loaded into Room for 2 scene
    Player 1 Can no Longer Move
    Player 1 hands no longer move.
    Player 1 Can see Player 2







    Code (csharp):
    1. using System;
    2. using System.Collections;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using Photon.Pun;
    6. using Photon.Realtime;
    7. namespace edu.fvtc.IptoBinary
    8. {
    9.     public class GameManager : MonoBehaviourPunCallbacks
    10.     {
    11.         #region Photon Callbacks
    12.         /// <summary>
    13.         /// Called when the local player left the room. We need to load the launcher scene.
    14.         /// </summary>
    15.         public override void OnLeftRoom()
    16.         {
    17.             SceneManager.LoadScene(0);
    18.         }
    19.         #endregion
    20.         #region Public Methods
    21.         public static GameManager Instance;
    22.         public GameObject playerPrefab;
    23.         public void LeaveRoom()
    24.         {
    25.             PhotonNetwork.LeaveRoom();
    26.         }
    27.         #endregion
    28.         #region Private Methods
    29.         void Start()
    30.         {
    31.             Instance = this;
    32.             if (playerPrefab == null)
    33.             { // #Tip Never assume public properties of Components are filled up properly, always check and inform the developer of it.
    34.                 Debug.LogError("<Color=Red><b>Missing</b></Color> playerPrefab Reference. Please set it up in GameObject 'Game Manager'", this);
    35.             }
    36.             else
    37.             {
    38.                 if (PlayerManagerMultiPlayer.LocalPlayerInstance == null)
    39.                 {
    40.                     Debug.LogFormat("We are Instantiating LocalPlayer from {0}", SceneManagerHelper.ActiveSceneName);
    41.                     var xx = UnityEngine.Random.Range(-3, 3);
    42.                     var zz = UnityEngine.Random.Range(-3, 3);
    43.                     PhotonNetwork.Instantiate(this.playerPrefab.name, new Vector3(xx, 1f, zz), Quaternion.identity, 0);
    44.                 }
    45.                 else
    46.                 {
    47.                     Debug.LogFormat("Ignoring scene load for {0}", SceneManagerHelper.ActiveSceneName);
    48.                 }
    49.             }
    50.         }
    51.         void LoadArena()
    52.         {
    53.             if (!PhotonNetwork.IsMasterClient)
    54.             {
    55.                 Debug.LogError("PhotonNetwork : Trying to Load a level but we are not the master Client");
    56.             }
    57.             Debug.LogFormat("PhotonNetwork : Loading Level : {0}", PhotonNetwork.CurrentRoom.PlayerCount);
    58.             PhotonNetwork.LoadLevel("Room for " + PhotonNetwork.CurrentRoom.PlayerCount);
    59.         }
    60.         #endregion
    61.         #region Photon Callbacks
    62.         public override void OnPlayerEnteredRoom(Player other)
    63.         {
    64.             Debug.LogFormat("OnPlayerEnteredRoom() {0}", other.NickName); // not seen if you're the player connecting
    65.             if (PhotonNetwork.IsMasterClient)
    66.             {
    67.                 Debug.LogFormat("OnPlayerEnteredRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient); // called before OnPlayerLeftRoom
    68.                 LoadArena();
    69.             }
    70.         }
    71.         public override void OnPlayerLeftRoom(Player other)
    72.         {
    73.             Debug.LogFormat("OnPlayerLeftRoom() {0}", other.NickName); // seen when other disconnects
    74.             if (PhotonNetwork.IsMasterClient)
    75.             {
    76.                 Debug.LogFormat("OnPlayerLeftRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient); // called before OnPlayerLeftRoom
    77.                 LoadArena();
    78.             }
    79.         }
    80.         #endregion
    81.     }
    82. }
    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3. using Photon.Pun;
    4. using System.Collections;
    5. namespace edu.fvtc.IptoBinary
    6. {
    7.     /// <summary>
    8.     /// Player manager.
    9.     /// Handles fire Input and Beams.
    10.     /// </summary>
    11.     public class PlayerManagerMultiPlayer : MonoBehaviourPunCallbacks, IPunObservable
    12.     {
    13.         #region IPunObservable implementation
    14.         [Tooltip("The local player instance. Use this to know if the local player is represented in the Scene")]
    15.         public static GameObject LocalPlayerInstance;
    16.         public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    17.         {
    18.             if (stream.IsWriting)
    19.             {
    20.                 // We own this player: send the others our data
    21.                 stream.SendNext(IsFiring);
    22.             }
    23.             else
    24.             {
    25.                 // Network player, receive data
    26.                 this.IsFiring = (bool)stream.ReceiveNext();
    27.             }
    28.         }
    29.         #endregion
    30.         [Tooltip("The current Health of our player")]
    31.         public float Health = 1f;
    32.         public GameObject mycolorcube;
    33.         #region Private Fields
    34.         [Tooltip("The Beams GameObject to control")]
    35.         [SerializeField]
    36.         private GameObject beams;
    37.         //True, when the user is firing
    38.         bool IsFiring;
    39. #if UNITY_5_4_OR_NEWER
    40.         void OnSceneLoaded(UnityEngine.SceneManagement.Scene scene, UnityEngine.SceneManagement.LoadSceneMode loadingMode)
    41.         {
    42.             this.CalledOnLevelWasLoaded(scene.buildIndex);
    43.         }
    44. #endif
    45.         #endregion
    46.         #region MonoBehaviour CallBacks
    47.         /// <summary>
    48.         /// MonoBehaviour method called on GameObject by Unity during early initialization phase.
    49.         /// </summary>
    50.         void Awake()
    51.         {
    52.             if (beams == null)
    53.             {
    54.                 Debug.LogError("<Color=Red><a>Missing</a></Color> Beams Reference.", this);
    55.             }
    56.             else
    57.             {
    58.                 beams.SetActive(false);
    59.             }
    60.             // #Important
    61.             // used in GameManager.cs: we keep track of the localPlayer instance to prevent instantiation when levels are synchronized
    62.             if (photonView.IsMine)
    63.             {
    64.                 PlayerManagerMultiPlayer.LocalPlayerInstance = this.gameObject;
    65.             }
    66.             // #Critical
    67.             // we flag as don't destroy on load so that instance survives level synchronization, thus giving a seamless experience when levels load.
    68.             DontDestroyOnLoad(this.gameObject);
    69.         }
    70.         void Start()
    71.         {
    72.             if (photonView.IsMine)
    73.             {
    74.                 gameObject.AddComponent<MyOvrPlayerController>();
    75.                 mycolorcube.GetComponent<Renderer>().material.color = Color.green;
    76.             }
    77. #if UNITY_5_4_OR_NEWER
    78.             // Unity 5.4 has a new scene management. register a method to call CalledOnLevelWasLoaded.
    79.             UnityEngine.SceneManagement.SceneManager.sceneLoaded += OnSceneLoaded;
    80. #endif
    81.         }
    82.         /// <summary>
    83.         /// MonoBehaviour method called on GameObject by Unity on every frame.
    84.         /// </summary>
    85.         void Update()
    86.         {
    87.             if (photonView.IsMine)
    88.             {
    89.                 if (Health <= 0f)
    90.                 {
    91.                     GameManager.Instance.LeaveRoom();
    92.                 }
    93.             }
    94.             //Changed this Josh
    95.             if (photonView.IsMine)
    96.             {
    97.                 ProcessInputs();
    98.             }
    99.             // trigger Beams active state
    100.             if (photonView.IsMine && beams != null && IsFiring != beams.activeInHierarchy)
    101.             {
    102.                 beams.SetActive(IsFiring);
    103.             }
    104.         }
    105.         /// <summary>
    106.         /// MonoBehaviour method called when the Collider 'other' enters the trigger.
    107.         /// Affect Health of the Player if the collider is a beam
    108.         /// Note: when jumping and firing at the same, you'll find that the player's own beam intersects with itself
    109.         /// One could move the collider further away to prevent this or check if the beam belongs to the player.
    110.         /// </summary>
    111.         void OnTriggerEnter(Collider other)
    112.         {
    113.             if (!photonView.IsMine)
    114.             {
    115.                 return;
    116.             }
    117.             // We are only interested in Beamers
    118.             // we should be using tags but for the sake of distribution, let's simply check by name.
    119.             if (!other.name.Contains("Beam"))
    120.             {
    121.                 return;
    122.             }
    123.             Health -= 0.1f;
    124.         }
    125.         /// <summary>
    126.         /// MonoBehaviour method called once per frame for every Collider 'other' that is touching the trigger.
    127.         /// We're going to affect health while the beams are touching the player
    128.         /// </summary>
    129.         /// <param name="other">Other.</param>
    130.         void OnTriggerStay(Collider other)
    131.         {
    132.             // we dont' do anything if we are not the local player.
    133.             if (!photonView.IsMine)
    134.             {
    135.                 return;
    136.             }
    137.             // We are only interested in Beamers
    138.             // we should be using tags but for the sake of distribution, let's simply check by name.
    139.             if (!other.name.Contains("Beam"))
    140.             {
    141.                 return;
    142.             }
    143.             // we slowly affect health when beam is constantly hitting us, so player has to move to prevent death.
    144.             Health -= 0.1f * Time.deltaTime;
    145.         }
    146. #if !UNITY_5_4_OR_NEWER
    147. /// <summary>See CalledOnLevelWasLoaded. Outdated in Unity 5.4.</summary>
    148. void OnLevelWasLoaded(int level)
    149. {
    150.     this.CalledOnLevelWasLoaded(level);
    151. }
    152. #endif
    153.         void CalledOnLevelWasLoaded(int level)
    154.         {
    155.             // check if we are outside the Arena and if it's the case, spawn around the center of the arena in a safe zone
    156.             if (!Physics.Raycast(transform.position, -Vector3.up, 5f))
    157.             {
    158.                 transform.position = new Vector3(0f, 5f, 0f);
    159.             }
    160.         }
    161. #if UNITY_5_4_OR_NEWER
    162.         public override void OnDisable()
    163.         {
    164.             // Always call the base to remove callbacks
    165.             base.OnDisable();
    166.             UnityEngine.SceneManagement.SceneManager.sceneLoaded -= OnSceneLoaded;
    167.         }
    168. #endif
    169.         #endregion
    170.         #region Custom
    171.         /// <summary>
    172.         /// Processes the inputs. Maintain a flag representing when the user is pressing Fire.
    173.         /// </summary>
    174.         void ProcessInputs()
    175.         {
    176.             if (OVRInput.Get(OVRInput.Axis1D.SecondaryIndexTrigger) > .1)
    177.             {
    178.                 if (!IsFiring)
    179.                 {
    180.                     IsFiring = true;
    181.                 }
    182.             }
    183.             if (OVRInput.Get(OVRInput.Axis1D.SecondaryIndexTrigger) < .1)
    184.             {
    185.                 if (IsFiring)
    186.                 {
    187.                     IsFiring = false;
    188.                 }
    189.             }
    190.         }
    191.         #endregion
    192.     }
    193.     class MyOvrPlayerController : OVRPlayerController
    194.     {
    195.     }
    196. }
    197.  
     
  2. Psajmon_unity

    Psajmon_unity

    Joined:
    Oct 18, 2019
    Posts:
    10
    I use photon in vr and it works fine. Don't know if it's correct way to do it but I do not instantiate ovrplayercontroller in photon, only avatars that have photon views on desired objects(hands and head). Multiple OVRPlayerControllers in scene? Not best idea... multiple cameras and more...