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 AdrianC · Oct 04, 2012 at 05:14 PM · raycastraycasthitraylayermask

Layermask doesn't seem to work

This is quite odd, I've used layer masks before with no problems, so not sure what is happening. I'm trying to use a simple layer mask.

 int layerMask = 1 << 12;
 layerMask = ~layerMask;
 Ray ray camera.ScreenPointToRay(new Vector3(x, y, 0));
 RaycastHit hit;
 
 if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
 {
   Debug.Log("Hit: " + hit.transform.name + " Layer: " + hit.transform.layer);
 }

So I want to hit all layers except layer 12, but no matter what I do, it always hits it. I even tried taking out layerMask = ~layerMask (which wouldn't make any sense) and of course it still hits it. Both times it prints out Layer 12.

Any ideas?

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

Answer by Screenhog · Oct 04, 2012 at 05:33 PM

When I'm using Layermasks, my preferred method is to do this:

 var mask: LayerMask;

I then use that mask in my Raycast, rather than dealing with bitshifting and all of that.

I realize it's not an explicit answer to your question, but it will solve your problem.

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 AdrianC · Oct 04, 2012 at 06:57 PM 0
Share

Similar thing happens. I'm starting to think there might be a bug with Unity.

avatar image Screenhog · Oct 04, 2012 at 07:03 PM 0
Share

Have you tried a smaller ray? Perhaps $$anonymous$$athf.Infinity is too long to not hit something?

avatar image AdrianC · Oct 04, 2012 at 08:38 PM 0
Share

Yeah, tried messing with it. Same thing.

avatar image
0

Answer by Dragonlance · Oct 04, 2012 at 07:44 PM

I have just tested the layer thing and this works fine for me:

 int layerMask1 = ~(1<<12);
 RaycastHit hit;
 if (Physics.Raycast(transform.position, transform.forward, out hit, Mathf.Infinity,layerMask1))
     Debug.Log("Hit something " + hit.transform.name + " " + hit.transform.gameObject.layer);

Don't know whats wrong in your case.

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 AdrianC · Oct 04, 2012 at 08:39 PM 0
Share

Yeah, I've used layer masks before. At this point I'm pretty sure the project is getting too big and Unity started to mess up.

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

11 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

Related Questions

Why in Unity3D RaycastHit. textureCoord always return 0,0 after building the project? 1 Answer

Exclude tag from raycast 1 Answer

how detect "OnRaycastOut" 1 Answer

Ignore one layermask question 1 Answer

Raycast not visible 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