- Home /
Network HP Bar
Hey, I wanna implement a FPS HP bar in network. I got the "ApplyDamage" Script all set up and the dealing of the damage works just fine as see in the inspector with the public float health.
Now i want to sync that health Variable with a healthBar. However, with the following script attached, whenever I hit an enemy. MY OWN hp bar goes down. Plus, it drops with 100 to 1 with just 20 damage losing of 100 health. Im really frustrated right now. Thanks in advance
. . .
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Networking;
public class FPSPlayerHealth : NetworkBehaviour {
public const int maxHealth = 100;
[SyncVar]
public int currentHealth = maxHealth;
public Image HP;
Animator anim;
string DAMAGE_TAKEN = "TakeDamage";
private void Start()
{
anim = GetComponent<Animator>();
}
public void TakeDamage(int damage)
{
if (!isServer)
{
return;
}
currentHealth -= damage;
HP.fillAmount = currentHealth / 100;
anim.SetTrigger(DAMAGE_TAKEN);
print("DAMAGE RECEIVED");
//MAKE DIFFERENT THINGS IF DEAD OR RECEIVE DAMAGE
}
Your answer
Follow this Question
Related Questions
Health subtracts when hit by bullit 2 Answers
Enemy Health Bar UI 1 Answer
Instantiate health bar with networking 2 Answers
Healthpickup isn't working 1 Answer