Question by
DominikHeyn · Apr 16, 2017 at 07:06 PM ·
addexplosionforce
Why does AddExplosionForce() not apply force to clients? (UNet)
Hey Folks,
I got a sphere which has a collider, rigidbody and the following script attached to it.
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class PhysicsTest: NetworkBehaviour
{
int playerMask;
[SerializeField] float[] explosionRadius = 5f;
[SerializeField] float[] explosionForce = 1000f;
[SerializeField] AudioSource ambienceAudio;
private void Start()
{
if (isServer)
ambienceAudio.Play();
playerMask = LayerMask.GetMask("Player");
}
[ServerCallback]
private void OnCollisionEnter(Collision c)
{
if(c.gameObject.tag == "Player")
{
ApplyPhysics(explosionForce, explosionRadius);
NetworkServer.Destroy(gameObject);
}
}
[ServerCallback]
private void ApplyPhysics(float force, float radius)
{
Collider[] players = Physics.OverlapSphere(transform.position, radius, playerMask);
foreach (Collider targets in players)
{
Rigidbody playerRB = targets.GetComponent<Rigidbody>();
targets.AddExplosionForce(force, transform.position, radius);
}
}
}
This applies force to the server only. However, If I call ApplyPhsics via OnNetworkDestroy it works somehow for both, the server and the client(s).
public override void OnNetworkDestroy()
{
ApplyPhysics(explosionForce, explosionRadius);
base.OnNetworkDestroy ();
}
Can anyone explain to me why it behaves like this? What do I have to do to make this work without OnNetworkDestroy() ?
Thank you very much in advance for your input!
Dominik
Comment