Unet: Add a force to hit player (by projectile)
I am trying to add an explosion force to a player object by the projectile that hit him. Any suggestions on how to achieve that? It works for some rigidbodys spawned by Network.Spawn() but not for the player object.
I use this code for the Projectile:
using UnityEngine;
using UnityEngine.Networking;
public class ShellExplosion : NetworkBehaviour
{
//public GameObject explosionEffect;
[SerializeField]
private ParticleSystem explosionEffect;
public float maxDamage = 100f;
public float explosionForce = 1000f;
public float maxLifeTime = 10f;
public float explosionRadius = 5f;
private void Start()
{
// If it isn't destroyed by then, destroy the shell after it's lifetime.
Destroy(gameObject, maxLifeTime);
}
private void OnTriggerEnter(Collider other)
{
Collider[] colliders = Physics.OverlapSphere(transform.position, explosionRadius);
// Go through all the colliders...
for (int i = 0; i < colliders.Length; i++)
{
// ... and find their rigidbody.
Rigidbody targetRigidbody = colliders[i].GetComponent<Rigidbody>();
// If they don't have a rigidbody, go on to the next collider.
if (!targetRigidbody)
continue;
// Add an explosion force.
targetRigidbody.AddExplosionForce(explosionForce, transform.position, explosionRadius);
}
// Unparent the particles from the shell.
explosionEffect.transform.parent = null;
// Play the particle system.
explosionEffect.Play();
// Once the particles have finished, destroy the gameobject they are on.
Destroy(explosionEffect.gameObject, explosionEffect.duration);
// Destroy the shell.
Destroy(gameObject);
}
}
I could spawn the projectile with client authority but i dont know if thats a good way to do it. As it is spawned by Network.Spawn() it should be doing the AddExplosionForce on the server, right? And as i mentioned, it is working for some cubes i spawned in for testing.
I am an idiot. The problem had nothing to do with the code. $$anonymous$$y Player prefab just had the rigidbody on the root object and the colliders on the children. So i had to get to the root of the hit colliders and add force there.
Your answer
Follow this Question
Related Questions
How to Change the player status during run time 0 Answers
[UNET/JS] How to enable local player controll 0 Answers
Multiplayer: 4 players, each player spawning less amount of other players. 0 Answers
Unet: Spawn bullet - client does not have a reference of bullet 1 Answer
How to identify players on Photon Unity? 2 Answers