- Home /
Issue with synchronizing NPC Health over the network.
Hello there, I am facing a major problem at the moment with Networking. I am new to this tpoic, but already read a lot of theory and I think I understood it. What I do not know is the synatx and I could not find another topic in the forum so far that could help me with that.
Now my problem. I have set up soem code, that allows to set up a Masterserver and up to clients to connect. Each client controls a capsule and a third-person-camera at the moment. By pressing "1" they can apply some "Damage" to a cube that has 10 health at the Moment.
When Player 1 now damages the cube he can actually see a healthbar that shows the damage that was done. But Player 4 e.g. can not see this damage. It is somehow not synchronized. When Player 1 however "killed" the cube it disappears using "Network.Destroy". Then it disappears on all clients and the server.
This is the relevant code I use on the cube to parse his health to all clients.
 void Update (){
     if(Health <= 0){
         Network.Destroy(gameObject);
     }
 }
 [RPC]
 public void ParseBossHealth(float health){
     Debug.Log ("ParsingHealth__ " + health);
     networkView.RPC ("ParseBossHealth", RPCMode.All, health);
 }
 public void applyDamage (float damage){
     Health -= damage;
     ParseBossHealth (Health);
 }
This is the relevant code I use on each player to apply the damage:
 //Apply the done Damage to the target
 [RPC]
 public void DamageTheBoss(float damage){
     target.GetComponent<HealthScript> ().applyDamage(damage);
     GetComponent<DamageDealt> ().DamageDealtValue += damage;
     Debug.Log ("CurrentBossHealth" + currentBossHealth);
 }
Answer by ooParanoia · Oct 30, 2014 at 12:57 PM
Solved the problem myself now.
This is the code that works for me:
On the Cube:
 [RPC]
 public void ParseBossHealth(float health){
     Debug.Log ("ParsingHealth__ " + health);
     Health = health;
 }
 
 public void applyDamage (float damage){
     Health -= damage;
     networkView.RPC ("ParseBossHealth", RPCMode.All, Health);
     if(Health <= 0){
         Network.Destroy(gameObject);
     }
     
 }
On the Player:
 if (skillID == 1) {
         if(Vector3.Distance(target.transform.position, transform.position) <= Meele_Range){
             DamageTheBoss(Meele_Damage);
             //Play Anim(statemachine)
         }
     }
 public void DamageTheBoss(float damage){
     target.GetComponent<HealthScript> ().applyDamage(damage);
     GetComponent<DamageDealt> ().DamageDealtValue += damage;
     Debug.Log ("CurrentBossHealth" + currentBossHealth);
 }
Your answer
 
 
             Follow this Question
Related Questions
unity c# RPC 0 Answers
Unity Networking synchronize 3DText's 1 Answer
Unity Multiplayer Networking..... in 2D!?!? crazy i know! 1 Answer
Sending Animation Over Network 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                