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 /
avatar image
0
Question by $$anonymous$$ · Jul 27, 2014 at 06:48 PM · physicscollidersphysics2d

[SOLVED] Physics2D OverlapCircle

Hello All,

I would appreciate it if you could help to untie this knot of 2D Physics..

I'm unsuccessfully trying to build a Physics2D.OverlapCircle enemies detection system for my attack system..

This is my situation: Lots of enemies and one melee attack system that has to use the function SendMessage ("Damage").

I know that there is the "trigger on weapon" way but I started with an Aldonetto suggestion and now I want to understand this Physics2D.OverlapCircle..

This is what I've made:

 #pragma strict
 
 var playerHealth : float = 100;
 var playerDamage : float = 25;
 var playerRange : float = 5;
 
 var center : Vector2 = transform.position;
 var radius : float = 5;
 
 //Attack () is called by the touch of the attack button
 function Attack ()
 {
     Enemycheck ();
 }
 
 function Enemycheck (center: Vector2, radius: float)
 {
     var hitColliders = Physics2D.OverlapCircle(center, radius, 1 << LayerMask.NameToLayer("Enemies")); //layermask to filter the varius useless colliders
     
     for(var i : Collider2D in hitColliders)   //here the problem
     {
         var distance = Vector2.Distance(hitColliders.transform.position - transform.position);
         if (distance <= playerRange)
         {
             hitColliders.SendMessage("Damage", playerDamage); //send message damage + damage variable
         }
     }
 }
 
 

Take a look to the line 20. I tried with Aldonetto's way, he use "in" operator that returns true if the specified property is in the specified object. I tried to translate the 3D example of the documentation but they use hitColliders.Length that doesn't work with 2D Collider..

I hope that you'll try to let me understand and if you require any further information about the script or other things, feel free to reply!

Thank you in advance guys!

Comment
Add comment · Show 2
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 $$anonymous$$ · Jul 28, 2014 at 05:04 AM 0
Share

Now that Hazlo gently point out that thing i've just saw OverlapCircleAll, now I find the the same difficults as above..

avatar image Hazlo · Jul 28, 2014 at 10:28 AM 0
Share

Hi again! I edited my answer with more info. Hope it helps.

3 Replies

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

Answer by $$anonymous$$ · Jul 30, 2014 at 06:54 PM

 function Attack ()
 {
     var hitColliders : Collider2D[] = Physics2D.OverlapCircleAll(transform.position, enemyAttackRange, 1 << LayerMask.NameToLayer("Player"));
     
     for (var i = 0; i < hitColliders.Length; i++)
     {
         //hitColliders[i].SendMessage("Damage", enemyDamage);
         if(playerInAttackRange ==true)
         {
             if(Time.time > nextAttack)
             {
                 nextAttack = Time.time + enemyAttackTime;    
                 hitColliders[i].SendMessage("Damage", enemyDamage, SendMessageOptions.DontRequireReceiver);
 //                Debug.Log("Damage sent");
             }                                        
         }
     }
 }



Here the solution that I've found by myself!

However thanks :)

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 DugelStudios · Sep 08, 2015 at 01:35 AM 0
Share

I know this is old, but how do you convert line 3 into c#?

avatar image
0

Answer by Hazlo · Jul 27, 2014 at 10:34 PM

See http://docs.unity3d.com/ScriptReference/Physics2D.OverlapCircle.html. It only returns one collider, the one with the lowest Z. It is different than in 3D, where OverlapSphere returns an array! So you can't use the same code. Sorry.

EDITED: using OverlapCircleAll (sorry I didn't see that function!) this piece of JS code should work fine then:

     for (var i = 0; i < hitColliders.Length; i++)  
     {
         var distance = Vector2.Distance(hitColliders[i].transform.position - transform.position);
         if (distance <= playerRange)
         {
             hitColliders[i].SendMessage("Damage", playerDamage); //send message damage + damage variable
         }
     }
 
 

Note that you have mixed "in" operator with arrays in your first piece of code. You used "in" with "i" variable, but then you didn't reference "i" inside the loop code. The code I wrote up there is using arrays directly (it's more simple). But if you really wanted to use "in", then inside the "for" code you need to use "i" instead of "hitColliders". "hitColliders" is the entire array; "i" is one element for each of the iterations. That's where the problem resides.

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 $$anonymous$$ · Jul 28, 2014 at 05:07 AM 0
Share

Thank you, I've added a comment to my own question, take a look :)

avatar image
0

Answer by gabregni · Jul 27, 2014 at 10:59 PM

Take a look to the range 20. I tried with Aldonetto's way, he use "in" owner that profits real if the specified residence is in the specified item. I tried to convert the 3D example of the certification but they use hitColliders.Length that does not perform properly with 2D Collider.. Spybubble

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Click an object to applyforce in different directions. 1 Answer

OnTriggerEnter2D triggering multiple times when moving around inside the trigger area. 1 Answer

Could someone elaborate on what the CompositeCollider2D.GeometryType documentation means by "unwanted collisions?" 0 Answers

Too sensitive GameObjects 1 Answer

Not all sides of a cube collide properly( with pic) 2 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