add explodable script

This commit is contained in:
2026-07-15 21:23:59 -05:00
parent 2715ba7a86
commit 16342f1ff4
8 changed files with 5368 additions and 2 deletions
+13 -2
View File
@@ -2,12 +2,23 @@
public class BurriedItem : Diggable
{
public GameObject ItemToSpawn;
// Spawnpoint is optional, will spawn at location of dig spot, translated up
// on y-axis
public Transform Spawnpoint;
public GameObject ItemToSpawn;
public override void Dig()
{
Instantiate(ItemToSpawn, Spawnpoint.position, Spawnpoint.rotation, null);
if (Spawnpoint == null)
{
Vector3 newPos = new Vector3(transform.position.x, transform.position.y + 0.2f, transform.position.z);
Instantiate(ItemToSpawn, newPos, transform.rotation, null);
}
else
{
Instantiate(ItemToSpawn, Spawnpoint.position, Spawnpoint.rotation, null);
}
Destroy(gameObject);
}
}
@@ -0,0 +1,34 @@
using UnityEngine;
// Put this on things that will explode when they enter the collision trigger
// an Explosion script
public class Explodable : MonoBehaviour
{
// Light is optional (only if you want the exploded object to create light)
public Light explodeLight;
public ParticleSystem explodeParticle;
public float explosionLengthTime;
public void Explode()
{
if (explodeLight != null)
{
explodeLight.gameObject.SetActive(true);
explodeLight.transform.parent = null;
Destroy(explodeLight.gameObject, explosionLengthTime);
}
if (explodeParticle != null)
{
explodeParticle.gameObject.SetActive(true);
explodeParticle.transform.parent = null;
//explodeParticle.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
explodeParticle.Play();
Destroy(explodeParticle.gameObject, explosionLengthTime);
}
Destroy(gameObject);
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 0c0bbb84f48543046b86f073995c09b1
@@ -0,0 +1,18 @@
using UnityEngine;
// Put this on an empty game object with a trigger collider. Anything inside of this
// trigger collider with an Explodable script will explode
public class Explosion : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if (other != null)
{
Explodable explodable = other.GetComponent<Explodable>();
if (explodable != null)
{
explodable.Explode();
}
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: fcf160d0f430fff438a4e57b70f778b2
+4
View File
@@ -4,6 +4,7 @@ public class Tnt : MonoBehaviour
{
public Light explodeLight;
public ParticleSystem explodeParticle;
public Explosion explosion;
public float fuseTime;
public float explosionLengthTime;
@@ -17,6 +18,8 @@ void Update()
if (!exploded && timer > fuseTime)
{
exploded = true;
explosion.gameObject.SetActive(true);
explosion.transform.parent = null;
explodeLight.gameObject.SetActive(true);
explodeLight.transform.parent = null;
explodeParticle.gameObject.SetActive(true);
@@ -26,6 +29,7 @@ void Update()
Destroy(explodeLight.gameObject, explosionLengthTime);
Destroy(explodeParticle.gameObject, explosionLengthTime);
Destroy(explosion.gameObject, 0.1f);
Destroy(gameObject);
}