Hello community, I’m working on a fighting game that is hosted online using UNet.
Here is some code I have.
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Sword"))
{
TakeDamage(1);
if (health == 0)
{
CmdKillPlayer(true);
Destroy(gameObject, 3f);
}
else
{
if (canHit)
{
Instantiate(slash, transform.position, Quaternion.identity);
isHit = true;
isHitCooldown = isHitStartCooldown;
}
}
}
}
[ClientRpc]
void RpcDamage(int damage)
{
health -= damage;
dazedTime = startDazedTime;
myAni.SetTrigger("Hit");
}
[ClientRpc]
void RpcDie(bool kill)
{
canHit = false;
isDead = true;
Score.scoreValue += 10;
jumpCount += 1;
myAni.SetTrigger("Die");
}
public void TakeDamage(int damage)
{
RpcDamage(damage);
}
[Command]
public void CmdKillPlayer(bool kill)
{
RpcDie(true);
}
The effect I’m getting is that the host can hit the clients and hurt them until they die. But the clients that join the host server can’t do any damage to any players or hurt the host player. I’m using a NetworkManager script that loads a prefab of my character with a NetworkBehavior PlayerController script on it. The PlayerController script contains all my code for movement and attacking.
I’m just getting into UNet code, and any help would be greatly appreciated.
Kind Regards,
~Macchus~