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 Nanako · Feb 17, 2015 at 05:42 AM · masklayer

How can i get a layer's collision mask?

I want to cast a ray with the same collision properties that it would have, if it were an object on a specific layer. The raycast should hit anything that object would collide with, and ignore everything that object would pass through.

I can manually copy this information and hardcode it, but that seems wrong, especially since i'd have to update that if i change the collision settings later

Is there any script command where i can specify a layer, and it'll return a layermask for how that layer collides with others?

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

1 Reply

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

Answer by Baste · Feb 17, 2015 at 09:49 AM

There's a method named Physics.GetIgnoreLayerCollision(int layer1, int layer2). It returns true if layer1 and layer2 doesn't collide.

So to set up the layermask for your raycast, you would get the layer of your object, and then create a layermask with a for-loop:

 int myLayer = gameObject.layer;
 int layerMask = 0;

 for(int i = 0; i++; i < 32) {
     if(!Physics.GetIgnoreLayerCollision(myLayer, i))  {
         layerMask = layermask | 1 << i;
     }
 }

That should give you the correct layermask.

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 Nanako · Feb 17, 2015 at 11:57 AM 0
Share

that seems like a pretty roundabut way of doing things :/ i guess it'll do, but shouldn't this data be cached and retrievable somewhere?

avatar image Baste · Feb 17, 2015 at 12:52 PM 1
Share

You're right about it being roundabout, but you just have to do it once, in the start method:

 private int layer$$anonymous$$ask;

 void Start() {
     int myLayer = gameObject.layer;
     int layer$$anonymous$$ask = 0;
 
     for(int i = 0; i++; i < 32) {
         if(!Physics.GetIgnoreLayerCollision(myLayer, i))  {
             layer$$anonymous$$ask = layermask | 1 << i;
         }
     }
 }

and then when you raycast:

 void DoTheRaycast(Vector3 direction, float length) {
     RaycastHit hit;
     if(Physics.Raycast(transform.position, direction, out hit, length, layer$$anonymous$$ask) {
         Debug.Log("I hit: " + hit.collider);
     }
 }

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 make an animation that changes color in a clockwise rotation 2 Answers

Linecast Layer Mask Declaration Problem 2 Answers

culling mask on camera ignores layer on lights? 1 Answer

Material: What is a "Mask map" and when to use it? 0 Answers

Dealing with multiple crosshairs 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