Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by eman1best · Feb 12, 2020 at 12:36 PM · radius

Need help using overlap sphere

I am having trouble trying to figure out how to make it so when an enemy is in the radius of a tower it will start taking damage, iv found other topics asking similar questions but I didn't quit get them and I am hoping out at least how to use Overlap Sphere?

Comment
Add comment · Show 1
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 ShadyProductions · Feb 12, 2020 at 01:45 PM 0
Share

Using overlap sphere can cut fast into performance, maybe u can put a circle/sphere(based on 2D/3D) collider on your object and make it a trigger, then u can do your stuff in the on trigger enter event, its easier and better for performance. (You can have multiple colliders, if you add an empty gameobject to your object and add the collider on that one)

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Favouriteless · Feb 13, 2020 at 11:13 AM

Honestly, I suggest not using OverlapSphere as it is very performance heavy. If you really want to, see @ctjagielski 's answer.

As for the other approach, would keep a list of all enemies and use (tower.position - enemy.position).sqrMagnitude < tower.range (range should be squared on start) to compare the distances of enemies from the tower. I do this in the tower defense game I am currently developing and it made a huge performance impact.

Using an ArrayList to keep track of enemies will do, add any enemies to the ArrayList in their Start() method and remove them when they die.

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 Bonfire-Boy · Feb 13, 2020 at 01:56 PM 0
Share

Very good advice I$$anonymous$$HO - just because Unity makes things like this function, and colliders even, available does not mean you need to or should use them.

As an example, I came across someone once who needed a strictly cylindrical collider, and implemented it by using the overlap of a box collider with a capsule collider.

The thing was, they only had one ball they needed to know about, and the cylinder was always aligned with the y axis.

What with the extra colliders and overlap code they needed to make events fire only when the ball was in both colliders, it ended up being ridiculously slow compared to just looking at the horizontal (squared) distance of the ball from a point, together with testing its y coordinate for the height.

avatar image
1

Answer by ctjagielski · Feb 12, 2020 at 01:27 PM

If you're using 3D, use this:

Physics.OverlapSphere returns an array of colliders within the given radius, from a defined center point.

For this I'd use transform.position for center, and you may need to try a few times to get the radius right. You can use OnDrawGizmos to view the size of the radius in the scene view, just make sure Gizmos are enabled.

public float radius;

 private void Update ()
 {
     Collider[] hitColliders = Physics.OverlapSphere(transform.position, radius);
     for (int i = 0; i < hitColliders.Length; i++)
     {
         GameObject hitCollider = hitColliders[i].gameObject;
         if (hitCollider.CompareTag("Enemy"))
         {
             //add damage
         }
     }
 }

 void OnDrawGizmosSelected ()
 {
     Gizmos.color = Color.white;
     Gizmos.DrawWireSphere (transform.position, radius);
 }
Comment
Add comment · 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

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

121 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

Related Questions

How to apply explosion/magic/grenade damage to detect objects (enemies) within a given radius, and apply AOE damage to each enemy based on its distance? 2 Answers

Is a BoxHandle similar to RadiusHandle possible? 1 Answer

Particle system world collision radius setting 0 Answers

Destroying pixels of a dynamic 2D world based on hardness of a pixel? 1 Answer

How to find the radius area of linerencderer drawn circle and move objects inside ? 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