- Home /
Question by
tobbe50cent · Jul 09, 2021 at 12:53 PM ·
unity 2dontriggerenterbomb
Bomb not destroying colliders
Hell, I've made a bomb power, that let's you place bombs, that explode after 3 seconds. However, when I try to destroy gameobjects that collide with the explosion, the gameobjects do either not destroy or they only detroy on the exact touch. Both the bomb and the objects to destroy have Rigibody2D and BoxCollider2D attached but I still don't get the wanted effect.
Also tried onTriggerStay2D but that does not work either.
Anyway, this is the code for the bomb:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Bomb : MonoBehaviour
{
private Animator anim;
private bool exploding;
// public Text countDown;
private float count;
[SerializeField] AudioClip explosion;
[SerializeField] [Range(0, 1)] float volume = 0.75f;
// Start is called before the first frame update
void Start()
{
exploding = false;
anim = GetComponent<Animator>();
// countDown.gameObject.SetActive(true);
// countDown.gameObject.transform.position = this.transform.position;
// countDown.text = count.ToString();
StartCoroutine(countDown());
}
public bool isExploding()
{
return exploding;
}
// Update is called once per frame
void Update()
{
}
private IEnumerator countDown()
{
yield return new WaitForSeconds(3);
exploding = true;
StartCoroutine(explode());
}
private IEnumerator explode()
{
AudioSource.PlayClipAtPoint(explosion, Camera.main.transform.position, volume);
anim.SetTrigger("BossExplosion");
yield return new WaitForSeconds(1);
Destroy(gameObject);
}
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("test");
if (exploding)
{
if (other.gameObject.CompareTag("Player"))
{
Player p = other.gameObject.GetComponent<Player>();
if (!p.isFlashing())
{
p.hurt(1);
}
}
else if (other.gameObject.CompareTag("Enemy_Carnivore"))
{
EnemyCarnivore ec = other.gameObject.GetComponent<EnemyCarnivore>();
ec.death();
}
else if (other.gameObject.CompareTag("EStone"))
{
stone s = other.gameObject.GetComponent<stone>();
s.stoneDestroy();
}
}
}
Comment