Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 Bhanuka_Dassanayake · Dec 29, 2021 at 02:05 AM · physics2dlayermaskoverlapsphere

OverlapCircleAll, layerMask ( Problem)

Hi,

this is my code,

         if(targetFood == null)
         {
             Collider2D[] allFood = Physics2D.OverlapCircleAll(antPosition, viewRadius, 7);
 
             if(allFood.Length > 0)
             {
                 Debug.Log("Find Food");
                 //Select one food Randomly
                 Transform food = allFood[Random.Range(0, allFood.Length)].transform;
                 //Find Dir to Food
                 Vector2 dirToFood = (food.position - transform.position).normalized;
 
                 //Search is it in ant's view angle (Simulating that ant smell that food)
                 if(Vector2.Angle(transform.position,dirToFood) < viewAngle / 2)
                 {
                     food.gameObject.layer = takenFoodLayer;
                 }
                 targetFood = food;
             }
         }




I have foods in my scene and ants randomly move until they find food. I only need to detect foods. I make "Food" layer for foods. and it's layer 7. but it's not debug when food in it range. but when I remove 7, it work. but it work for every layers.

alt text

have a good day, thanks.

screenshot-2021-12-29-at-073010.png (327.3 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

2 Replies

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

Answer by Aluminum18 · Dec 29, 2021 at 02:53 AM

The LayerMask is acctually not a number, it is bitmask. You can use LayerMask.GetMask ("YourLayerString") to get the mask then past this mask into the Physics function.

You can find an example of using LayerMask here LayerMask

When you remove "7", the method works for all layers because it uses the default params which is EveryThing

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
avatar image
0

Answer by Bhanuka_Dassanayake · Jan 01 at 07:57 PM

@Aluminum18

Actually I find a way, in this I need it for layer 7 and it's not work. but when I use 1 << 7 instead of 7, it work. can you please tell me if you know what this mean (1 << 7) it to work.

 Collider2D[] allFood = Physics2D.OverlapCircleAll(antPosition, viewRadius, 1 << 7);
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 Aluminum18 · Jan 02 at 12:54 AM 0
Share

"<<" is left bit shifting operator (">>" also exists as right bit shifting). integer in C# is a 32-bit data type. Value 1 is presented as

0000_0000_0000_0000_0000_0000_0000_0001. 1 << 7 turns it into

0000_0000_0000_0000_0000_0000_1000_0000 which is the actual value of Layer "7".

The number you see in Layer settings represents how many bit you need to left shifting to get it actual value.

avatar image Bhanuka_Dassanayake Aluminum18 · Jan 02 at 06:40 AM 0
Share

Thank You :)

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

135 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

Related Questions

OverlapSphere doesn't detect colliders 1 Answer

Does adding a Layermask improve efficieny of Physics.OverlapSphere? 1 Answer

OverlapSphere ignoring all colliders when I use the layerMask parameter. 1 Answer

Physics2D.Raycast is fine, I'm an idiot 2 Answers

Replacing OverlapSphere with colliders (Detecting weapon collision with enemies - SOLVED) 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