Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Sendatsu_Yoshimitsu · Jun 11, 2014 at 03:34 AM · c#collideroverlapspherespherecast

Overlapsphere not returning all colliders it touches

I'm using an overlapsphere to detect whether enemies are close enough to be hit, iterating through the array it generates, and applying damage to every enemy within range. The problem is, enemies only take damage inconsistently: I've put three enemies in a cluster around me, make the overlapsphere more than big enough to cover them all, and watched as only one or two of the pack take damage. Am I somehow missing something in my iteration?

Below is the function I'm trying to fix:

 void Attack(){
     center = transform.position;
             Collider[] hitColliders = Physics.OverlapSphere (center, radius);
     for (int i = 0; i < hitColliders.Length; i++){
                 if (hitColliders[i].gameObject.tag == "Enemy"){
                     distanceToEnemy = Vector3.Distance(transform.position, hitColliders[i].gameObject.transform.position);
                     if (distanceToEnemy <= attackRange){
                         hitColliders [i].gameObject.SendMessage ("ApplyDamage", damage);
                         Debug.Log ("Applied damage");
                     }
         
                 }
         i++;
     }
                 
 }
Comment
Add comment · Show 3
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 Tarlius · Jun 11, 2014 at 05:56 AM 0
Share

Derp question, but needs asking: You made sure they are all inside "attackRange" as well as "radius", right?

Is there a reason you have a radius and a range?

avatar image Sendatsu_Yoshimitsu · Jun 11, 2014 at 06:33 AM 0
Share

Good question to ask, and I did- I have a gizmo which draws a sphere to the same dimensions as my overlapsphere, and to test this I set range to an absurdly high number, verified they were both in the sphere and in range with a drawline, and it still didn't consistently pick up every enemy whose collider was in range.

I have both because I want to make sure range is being calculated based on the enemy model's position, not their collider position. Later on I might end up using colliders as vision/sensory cones, for instance, and having this set up in advance lets me avoid punching anything that can see me.

avatar image Sendatsu_Yoshimitsu · Jun 11, 2014 at 07:26 AM 0
Share

I've cleaned up and improved my code a bit, current version below:

 void Attack(){
     anim.SetTrigger("Punch");
     Collider[] hitColliders = Physics.OverlapSphere (transform.position, radius);
     for (int i = 0; i < hitColliders.Length; i++){
                 if (hitColliders[i].gameObject.tag == "Enemy"){
                     
                     distanceToEnemy = Vector3.Distance(transform.position + new Vector3 (0,1,0), hitColliders[i].gameObject.transform.position);
                     directionToTarget = (hitColliders[i].gameObject.transform.position - transform.position).normalized;
                     if (distanceToEnemy <= attackRange && (Vector3.Dot(directionToTarget, transform.forward) > 0.85)){
                         hitColliders [i].gameObject.Send$$anonymous$$essage ("ApplyDamage", damage);
                         Debug.Log ("Applied damage");
                     }
         
                 }
         i++;
     }
                 
 }

Oddly enough the same problem persists- very occasionally, one enemy will not detect the attack, and repositioning myself doesn't change things- the only way to make him register the hit is to step back until he's outside the sphere, and then re-approach, at which point he takes damage.

1 Reply

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

Answer by Nick4 · Jun 11, 2014 at 07:36 AM

I think it's because you're increasing i variable twice.

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 Sendatsu_Yoshimitsu · Jun 11, 2014 at 07:56 AM 0
Share

facepalm I don't know how I didn't catch that before, but you just made my day! Thank you :)

avatar image Nick4 · Jun 11, 2014 at 07:59 AM 0
Share

no problem! :)

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

23 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

Related Questions

Want to spawn a box on other box but don't want those boxes to have same tag on each other using tag 0 Answers

Distribute terrain in zones 3 Answers

Unity C# Bomb Radius Issue 0 Answers

Multiple Cars not working 1 Answer

OverlapSphere ignoring all colliders when I use the layerMask parameter. 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