mirror of
https://github.com/DerTyp7/defrain-shooter-unity.git
synced 2025-10-29 12:52:07 +01:00
Grenade damage
If player is in sphere around grenade he gets damage
This commit is contained in:
@@ -32,16 +32,23 @@ public class Grenade : MonoBehaviour
|
|||||||
GameObject spawnedExplosion = Instantiate(explodeParticle, transform.position, transform.rotation);
|
GameObject spawnedExplosion = Instantiate(explodeParticle, transform.position, transform.rotation);
|
||||||
// Destroys explosion particle after on second
|
// Destroys explosion particle after on second
|
||||||
Destroy(spawnedExplosion, 1);
|
Destroy(spawnedExplosion, 1);
|
||||||
|
|
||||||
// Gets all collider that are in a sphere around the grenade
|
// Gets all collider that are in a sphere around the grenade
|
||||||
Collider[] colliders = Physics.OverlapSphere(transform.position, weapon.GrenadeRadius);
|
Collider[] colliders = Physics.OverlapSphere(transform.position, weapon.GrenadeRadius);
|
||||||
// Iterate over all colliders found in radius
|
// Iterate over all colliders found in radius
|
||||||
foreach(Collider nearbyObject in colliders) {
|
foreach(Collider nearbyObject in colliders) {
|
||||||
// Get Rigidbody from nearby object and...
|
// Check if nearby object is a Player
|
||||||
Rigidbody rb = nearbyObject.GetComponent<Rigidbody>();
|
if (nearbyObject.transform.gameObject.GetComponent<Player>()) {
|
||||||
// if rigidbody exists...
|
// Remove health from player
|
||||||
if(rb != null) {
|
nearbyObject.transform.gameObject.GetComponent<Player>().RemoveHealth(weapon.Damage);
|
||||||
// adds force to nearby objects
|
} else {
|
||||||
rb.AddExplosionForce(weapon.ExplosionForce, transform.position, weapon.GrenadeRadius);
|
// Get Rigidbody from nearby object and...
|
||||||
|
Rigidbody rb = nearbyObject.GetComponent<Rigidbody>();
|
||||||
|
// if rigidbody exists...
|
||||||
|
if (rb != null) {
|
||||||
|
// adds force to nearby objects
|
||||||
|
rb.AddExplosionForce(weapon.ExplosionForce, transform.position, weapon.GrenadeRadius);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
weapon.HasExploded = true;
|
weapon.HasExploded = true;
|
||||||
|
|||||||
Reference in New Issue
Block a user