Search Unity

Third Party [PHOTON] Trying to have separate health bars on the canvas for two players.

Discussion in 'Multiplayer' started by bamcd0313, Dec 12, 2023.

  1. bamcd0313

    bamcd0313

    Joined:
    May 15, 2022
    Posts:
    1
    Hello! I am trying to make it so both players can see each other's health bars, but they can only see their own. I have a health bar prefab with a PhotonView component that is being spawned on a GameCanvas that is an overlay. It has this script on it
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using TMPro;
    5. using UnityEngine;
    6. using UnityEngine.UI;
    7.  
    8. public class HealthBar : Photon.MonoBehaviour
    9. {
    10.  
    11.     //this is a sldier!
    12.     [SerializeField] Slider healthBar;
    13.     [SerializeField] TextMeshProUGUI username;
    14.     public string usernameStr;
    15.     Ship refrenceShip;
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         if (photonView.isMine)
    20.         {
    21.             GameObject.Find("ShipManager").GetComponent<ShipManager>().OnActiveShipChanged += OnNewShip;
    22.             healthBar = GetComponentInChildren<Slider>();
    23.         }
    24.     }
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         if (photonView.isMine)
    29.         {
    30.             if (refrenceShip == null)
    31.             {
    32.                 refrenceShip = GameObject.Find("ShipManager").GetComponent<ShipManager>().activeShip.GetComponentInParent<Ship>();
    33.                 if (refrenceShip == null)
    34.                 {
    35.                     return;
    36.                 }
    37.                 healthBar.maxValue = refrenceShip.maxHealth;
    38.                 healthBar.value = refrenceShip.currentHealth;
    39.             }
    40.             username.text = usernameStr;
    41.             healthBar.value = refrenceShip.currentHealth;
    42.             this.gameObject.transform.position = Camera.main.WorldToScreenPoint(refrenceShip.transform.position + new Vector3(0, 0.5f, 0));
    43.             username.text = PhotonNetwork.playerName;
    44.         }
    45.     }
    46.  
    47.     void OnNewShip()
    48.     {
    49.         if (photonView.isMine)
    50.         {
    51.             //the refrence ship is the parent of the health bar
    52.             refrenceShip = GameObject.Find("ShipManager").GetComponent<ShipManager>().activeShip.GetComponentInParent<Ship>();
    53.             healthBar.maxValue = refrenceShip.maxHealth;
    54.             healthBar.value = refrenceShip.currentHealth;
    55.          
    56.         }
    57.  
    58.     }
    59. }
    60.  
    Here is an image.
    upload_2023-12-12_8-16-37.png upload_2023-12-12_8-16-24.png
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    When you run scripts on the networked game objects, you have to pay attention which parts you want to run on any such objects (e.g. showing and updating a health bar) and which parts only should only be used if the local player controls the object (e.g. making a character react to input locally).

    I think in this case, your IsMine checks are not needed.

    The PUN Basics Tutorial shows how to implement health bars for the players.