Physics.OverlapSphere not detecting collision! Collision help.
Explanation
Hey. I've seen other countless articles about this same question but nobody seems to have an answer and it really is getting frustrating for me.
I have a Bullet prefab that is shot out a turret to hit an enemy. And on impact, I have set it to destroy the enemy and its been working. But I wanted to add some splash damage so I added an overlap sphere on impact with a small radius (with the bullet being the center, obviously). And any collider with the tag "Enemy", I want to destroy.
Problem
Now, it is detecting that it has collided with the enemies in the radius, and I know this because I have set it to Debug.Log a statement whenever any enemies are hit upon impact within the radius of the sphere.
So if it hits 3 enemies, it will Debug.Log the statement 3 times.
Although it fires the Debug.Log statement, it for some reason isn't firing the Destroy function. It is only destroying the first target it hits.
My Enemy prefabs have a sphere collider, but not a rigidbody. This shouldnt be a problem though because Im only trying to detect collision, right...?
Here is the code for the Bullet (only the part that is supposed to do the destroy and overlap stuff)
void Explode()
{
Collider[] colliders = Physics.OverlapSphere(transform.position, explosionRadius);
foreach (Collider collider in colliders)
{
if (collider.CompareTag("Enemy"))
{
Debug.Log("Damage in radius");
Destroy(target.gameObject);
}
}
}
void Damage(Transform enemy)
{
Destroy(target.gameObject);
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, explosionRadius);
}
void HitTarget()
{
if (explosionRadius > 0f)
{
Explode();
}
else
{
Damage(target);
}
Destroy(gameObject);
}
the "explosionRadius" variable is a public float, that I can change through the inspector (it is attached to the bullet prefab).
I've done it to only execute if explosionRadius is over 0 (set in the inspector) because there are different types of turrets, and I don't want some of them to have splash damage. So I make them shoot different bullet prefabs, and each of these bullet prefabs have the same bullet script (that contains the code above).
If I don't want a specific turret to have splash damage, then I just set the explosionRadius to 0 on the bullet prefab assigned to that turret through the inspector.
right now, the Explode() function, and the Damage() function, both do the same thing; destroys the gameObject for now (which is the enemy). But if the explosionRadius is set to 0, it should only destroy the target Enemy it hit. But if it is over 0, it should destroy every Enemy in the bullets radius upon impact... which is not happening.
I can see the Radius of the explosionRadius of the bullet visually by adding a gizmos to it.
What I've tried
Yes, the Enemy does have a tag called "Enemy". Even the clones do. I checked.
The enemy does have a sphere collider, with isTrigger toggled off.
Changing the`if (collider.CompareTag("Enemy"))` to
if (collider.tag == "Enemy")
(which for some reason made it worse. It would call the Debug.Log statement multiple times for a single hit).Changing the Enemies collider from sphere to box to mesh, etc. None work. So I have it set a sphere collider now.
I've tried changing
public void Damage(Transform enemy) {
Destroy(target.gameObject);
}
void HitTarget()
{
if (explosionRadius > 0f)
{
Explode();
}
else
{
Damage(target);
}
Destroy(gameObject);
}
to
public void Damage(GameObject enemy) {
Destroy(target.gameObject);
}
void HitTarget()
{
if (explosionRadius > 0f)
{
Explode();
}
else
{
Damage(target.gameObject);
}
Destroy(gameObject);
}
nothing has worked.
You should know
- that the Bullet does not have a collider nor rigidbody.
The enemy has a sphere collider, with the isTrigger toggled off.
The bullet radius for explosion is visible via gizmos and you can see how it can take out 3 Enemies during runtime. (attachment is a link now because it only allows 2)
every object i've mentioned so far is a prefab
I'm using Unity version 2019.4.2f1
That i'm going to rip my hair out.
Any type of help is appreciated. Even if you don't have a clue, just try and answer please. Thank you.
If you need to know anything else, I gotchu. Just ask.
Answer by DenisIsDenis · Jun 29, 2021 at 03:25 AM
Looking at this piece of code:
void Explode()
{
Collider[] colliders = Physics.OverlapSphere(transform.position, explosionRadius);
foreach (Collider collider in colliders)
{
if (collider.CompareTag("Enemy"))
{
Debug.Log("Damage in radius");
Destroy(target.gameObject);
}
}
}
And after reading that all Debug.Log
work, I immediately drew attention to the Destroy
function. In it, you destroy target.gameObject
every time. And you need to destroy collider.gameObject
.
SOLUTION: just replace Destroy(target.gameObject);
on Destroy(collider.gameObject);
in the Explode()
function.
(Note: in the Damage
function, you accept the Transform enemy
parameter, and for some reason destroy target.gameObject
, change it to Destroy(enemy.gameObject)
, just in case. In your case, this error did not play any role, but it can cause errors in future).
It should work :)
Wow! It worked!
Thank you so much. With many years of experience, you'd think something that mild would be figured out right!? I appreciate the help. I've accepted this answer and rewarded you a point ;).
Keep doing what you're doing! You helped someone today. Nothing in the world can succeed being generous.
And sorry for not specifying what the target variable was supposed to be. target is a private Transform I created. And it is used in a void that the bullet uses to seek it's target.