- Home /
How do I use RPC ,sync player health bar in unity3d c#
I have a network script . The health bar shows on top of the players head. But thing the health bar is not updating when I shoot. In other word its not taking damage when I shoot. I must be forgetting something . Here is my code : using UnityEngine; #if ENABLE_MULTIPLAYER using UnityEngine.Networking; using UnityEngine.UI; if ENABLE_MULTIPLAYER public class Health : NetworkBehaviour #else public class Health : MonoBehaviour #endif {
int health; Healthbar healthbar;
void Start()
{
healthbar = FindObjectOfType<Healthbar>();
if (healthbar == null)
{
Debug.LogError("Healthbar class is not found in scene!");
}
}
if ENABLE_MULTIPLAYER
if (isServer) {
RpcDamage(amount, position, force, radius, attacker);
}
// Execute the method on the local instance. Use isClient instead of isServer because the client and server may be the same instance
// in which case the method will be called with the Rpc call.
if (!isClient) {
endif
DamageLocal(amount, position, force, radius, at
[ClientRpc] private void RpcDamage(float amount, Vector3 position, Vector3 force, float radius, GameObject attacker) { DamageLocal(amount, position, force, radius, attacker); }
}
}
Answer by sinoby · Jun 10, 2016 at 10:59 AM
good article about your case: http://unity3d.com/learn/tutorials/topics/multiplayer-networking/networking-player-health?playlist=29690