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
3
Question by moghes · Jun 11, 2013 at 03:12 PM · layermaskray cast

Use layer name for LayerMask instead of Layer number for ignoring collideres while RayCasting

Hello everyone,

I have a trigger, and I have flying objects. Whenever my flying objects enter the trigger, they will stop moving.

Till now everything is working.

when objects are clicked, they are destroyed, I need a LayerMask to ignore the Trigger, since whenever the objects are inside the sphere they don't get hit by the ray.

So according to this link I have been achieve this, thanks to alucardj. But I need to do this with the layer name, since I might have 15-20 layers and don't want each time go to tag manager to see the number of the layer.

My 10th layer is named "transparent" and I tried this,

 layerMask.value = LayerMask.NameToLayer("transparent");

and I put this line of code in the start function, instead of

 layerMask = 1 << 10;

but no result.. Can SomeOne tell me how to use the layer name, or al least what does the << syntax explain.

 private LayerMask layerMask;

 void Start()
 {
   layerMask = 1 << 10;
   layerMask = ~ layerMask;
 }

 if(Physics.Raycast(ray.origin, ray.direction  ,out hit , Mathf.Infinity , layerMask.value))
 {                
      if (Input.GetMouseButtonUp(0))
      {                    
         if(hit.transform.tag == "targetObject")
         {    
             Destroy(hit.transform.gameObject);
             GameController.AddScore(15);                                        
         }
     }
 } 
 


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

Answer by InfiniBuzz · Jun 11, 2013 at 03:20 PM

Hi

the bitwise shift operator is used to shift bits in vairables. It's a way to mask bits, you can read more here: MSDN bitwise shift reference

However I solved a similar issue by just making the layerMask public and assigned it via Inspector.

 public LayerMask layerMask;

and then as you did

 layerMask.value

in the Raycast.

Hope it helps for you too.

Also see Bunny83's answer to get some clearance :)

Comment
Add comment · Show 5 · 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 Bunny83 · Jun 11, 2013 at 03:42 PM 0
Share

Sorry ;) forgot to upvote ;)

avatar image InfiniBuzz · Jun 11, 2013 at 03:46 PM 0
Share

me too ;) For some reason I didn't realize the NameToLayer()-$$anonymous$$ethod returns the index and didn't take a deeper look because I found another solution, but now I know thanks ;)

avatar image moghes · Jun 11, 2013 at 04:28 PM 1
Share

Bunny's answer was great, but I how didn't I think of having it public.. Its simple and great.. Thank you both

avatar image Darkgaze · Sep 30, 2018 at 07:16 PM 0
Share

What about Layer$$anonymous$$ask.Get$$anonymous$$ask?

avatar image Bunny83 Darkgaze · Sep 30, 2018 at 10:33 PM 1
Share

Get$$anonymous$$ask was added later for convenience. However due to the params array it creates garbage with each call. See the implementation here. The easiest way is to make a Layer$$anonymous$$ask field public / serialized and set them in the inspector. If you want hardcoded layermasks you could still use simple bit shifting. If you want to use Get$$anonymous$$ask you should use it only during initialization

avatar image
5

Answer by Vahradrim · Nov 30, 2015 at 06:50 PM

Men, just use this :

 Mathf.Log(collisionlayerMask.value, 2)

It's that simple (I searched for a while for this so, here it is) (It's the logarithm of the number in base 2 -> it's the opposite of the 2^x function)

I don't comment something often but I was too amazed to see why people struggled so much with bit-twisting.. You're welcome :)

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 _eternal · Apr 12, 2016 at 02:24 AM 0
Share

Does this work if collisionlayer$$anonymous$$ask contains multiple layers?

avatar image burakcatalok · Dec 04, 2018 at 02:18 PM 0
Share

Thanks dude ! You saved my day.

avatar image jbat100 · Jan 24, 2019 at 03:55 PM 1
Share

There's a VERY good reason. Performance. A floating point log base 2 is a LOT slower than a bit shift. Don't do this, read the docs https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

avatar image
4

Answer by Bunny83 · Jun 11, 2013 at 03:37 PM

The function LayerMask.NameToLayer doesn't return a layermask but the layer index. So you still have to bitshift by this index:

 int mask = 0;
 mask |= (1 << LayerMask.NameToLayer("someLayerName"))
 mask |= (1 << LayerMask.NameToLayer("someOtherLayerName"))
 
 mask = ~mask;

But as InfiniBuzz said, just make your LayerMask variable public, then you have a dropdown in the inspector to select the layers you want. The value property will contain the ready to use layer-mask. Depending on how you want to select them you might still want to invert the value:

 public LayerMask ignoreLayers;
 
 //[...]
 
 int mask = ~ ignoreLayers.value;
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 moghes · Jun 11, 2013 at 04:12 PM 0
Share

thumbs up! wish I could accept both answers.

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

20 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

Related Questions

How to create LayerMask field in a custom EditorWindow? 7 Answers

Raycast ignores my layer mask? 8 Answers

Why are raycasts so unreliable? 2 Answers

Using unity 4.6 UI and raycast2D functionality 0 Answers

i want to change only one child GameObject's layer in somewhere middle of a GameObject tree. its not work.. 0 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