- Home /
Question by
Voavo · Aug 14, 2018 at 02:03 AM ·
networkingrpcclientcommand
Why is this not working properly? ClientRPC
It's a very simple Raycast shooting, if the hit.collider.tag equals Enemy, then the ReceiveDamage function gets called (the function is in the enemy script)
The problem is that it works fine only if you are the host, you can shoot, and the client will also see the health decreasing and the enemy disappearing if it dies, but if the client is the one shooting, it only affects the enemy locally, if you shoot the enemy as a client you will see the health decreasing, but then if the host shoots it again then you will see that the health gets updated with the actual value, even if you kill it it will only disappear for you, and the host will still be able to see it.
Code: public void ReceiveDamage(float damage){ health -= damage;
CmdUpdateHealth (this.gameObject,this.health);
}
[Command]
void CmdUpdateHealth(GameObject go,float newHealth){
go.GetComponent<EnemyScript>().health = newHealth;
RpcUpdateHealth (go,go.GetComponent<EnemyScript>().health);
}
[ClientRpc]
void RpcUpdateHealth(GameObject go,float newHealth){
go.GetComponent<EnemyScript>().health = newHealth;
}
Comment