Object in the scene with a collider, photonview.ismine only true for the master client

Options
I have a cube within my scene with the below Fire script attached. The object also has a photon view component.

The photonView.isMine is always false, unless you trigger the collider with the master client / the first to connect to the room.

The cube is set as a scene object. Do I need to move that functionality into an RPC call to .All?
    public class Fire : Photon.MonoBehaviour
    {
        private SphereCollider _sphereCollider;
        private float _heatIncrement = 10f;

        void Start()
        {
            _sphereCollider = GetComponent<SphereCollider>();
            if (_sphereCollider == null) throw new MissingComponentException("Sphere collider missing");
        }

        private void OnTriggerStay(Collider other)
        {
            if (other.tag != "Player") return;
            if (!photonView.isMine) return;

            PlayerNetwork player = other.GetComponent<PlayerNetwork>();
            if (player == null) throw new MissingComponentException("Player missing Player script.");
            player.IncreaseTemp(_heatIncrement * Time.deltaTime);
        }
    }

Comments

  • Hi @ManxJason,

    Unity's Collision or Trigger functions will be called on any clients if they are detecting a collision.
    PhotonView.isMine is always true for the client, who is the owner of the object. It will be false for anyone else.

    If you only want to do something with the player whenever a collision is detected, I wouldn't add the collision detection to the object, but would add it to the player logic instead. This way the player can apply the modification directly and synchronize it with the other clients (if that is necessary).
  • ManxJason
    ManxJason
    edited February 2018
    Options
    Thanks Christian, I didn't think to change the collision detection to the network player.


    Thanks again.
  • ManxJason
    Options
    @Christian_Simon For educational purposes, can you point me in the direction of some help regarding scene objects and collisions. Lets say the scene object was going to do more than just update the player. Like explode, for all clients AND update something on the player.
  • If it is a scene object, it is normally controlled / owned by the MasterClient. So you basically can use the collision detection on the MasterClient to do something with the object on the one hand and something different to the other object on the other hand.

    An example: a player crashes into the object. On the MasterClient you can use the OnCollisionEnter or OnTriggerEnter (select the one which fits best to your setup) function. From this function, you can for example use a RPC call on the object in order to 'explode' it on any client before removing it later by using PhotonNetwork.Destroy(...). In the collision detection functions you also have the 'other' object. If this is a player, you can use the object's PhotonView component in order to send another RPC call which for example lowers the player's health. Please note, that you handle such scenarios on just one client in order to avoid desynchronization of the game. This can be for example the MasterClient (as described before) or the player who detects the collision.
  • ManxJason
    Options
    That's much appreciated, and makes sense. Thanks