I have at the moment a FPS game on networking,
Functionally each player moves and syncs fine, they can change weapons, fire bullets, bullets collide with anything not myPlayer or bullet, deal damage if the collider obj has playerHealth component.
Players can die and respawn.
It all went well, teaching myself network.
But now I’m stuck, I have a canvas holding a Text, in health script, it updates the health value on canvas. It only ever shows me the health of the target i have last hit, not my health.
I’m new to networking so i just cycled through everything I could think of, like an isLocalPlayer check, sending it through [command] then back as Rpc, then just on [client], always only 2 possible results; see targets health on my canvas OR no change to health on canvas.
I’m out of ideas, can someone help point me in the right direction here?
method TakeDamage is trigger via the bullets that where networkserver.spawn in.
the codes currently very butchered, I’ve rewritten sections over and over and now my brain is mush.
public const int maxHealth = 100;
[SyncVar] public int currentHealth = maxHealth;
public Text healthUI;
void OnEnable () {
healthUI = GameObject.Find ("healthHUD").GetComponent<Text> ();
}
public void TakeDamage (int amount)
{
if (!isLocalPlayer)
{
currentHealth -= amount;
if (currentHealth <= 0)
{
currentHealth = 0;
DeathDisable ();
}
UpdateHealth ();
}
}
void DeathDisable ()
{
GetComponent<CharacterController> ().enabled = false;
GetComponent<multiplayer_WeaponsSelection> ().enabled = false;
GetComponent<multiplayer_SpawnBullets> ().enabled = false;
GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = false;
Invoke ("Respawn", 5f);
}
void Respawn ()
{
GetComponent<CharacterController> ().enabled = true;
GetComponent<multiplayer_WeaponsSelection> ().enabled = true;
GetComponent<multiplayer_SpawnBullets> ().enabled = true;
GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = true;
transform.position = new Vector3 (0, 1, 0);
currentHealth = maxHealth;
UpdateHealth ();
}
void UpdateHealth () {
healthUI.GetComponent<multiplayer_HealthUI> ().UpdateHealth (currentHealth);
}