Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
1 capture
14 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 /
This question was closed Nov 08, 2017 at 03:25 AM by Bunny83 for the following reason:

The question is answered, right answer was accepted

This post has been wikified, any user with enough reputation can edit it.
avatar image
120
Question by Stelimar · Dec 07, 2009 at 12:08 AM · physicsraycastlayerslayermaskselect

How do I use layermasks?

Layermasks took me a while to figure out and get working correctly, and I've seen a few people asking about them on the forums, so I thought I'd add a little something to help anyone who may be struggling with them.

The question is: How do I use/create layermasks to use with, e.g., Physics.Raycast or other Physics functions? For those who don't know: a layermask allows you to specify certain layers to exclude/include in certain physics functions.

Comment
Comments Locked · Show 1
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 ReytheRed · Apr 24, 2014 at 03:26 PM 0
Share

This question and answers have been very helpful, thanks.

9 Replies

  • Sort: 
avatar image
0

Answer by Bryan-Legend · Apr 15, 2016 at 09:12 PM

Make sure you're not passing in the layer mask in the distance argument position.

Do This

     if (Physics.Raycast(transform.position, transform.forward, out hit, Mathf.Infinity, SelectionLOSCheckMask))

NOT THIS

     if (Physics.Raycast(transform.position, transform.forward, out hit, SelectionLOSCheckMask))
Comment
Comments Locked · 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 castor · Sep 20, 2016 at 04:21 PM

Honestly, after trying all these methods, I find the easiest to use 'GetMask' command:

You set your variable at the start:

     int maskMouseRaycastDefault;

and then in the Awake function, define the names that create that mask. No need for bit shifting, or memorizing what number is what.

 void Awake(){
         maskMouseRaycastDefault = LayerMask.GetMask("Default", "Collision", "DynamicObjs");
 }

The biggest advantage, and the reason why I stopped using the bit shift method, is that you can very clearly read what your layerMask is composed of, and if for some reason you later need to change your layer order, as long as you preserve the names it still works.

Comment
Comments Locked · 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 Owen-Reynolds · Sep 20, 2016 at 05:25 PM 1
Share

Unity didn't have an official Layermask type when this Q was asked, so you had to learn a little about bit-shifting. So the Q is now partly obsolete.

As it is now, bit-shifting is better if you know it. You can pre-define enemyLayer=1<<9 and so forth, then write enemyLayer | fishLayer. Shorter, Intellisense works on it and it runs faster. The layer$$anonymous$$ask class is a front-end if you don't have a program$$anonymous$$g background; which means if you haven't already used bit-operations before, don't waste your time learning them -- use the Layer$$anonymous$$ask class.

avatar image
0

Answer by Adam-Sowinski · Nov 10, 2016 at 01:47 PM

The easiest way of getting the masks is like this:

 var layerNames = new string[] { "YourLayerName" };
 var layerMask =  LayerMask.GetMask(layerNames);
Comment
Comments Locked · 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 shottogan · May 25, 2017 at 07:14 AM

If you simply make a public LayerMask var, multiple layers can be selected simultaneously in the dropdown.

 public LayerMask layerMask;

And then, for example:

 var standing = Physics2D.OverlapCircle( position, radius, layerMask );
Comment
Comments Locked · 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
  • ‹
  • 1
  • 2

Follow this Question

Answers Answers and Comments

36 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

Related Questions

Colliders attached to objects on separate layers are colliding? 1 Answer

How to achieve this basic physics effect? Detect when to change layers 2 Answers

Is it possible to allow a raycast to pass through a collider to hit things behind it? 6 Answers

Help with Layermasks 1 Answer

Raycasting a specific square area 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