The question is answered, right answer was accepted
only game host can damage clients
hello, im trying to make a simple first person shooter setup, my problem is that only the host can damage players but clients cant. when ever i try to damage a player on client i get the current warning : Trying to send command for object without authority. , code :
private void Shoot()
{
if (Physics.Raycast(cam.position, cam.forward, out hit, distance, mask))
{
if (hit.transform.tag == "Player")
{
hit.transform.GetComponent<FirstPersonController>().CmdDamagePlayer(damage);
}
}
}
[Command]
public void CmdDamagePlayer(int dmg)
{
RpcTakeDamage(dmg);
}
[ClientRpc]
public void RpcTakeDamage(int dmg)
{
health -= dmg;
if (health <= 0)
{
health = 0;
}
if(isLocalPlayer)
healthBar.value = health;
else
healthBarTP.value = health;
}
im calling this from the FirstPersonController class which is located on the root of the player object, from what i understand commands apply code on server using client data and rpc calls apply code on clients using server data, so what's the problem ?? thanks.
Answer by Moaid_T4 · Feb 22, 2016 at 01:42 AM
so i fixed it using a bit of tweaking and docs, so apearantly only the server is allowed to call commands and rpc's on other players
private void Shoot()
{
if (Physics.Raycast(cam.position, cam.forward, out hit, distance, mask))
{
if (hit.transform.tag == "Player")
{
CmdShootPlayer(damage,hit.transform.gameObject);
}
}
}
[Command]
public void CmdShootPlayer(int dmg,GameObject player)
{
player.GetComponent<FirstPersonController>().RpcTakeDamage(dmg);
}
[ClientRpc]
public void RpcTakeDamage(int dmg)
{
health -= dmg;
if (health <= 0)
{
health = 0;
}
if(isLocalPlayer)
healthBar.value = health;
else
healthBarTP.value = health;
}
Follow this Question
Related Questions
Only Host Can Damage Players In A Network Match 0 Answers
How to use a script to control the nickname of online players 0 Answers
Problem with Input.gyro in LAN network Unet 0 Answers
Unet Game on Phton Server? 0 Answers
Bullets not spawning in other clients but its spawning on the client that shot it. 1 Answer