Multiplayer - Host can damage client, not vice versa?
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~
Answer by Macchus · Oct 01, 2018 at 05:48 AM
I've tried adding an if statement (hasAuthority) in the OnTriggerEnter2D, but it seems to just remove any functionality.
From the forums and videos I've watched. [Command] should be used for the client to tell the server something happened and [ClientRpc] is to get the animations play for all the clients or update all the health. I still can't seem to wrap my head around how or what is causing my clients to not hit the host, when the host can freely hit them.
The battle continues....
Your answer
Follow this Question
Related Questions
Self hosted Unity Multiplayer alternative? 2 Answers
How to detect server host has left - Host Migration 0 Answers
Disconnected Host 0 Answers
Dedicated Server 2 Answers
Singleplayer and multiplayer using same gameobjects? 0 Answers