Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
6
Question by Jan_Julius · Sep 08, 2014 at 03:04 PM · raycastnulllayerlayermask

How can I have a raycast ignore a layer completely?

I got to the point to know how layers work and how to implement them into my code but how can I completely ignore all the objects that are on that layer? and how can I use that correctly in my code without creating any errors or null references?

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

Answer by ThePersister · Sep 08, 2014 at 03:39 PM

This one should be reasonably easy to answer :)

Hitting a certain layer leads to the usage of a LayerMask. Normally, you'll only be hitting that / those layers. To invert a LayerMask, add a cute "~" (squiggly) in front of it.

Here's some example code. Nero Fires Everything once you click your left mousebutton and then tells you what he hit: https://www.youtube.com/watch?v=1Io0OQ2zPS4

The code:

     public LayerMask IgnoreMe;
     private Ray ray;
     private RaycastHit hit;
 
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
             NeroFiresEverything();
     }
 
     public void NeroFiresEverything()
     {
         // Basic example ray
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (Physics.Raycast(ray, out hit, 1000f, ~IgnoreMe))
         {
             Debug.Log("Get Rekt " + hit.collider.name);
         }
     }


You can define what layer(s) to Ignore in the Inspector in a supercool dropdown box. Hope that helps, if anything was unclear let me know, otherwise, please accept the answer :)

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 ginryu · Nov 05, 2021 at 01:41 PM

I found an important thing about layermasking, layermasking seems to fail on even number layers. the time this happenes is when I bitshift int layerMask = gameObject.layer << gameObject.layer; then I use layerMask = ~layerMask; and use that in the layerMask slot of the raycast function, when tested, I found that the ray would ignore all odd numbered layers 1,3,5,7,9, etc. but the layermask would fail on even layers 2,4,6,8,10 etc

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Detecting that I'm clicking a unit even though I'm not? 0 Answers

unity raycast layers in Multiplayer 0 Answers

Raycast not working 2 Answers

LayerMask for RayCast 1 Answer

How to create User layer that acts like the "Ignore Raycast" layer? 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