mirror of
https://github.com/DerTyp7/defrain-shooter-unity.git
synced 2025-10-29 20:52:10 +01:00
[+] added + grenade.cs script + grenades can be thrown + grenades "explode" and spawn "explosion" (currently only a 50% transparent cube) - NEEDS TO BE TESTED => BUG FIXED
52 lines
1.8 KiB
C#
52 lines
1.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class Grenade : MonoBehaviour
|
|
{
|
|
[SerializeField] GameObject explodeParticle;
|
|
[SerializeField] Weapon weapon;
|
|
private float countdown;
|
|
|
|
void Start() {
|
|
countdown = weapon.Timer;
|
|
}
|
|
|
|
void Update() {
|
|
// If grenade has been thrown and countdown is over 0 and grenade has not exploded yet
|
|
if (weapon.HasBeenThrown && !weapon.HasExploded) {
|
|
// Decrease timer by 1 second
|
|
countdown -= Time.deltaTime;
|
|
// If countdown get to 0... BOOM!:
|
|
if(countdown <= 0) {
|
|
// Lets grenade explode
|
|
Explode();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/* - Spawn explosion particles and add force to nearby objects - */
|
|
private void Explode() {
|
|
// Spawns explosion particle
|
|
GameObject spawnedExplosion = Instantiate(explodeParticle, transform.position, transform.rotation);
|
|
// Destroys explosion particle after on second
|
|
Destroy(spawnedExplosion, 1);
|
|
// Gets all collider that are in a sphere around the grenade
|
|
Collider[] colliders = Physics.OverlapSphere(transform.position, weapon.GrenadeRadius);
|
|
// Iterate over all colliders found in radius
|
|
foreach(Collider nearbyObject in colliders) {
|
|
// 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;
|
|
// Destroys grenade
|
|
Destroy(gameObject);
|
|
}
|
|
}
|