Sync health with network manager
Hello, i try to sync the health between players in my game, but if the player(not host) is trying to attack someone the player will not lose heal. And if the player(host) is trying to attack someone he will lose heal.
I really dont know how to handle this. I used this tutorial https://unity3d.com/learn/tutorials/topics/multiplayer-networking/networking-player-health
This is my PlayerFightController.cs using UnityEngine; using System.Collections;
public class PlayerFightController : MonoBehaviour {
public GameObject armRight;
public bool attack = false;
public bool protect = false;
public bool push = false;
public int damage;
private Collision cPlayerCol;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Mouse0) && cPlayerCol != null) {
Debug.Log ("Hit");
Health health = cPlayerCol.gameObject.GetComponent<Health> ();
health.TakeDamage (damage);
}
}
public void OnCollisionEnter(Collision col) {
if (col.gameObject.tag == "Player" && cPlayerCol == null) {
Debug.Log ("Collision");
Debug.Log (col.gameObject.name);
cPlayerCol = col;
}
}
public void OnCollisionExit(Collision col) {
if (col.gameObject.tag == "Player" && cPlayerCol != null) {
cPlayerCol = null;
}
}
}
Health.cs using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.Networking;
public class Health : NetworkBehaviour {
public const int maxHealth = 100;
[SyncVar(hook = "OnChangeHealth")]
public int currentHealth = maxHealth;
public RectTransform healthbar;
public void TakeDamage(int amount) {
if (!isServer)
return;
currentHealth -= amount;
if (currentHealth <= 0) {
currentHealth = 0;
Debug.Log ("Dead");
}
}
public void OnChangeHealth(int health) {
healthbar.sizeDelta = new Vector2 (health * 2, healthbar.sizeDelta.y);
}
}
Can anybody help me?
Your answer
Follow this Question
Related Questions
Player flying into oblivion. 2 Answers
GUI.Button is not doing anything 0 Answers
Inconsistent input detection across different computers 0 Answers
MonoDevelop not starting (different from last time) 0 Answers