Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by In0sc0p3dJFK · Jun 28, 2021 at 08:24 PM · collisionphysicsrigidbodycollideroverlapsphere

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. ‍ alt text ‍ ‍ ‍


  • The enemy has a sphere collider, with the isTrigger toggled off.

alt text


  • 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)

https://ibb.co/P1MXzw0


  • 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.

missile.png (346.7 kB)
alienstats.png (244.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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 :)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image In0sc0p3dJFK · Jun 29, 2021 at 07:48 AM 2
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

316 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rigidbody randomly going through collider 1 Answer

[Help] Player stops moving when hitting wall diagonally 0 Answers

Why does the player hover over the ground instead of touching it? Why doesn't it always jump when I press space? 2 Answers

Very confused... Physics.OverlapSphere working, but not NonAlloc version? 2 Answers

Why Isn't My OnCollisionEnter2D Code Not Working? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges