Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Photon Multiplayer | Health Synchronisation Problem

Discussion in 'Formats & External Tools' started by cirazgames, Nov 17, 2021.

  1. cirazgames

    cirazgames

    Joined:
    Apr 13, 2021
    Posts:
    11
    Can anyone help me with an issue, regarding to synchronize the health of a character?
    My issue / problem is that when I rejoin with my second device, the health of the enemy is again at 100%, even though, on my first device that didn´t rejoin, the health is lower than 100%.


    Here is the code of the HealthSystem Script, that I use. (It´s from a tutorial by CodeMonkey. Please watch it, if it helps you understand the working of it better, in order to help me.
    )

    Code (CSharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4. public class HealthSystem
    5. {
    6.     public static HealthSystem healthSystem;
    7.  
    8.  
    9.     public event EventHandler OnHealthChanged;
    10.  
    11.     private int health;
    12.     [HideInInspector]public int healthMax;
    13.  
    14.    
    15.  
    16.     public HealthSystem(int healthMax)
    17.     {
    18.         this.healthMax = healthMax;
    19.         health = healthMax;
    20.     }
    21.  
    22.     public int GetHealth()
    23.     {
    24.         return health;
    25.     }
    26.  
    27.     public float GetHealthPercentage()
    28.     {
    29.         return (float)health / healthMax;
    30.     }
    31.  
    32.     public void Damage(int damageAmount)
    33.     {
    34.         health -= damageAmount;
    35.         if (health < 0) health = 0;
    36.         if (OnHealthChanged != null) OnHealthChanged(this, EventArgs.Empty);
    37.     }
    38.  
    39.     public void Heal(int healAmount)
    40.     {
    41.         health += healAmount;
    42.         if (health > healthMax) health = healthMax;
    43.         if (OnHealthChanged != null) OnHealthChanged(this, EventArgs.Empty);
    44.  
    45.     }
    46. }
    47.  


    Here is the EnemyHealth Script, that creates a new HealthSystem, which gives the health of the Enemy a value.


    Code (CSharp):
    1.  
    2. using Photon.Pun;
    3. using System.Collections;
    4. using UnityEngine;
    5.  
    6. public class EnemyHealth : MonoBehaviour, IPunObservable
    7. {
    8.     public HealthSystem healthSystemEnemy;
    9.  
    10.     //HealthBar linking to manage the damage and healing
    11.     public HealthBar healthBar;
    12.  
    13.     [HideInInspector] public int enemyHealth = 300;
    14.  
    15.  
    16.     private void Start()
    17.     {
    18.         healthSystemEnemy = new HealthSystem(enemyHealth);
    19.         StartCoroutine(GetNeededComponents());
    20.  
    21.         StartCoroutine(OnEnemyDeath());
    22.     }
    23.  
    24.     IEnumerator GetNeededComponents()
    25.     {
    26.         yield return new WaitForFixedUpdate();
    27.  
    28.         healthBar = GetComponentInChildren<HealthBar>();
    29.  
    30.         //Manage Damage and Heal on the HealthBar
    31.         healthBar.Setup(healthSystemEnemy);
    32.  
    33.         yield return new WaitForSeconds(3);
    34.         StopCoroutine(GetNeededComponents());
    35.         //Debug.Log("Coroutine Stopped");
    36.     }
    37.  
    38.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    39.     {
    40.         if (stream.IsWriting)
    41.         {
    42.             // We own this player: send the others our data
    43.             stream.SendNext(enemyHealth);
    44.         }
    45.         else
    46.         {
    47.             // Network player, receive data
    48.             enemyHealth = (int)stream.ReceiveNext();
    49.         }
    50.     }
    51.  
    52.     IEnumerator OnEnemyDeath()
    53.     {
    54.         yield return new WaitUntil(() => healthSystemEnemy.GetHealthPercentage() == 0);
    55.         {
    56.             Debug.Log("Enemy Died");
    57.             StopCoroutine(OnEnemyDeath());
    58.         }
    59.     }
    60. }
    61.  


    Below here is the HealthBar Script, which handles the view of the Health, on the Healthbar:


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class HealthBar : MonoBehaviour
    7. {
    8.  
    9.     private HealthSystem healthSystem;
    10.  
    11.     public void Setup(HealthSystem healthSystem)
    12.     {
    13.         this.healthSystem = healthSystem;
    14.  
    15.         healthSystem.OnHealthChanged += HealthSystem_OnHealthChanged;
    16.     }
    17.  
    18.     private void HealthSystem_OnHealthChanged(object sender, System.EventArgs e)
    19.     {
    20.         transform.Find("Bar").localScale = new Vector3(healthSystem.GetHealthPercentage(), 1);
    21.     }
    22. }
    23.  


    Last, but not least, here is the PlayerCombat Script, that helps the Player damage the Enemy.


    Code (CSharp):
    1.  
    2. using Photon.Pun;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class PlayerCombat : MonoBehaviour
    8. {
    9.     //Combat components
    10.     public static PlayerCombat playerCombat;
    11.  
    12.     public Transform attackPoint;
    13.     public float attackRange;
    14.     public LayerMask enemyLayers;
    15.  
    16.     public EnemyHealth EH;
    17.     public PhotonView PV;
    18.  
    19.     //Damage amount
    20.     [HideInInspector] public int attackDamage = 50;
    21.  
    22.  
    23.     private void Awake()
    24.     {
    25.         playerCombat = this;
    26.     }
    27.  
    28.     private void Start()
    29.     {
    30.         attackPoint = GetComponentInChildren<AttackPoint>().transform;
    31.         PV = GetComponent<PhotonView>();
    32.     }
    33.  
    34.     public void AttackEnemy()
    35.     {
    36.         //Detect enemies in range of the attack
    37.         Collider[] hitEnemies = Physics.OverlapSphere(attackPoint.position, attackRange, enemyLayers);
    38.  
    39.         //Get the right EnemyHealth
    40.         EH = GameObject.FindGameObjectWithTag("Enemy").GetComponent<EnemyHealth>();
    41.  
    42.         //Damage enemies
    43.         foreach (Collider enemy in hitEnemies)
    44.         {
    45.             EH.healthSystemEnemy.Damage(attackDamage);
    46.         }
    47.     }
    48.  
    49.  
    50.     private void OnDrawGizmosSelected()
    51.     {
    52.  
    53.         if (attackPoint == null)
    54.             return;
    55.  
    56.         Gizmos.DrawWireSphere(attackPoint.position, attackRange);
    57.     }
    58. }
    59.  
     
  2. unity_6abfP1dmd73sYA

    unity_6abfP1dmd73sYA

    Joined:
    Jul 3, 2021
    Posts:
    1
    I'm having the same issue in my project, and I can't find anywhere how to solve it, did you resolve this problem? if you got it could you tell me how to solve it? sorry for can't help you.