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 iron_attorney · Jun 18, 2015 at 11:16 PM · c#raycasthitraycastall

CircleCastAll layermask doesn't seem to work...

Hi there, I have made a script in which my AI character circlecasts around itself to detect food. My script appears to work fine except the layermask doesn't seem to be doing anything. This is my script so far:

 public class FindFood01 : MonoBehaviour {
 
     float rayRadius = 2;
     Vector2 position2D;
 
     void FixedUpdate () {
     
         if (GetComponent<AI> ().targetFood == null) {
 
             RaycastHit2D[] hit;
 
             position2D = new Vector2 (transform.position.x, transform.position.y);
 
             hit = Physics2D.CircleCastAll (position2D, rayRadius, position2D, 0f, 9);
 
             GetComponent<AI> ().targetFood = hit [0].transform.gameObject;
 
         }
     }
 }

And I have an AI script that deals with moving the charactor to the target. That bit works fine. The integer 9 is the layer for the food, and I seem to have put this in the correct place. The results though, are that the character detects everything. I can't tell if it detects anything preferentially or not, but it appears to detect food, the player or sometimes itself (and moves in a straight line forever trying to chase itself). Any ideas on what I've missed here?

To clarify in advance, the AI script does nothing until there is a target, and then moves towards it, so it's not that. And I've checked and tripple checked that food is definately layer 9, and that the layer food is only assigned to the food, everything else is on default layer.

Cheers in advance,

Pete

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
2
Best Answer

Answer by Xarbrough · Jun 18, 2015 at 11:25 PM

Layermasks in Unity are stored as a bitmask, so you don't input the layer number into the function, but it's bit-value. So something like 1 << 9 (a bitshift to the left 9 times). To make this much more easy, I would define a public LayerMask field and use the built-in enum dropdown of the Inspector to pick my layer mask.

LayerMask documentation

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 iron_attorney · Jun 19, 2015 at 10:27 AM

Hey thanks man! That's perfect. It hadn't occured to me that I should actually read about how the layerMasks work, thought they would just be the straight forward layer numbers. To clarify, this is my solution based on your answer:

 public class FindFoodSmellClosest : MonoBehaviour {
 
     float rayRadius;
     Vector2 position2D;
 
     LayerMask foodMask;
 
 
     void Start () {
 
         foodMask = LayerMask.GetMask ("Food");
 
         rayRadius = Random.Range (1.0f, 5.0f);
     }
 
     void FixedUpdate () {
     
         if (GetComponent<AI> ().targetFood == null) {
 
             RaycastHit2D[] hit;
 
             position2D = new Vector2 (transform.position.x, transform.position.y);
 
             hit = Physics2D.CircleCastAll (position2D, rayRadius, position2D, 0f, foodMask);
 
             if (hit [0] != null) {
                 GetComponent<AI> ().targetFood = hit [0].transform.gameObject;
             }
         }
     }

Using the line "foodMask = LayerMask.GetMask ("Food");" I don't have to find out what the actual the number is, though I debugged it and it's 512. This means the number must be 2 ^ (layer number, in this case 9), which admittedly is what you already said with "1 << 9 (a bitshift to the left 9 times)".

Cheers very much man!

Pete

Comment
Add comment · Show 4 · 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 Xarbrough · Jun 19, 2015 at 04:04 PM 1
Share

Glad I could help. And also remember, that if you make a Layer$$anonymous$$ask variable public or [SerializeField] it will show a nice dropdown in the Inspector to pick from. That way you don't have to use constants in your script.

avatar image iron_attorney · Jun 20, 2015 at 12:13 PM 0
Share

Ah right! That's what you meant, I get ya now, that's even better. Out of interest, could I add the bit values of multiple different layer masks together to search for multiple layers? Like if I wanted layer 3 (100 / 4) and layer 5 (10000 / 16) I could search for 10100 (or 20). If on the off chance it searched based on the active bits, there's a possibility that could work right? Or is there another way of doing that without just doing another raycast?

avatar image Xarbrough · Jun 20, 2015 at 12:26 PM 1
Share

Yes that's why they chose to use bitmasks, because you can select multiple ones in one number. So if you want layer 3 and 6 you could do (4 || 16) using the OR operator. You can lookup AND OR Bitshifting Bitmasks if you want a detailed tutorial. Generally, keep in $$anonymous$$d that if you want to include several layers you need OR because you don't care if it is layer 3 or layer 5. If you use AND (&&) it will only work if objects have layer 3 and layer 5.

avatar image iron_attorney · Jun 20, 2015 at 01:00 PM 0
Share

Awesome stuff! That's solved a different problem that I was going to face soon. Cheers for all your help buddy!

Pete

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Problem with ray tracing and obstacle detection 0 Answers

Making a bubble level (not a game but work tool) 1 Answer

An OS design issue: File types associated with their appropriate programs 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