mirror of
https://github.com/DerTyp7/defrain-shooter-unity.git
synced 2025-10-29 20:52:10 +01:00
Grenade Data Location
Grenade data is now on Grenade.cs and not on Weapon.cs. You need both scripts to make a grenade work
This commit is contained in:
@@ -4,17 +4,30 @@ using UnityEngine;
|
||||
|
||||
public class Grenade : MonoBehaviour
|
||||
{
|
||||
[SerializeField] GameObject explodeParticle;
|
||||
[SerializeField] Weapon weapon;
|
||||
|
||||
[Header("Grenade Info")]
|
||||
[SerializeField] float timer = 2f;
|
||||
[SerializeField] float explosionForce = 500f;
|
||||
[SerializeField] float grenadeRadius = 3f;
|
||||
[SerializeField] bool hasExploded = false;
|
||||
private float countdown;
|
||||
|
||||
[Header("Explosion GameObject")]
|
||||
[SerializeField] GameObject explodeParticle;
|
||||
|
||||
[Header("Scripts")]
|
||||
[SerializeField] Weapon weapon;
|
||||
|
||||
[Header("Debug")]
|
||||
[SerializeField] bool showExplosion = true;
|
||||
|
||||
void Start() {
|
||||
countdown = weapon.Timer;
|
||||
countdown = timer;
|
||||
}
|
||||
|
||||
void Update() {
|
||||
// If grenade has been thrown and countdown is over 0 and grenade has not exploded yet
|
||||
if (weapon.HasBeenThrown && !weapon.HasExploded) {
|
||||
if (weapon.HasBeenThrown && !hasExploded) {
|
||||
// Decrease timer by 1 second
|
||||
countdown -= Time.deltaTime;
|
||||
// If countdown get to 0... BOOM!:
|
||||
@@ -28,13 +41,17 @@ public class Grenade : MonoBehaviour
|
||||
|
||||
/* - 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);
|
||||
if (showExplosion)
|
||||
{
|
||||
// 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);
|
||||
Collider[] colliders = Physics.OverlapSphere(transform.position, grenadeRadius);
|
||||
// Iterate over all colliders found in radius
|
||||
foreach(Collider nearbyObject in colliders) {
|
||||
// Check if nearby object is a Player and if Collider is not a CharacterController (can be changed to CapsuleCollider)
|
||||
@@ -47,11 +64,11 @@ public class Grenade : MonoBehaviour
|
||||
// if rigidbody exists...
|
||||
if (rb != null) {
|
||||
// adds force to nearby objects
|
||||
rb.AddExplosionForce(weapon.ExplosionForce, transform.position, weapon.GrenadeRadius);
|
||||
rb.AddExplosionForce(explosionForce, transform.position, grenadeRadius);
|
||||
}
|
||||
}
|
||||
}
|
||||
weapon.HasExploded = true;
|
||||
hasExploded = true;
|
||||
// Destroys grenade
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user