• 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
1
Question by Disaster · Apr 28, 2011 at 06:00 PM · physicstrigger

Detecting enemies - SphereCast or Sphere Collider?

I want to be able to detect enemies within a certain range of my player. To do this, I want to build a hash table of known enemies within range of my player and cast rays to see if they are visible or not.

In order to see any enemies are in range of the player, I have two options:

1) Attach a sphere collider to my player, set it to "trigger" and add any enemies who fall into the trigger to my hash table. So far, the results for this seem variable and I'm getting trigger events when other triggers enter the sphere which is undesirable... I only want to listen for colliders.

2) Use Physics.SphereCast at a set interval. This will likely be more accurate.

Considering my scene could have up to about 20 players at a time all checking for enemies, which of the two methods would be considered the most sensible?

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

3 Replies

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

Answer by Bampf · Apr 28, 2011 at 06:23 PM

SphereCast is like raycasting, but instead of casting a single ray it casts a sphere. So this would be more useful for, say, detecting if a thick beam hits something, or if a ball will hit anything as it moves along the direction of the ray.

The sphere collider seems more suited to your task as described, and may be less expensive to check. You will have to skip over collisions with objects you aren't interested in; you can script that, or use Unity's collision layers.

If your player is moving fast enough that it will skip completely over obstacles between frames, then maybe SphereCast is indeed the way to go. You'd be casting between the player's position last frame to the position this frame, and using the distance argument as well. You'd be able to limit the collisions to objects of interest using the layermask argument.

Comment
Add comment · Show 3 · 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 Disaster · Apr 28, 2011 at 06:50 PM 0
Share

$$anonymous$$y players will never be moving that fast, so it sounds like a sphere collider is the way to go.

avatar image Bampf · Apr 28, 2011 at 09:23 PM 0
Share

I was going to say that Bunny83's suggestion of OverlapSphere might actually be superior to a sphere collider, in that it lets you use layers to screen out objects you aren't interested in. But you can use layers with colliders as well. So either way will work. I'll add a note to my original answer.

avatar image Bovine · Feb 01, 2015 at 09:43 PM 0
Share

If you're doing this often though, you're creating a lot of temporary objects which can lead to GC spikes - bad on mobile!

avatar image
0

Answer by Joshua · Apr 28, 2011 at 06:17 PM

I think a trigger sphere would be less heavy for the machine. And if it also fires when other triggers enter, just check if it's a collider. But instead, could you not use isVisible?

Comment
Add comment · Show 2 · 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 by0log1c · Apr 28, 2011 at 06:19 PM 0
Share

Is Visible refer cameras. Which are not always what the game needs.

avatar image Bunny83 · Apr 28, 2011 at 06:21 PM 1
Share

You're right isVisible would be a way to check for visibility. But keep in $$anonymous$$d every camera counts! In the editor the sceneview camera is also a camera. http://unity3d.com/support/documentation/ScriptReference/Renderer-isVisible.html

avatar image
5

Answer by Bunny83 · Apr 28, 2011 at 06:18 PM

In such cases i would recommend Physics.OverlapSphere which gives you an array of all colliders within the sphere radius. Note that you can use a seperate layer for your enemies and specify a layermask when you use OverlapSphere. That way you only get colliders that are on this layer. For the visibility check it seems that you can't get around a raycast. For that purpose i'd like to mention Collider.Raycast which tests only against this collider.

Comment
Add comment · Show 3 · 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 by0log1c · Apr 28, 2011 at 06:20 PM 0
Share

I believe OverlapSphere only return colliders at the same height. It will not return enemies on a slope or in similar situation.

avatar image Bunny83 · Apr 28, 2011 at 06:23 PM 2
Share

What do you mean with at the same height? as long as the bounding box of a collider touches or overlaps the sphere it will be returned.

avatar image by0log1c · Apr 28, 2011 at 06:38 PM 0
Share

I've always believe OverlapSphere was more of a 'circle' than an actual 3d sphere, I thought it was the difference with CheckSphere, now that I re-read the Scripting Reference, I wouldn't know. Disregard my first comment :p

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Is there a way to deny an exit (and therefore OnTriggerExit) 2 Answers

Collider/trigger collision causing physics glitch 0 Answers

Inconsistent Trigger events 1 Answer

overlapsphere to destroy NPCs on exit 1 Answer

My script doesn work Trigger Problem!! HELP!! 3 Answers


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