- Home /
Why is my client player unaffected by a networked explosion?
In my game, players can spawn fireballs. These fireballs, after being spawned, travel in a straight line. Upon impact with, among other things, a player, an explosion occurs, impacting all objects and players within a certain radius.
Like so:
Unfortunately, I cannot seem to get this to work in a networked game. The host player is affected by the explosion and is pushed back, but the client player is not, irrespective of which player spawned the fireball. I am testing over LAN using the standard NetworkManager GUI.
The code responsible for the explosion is a script on the fireball and reads as follows:
private void OnCollisionEnter(Collision collision)
{
Collider[] colliders = Physics.OverlapSphere(rb.position, radius);
foreach (Collider hit in colliders)
{
// Add explosive force on hit objects
Rigidbody hitRB = hit.GetComponent<Rigidbody>();
if (hitRB != null)
{
//hitRB.AddExplosionForce(force * timeForceFactor, rb.position, radius, 0, ForceMode.Impulse);
CmdExplode(hit.gameObject, force * timeForceFactor, rb.position, radius);
//RpcExplode(hit.gameObject, force * timeForceFactor, rb.position, radius);
}
}
Destroy(gameObject);
}
[ClientRpc]
void RpcExplode(GameObject hit, float power, Vector3 origin, float radius)
{
hit.GetComponent<Rigidbody>().AddExplosionForce(power, origin, radius, 0, ForceMode.Impulse);
}
[Command]
void CmdExplode(GameObject hit, float power, Vector3 origin, float radius)
{
hit.GetComponent<Rigidbody>().AddExplosionForce(power, origin, radius, 0, ForceMode.Impulse);
}
With the above code, only the host player gets jostled around by explosions. If I use the RPC, instead of the Command, only the client player gets moved around by explosions instead, and some minor glitches are introduced. Often, the glitched player is affected by the explosion for the duration of a single frame, before being pulled back to its initial position.
The fireball object is spawned by either player using NetworkServer.spawn, has a network transform and otherwise functions as required. As everything worked fine for the offline version of the game, I am convinced that the error is due to my limited skill in unity networking.
Thanks in advance
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
UNet NetworkServer.Spawn() not working / Failed to spawn server object, assetId= 2 Answers
Creating a client player and a host player in a single device and networking them correspondingly 0 Answers
Networking bullet spawning issues between Host/Client 0 Answers
[Command] not sending to server? 1 Answer