- Home /
X number of collisions for destroy
Hello I’m having a hard time destroying my object after multiple collisions. My enemy destroys after the first hit & I would want it to destroy after multiple hits I need help.
public int hitsneeded;
int hits;
void OnTriggerEnter2D (Collider2D col)
{
if (col.gameObject.tag == “Bullet”)
{
hits++;
if (hits >= hitsneeded)
{
Destroy(gameObject);
}
}
}
Did you set your hitsneeded in the inspector so that it isn't defaulted to zero?
There isn't anything wrong with this code, so that means something is fishy with how the gameObjects are set up. They have colliders2D on them, etc? Is there multiple things hitting this trigger that you don't know about?
Before the if Statement can you please add a Debug.Log("Hit");
and screenshot the output?
Answer by darkStar27 · Dec 17, 2018 at 03:52 AM
Try this:
public int health = 10;
private int hits;
void Update () {
if (health <= 0)
{
Destroy(gameObject);
}
}
public void DeductPoints (int damageAmount) {
health -= damageAmount;
}
void OnTriggerEnter2D (Collider2D col)
{
if (col.gameObject.tag == “Bullet”)
{
DeductPoints(5); //Will destroy in two shots.
}
}
Answer by Vega4Life · Dec 17, 2018 at 04:09 AM
I am going to take a fun guess and say you are shooting more than one bullet without you knowing it (or something equivalent to that). The code should work with a basic setup of a single gameObject hitting it.
You could also have mutliple colliders (child gameObjects) on your bullet that are tagged "Bullet". This could also grant multiple hits.
You need to debug how many bullets are firing, whats on the bullet that could be an extra tag, is there other objects that you accidentally tagged as "Bullet" that is hitting the character. Does the character itself have a collider with a tag as "Bullet"
That's how I would start debugging.
I have 1 bullet spawn per click and if held how spawns every 0.2 seconds I have a polygon collider on my bullet
Can you show some code on how they are spawning?
private float timedSpawn;
public float startSpawn;
void Update ()
{
if (Input.Get$$anonymous$$ouseButtonDown (0))
{
Vector3 offset = transform.rotation * new Vector3 (0, 0.5f, 0);
Instantiate (Bullet, transform.position + offset, transform.rotation);
AudioSource.PlayClipAtPoint(sound, transform.position);
}
else if (Input.Get$$anonymous$$ouseButton(0))
{
if (timedSpawn <= 0)
{
Vector3 offset = transform.rotation * new Vector3 (0, 0.5f, 0);
Instantiate (Bullet, transform.position + offset, transform.rotation);
AudioSource.PlayClipAtPoint(sound, transform.position);
timedSpawn = startSpawn;
}
else
{
timedSpawn -= Time.deltaTime;
}
}
Just to Test it try replacing Input.Get$$anonymous$$ouseButtonDown (0)
with Input.GetkeyDown($$anonymous$$eyCode.$$anonymous$$ouseButton0)
Answer by FBP_SHH · Dec 24, 2018 at 09:40 AM
The detection looks fine. But it looks like you don't remove the bullet. Try destroying the col.gameObject before doing the hits++;
I would handle the bullet spawns like this. This way you're sure you are only spawning one bullet each X seconds and it will work for all weapons imaginable.
float fireRate = 1f; // How fast can we fire?
float lastShot; // When did we shoot last?
void Update ()
{
if ( Input.GetMouseButtonDown)(0) ) // LMB is down
{
if ( Time.time - lastShot > fireRate ) // Is the last shot longer ago than the fireRate?
{
Fire(); // Shoot!
lastShot = Time.time; // Register the time of the last shot
}
}
}
void Fire(){
// Didn't change anything here
Vector3 offset = transform.rotation * new Vector3 (0, 0.5f, 0);
Instantiate (Bullet, transform.position + offset, transform.rotation);
AudioSource.PlayClipAtPoint(sound, transform.position);
}