Indipendent views for a scene object

Hello everyone, I am using a scene object that glows and opens a dialogue box when a player comes near it. I would like to make the glow and the dialogue box visible only for the local player. Is there a way to use the PhotonView component to achieve this result (possibly avoiding to call RPCs) ?
Here below I attach a snippet of code to show how I manage things

/////////////////////////////////////////////////////////////////////////
private void OnTriggerEnter(Collider other)
{


if (other.tag == "Player")
{

playerCounter++;
highlight.SetActive(true);
dialogueBox.SetActive(true);

}

}

private void OnTriggerExit(Collider other)
{


if (other.tag == "Player" )
{

playerCounter--;
if (playerCounter == 0)
{
highlight.SetActive(false);
dialogueBox.SetActive(false);
}
}

}

Comments

  • If its a scene object, it will send the call to all players in the scene when OnTriggerEnter / Exit occurs.
    should be as simple as checking if the photonView is they'res:
    private void OnTriggerEnter(Collider other)
    {
    if(!photonView.IsMine) return;
    
    if (other.CompareTag("Player"))
    {
    
    playerCounter++;
    highlight.SetActive(true);
    dialogueBox.SetActive(true);
    
    }
    
    }
    
    private void OnTriggerExit(Collider other)
    {
    if(!photonView.IsMine) return;
    
    if (other.CompareTag("Player"))
    {
    
    playerCounter--;
    if (playerCounter == 0)
    {
    highlight.SetActive(false);
    dialogueBox.SetActive(false);
    }
    }
    
    }
    
  • @LeeroiJenkiins Thank you for your reply! I have already tried this solution, but it doesn't work as I would like. In this case only the master client sees the activation of the object and all the others client do not see it.
  • LeeroiJenkiins
    edited December 2020
    Hmm, well in that case my other solution to you is to do the following:
    create a script and attach it to your player character named (Multiplayer_UIManager)
    have it extend MonoBehaviourPun instead of MonoBehaviour.

    -create a method in it to display on for that user in the Multiplayer_UIManager:
    public void DisplayPlayerUI(bool active)
        {
            if (!photonView.IsMine) return;
    
            highlight.SetActive(active);
            dialogueBox.SetActive(active);
        }
    
    and in your NPC script change it to:
    private void OnTriggerEnter(Collider other)
        {
            if (other.CompareTag("Player"))
            {
                other.GetComponent<Multiplayer_UIManager>.DisplayPlayerUI(true);
            }
        }
    
        private void OnTriggerExit(Collider other)
        { 
            if (other.CompareTag("Player"))
            {
                other.GetComponent<Multiplayer_UIManager>.DisplayPlayerUI(false);
            }
        }
    
    The difference here is the players client sets the UI on not a PhotonNetwork.RoomObject, and there is no way for a different client to active theirs when you check on that specific client for if (!photonView.IsMine) return;

    This is how I personally like to deal with trigger enters / exits for players so it should work for you too :)
  • @LeeroiJenkiins Thank you very much ! That solved my issue!
  • @fra97p anytime friend, glad I could help :)