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
1
Question by XartaX · Apr 03, 2014 at 07:30 PM · raycastlayerslayermask

Usage of Layer masks, what am I doing wrong?

I'm looking into using layer masks to make my raycasts ignore certain layers. I've named Layer 8 (the first one open to editing) "Ground" and set my ground object to this layer, and then tried to use the following code (the idea is to only make the ray hit register if it hits the ground object):

         Ray ray = Camera.main.ScreenPointToRay(origin);
         RaycastHit hit;
         //set layermask to test for "Ground" layer (layer 8)
         int groundLayer = 8;
         int layerMask = 1 << groundLayer;
         if (Physics.Raycast(ray, out hit, layerMask))
             return hit.point;

It operates just like a normal raycast and hits everything not in the "ignore raycast" layer. What am I doing wrong?

I tried looking into it and found links like these:

http://answers.unity3d.com/questions/416906/make-raycast-ignore-some-objects.html

http://answers.unity3d.com/questions/8715/how-do-i-use-layermasks.html

But I don't really get what I'm doing wrong.

Comment
Add comment · 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 koray1396 · Apr 03, 2014 at 07:59 PM 0
Share

It should work normally, can you please try putting $$anonymous$$athf.Infinity before layer$$anonymous$$ask? It might recognize it as distance. another way of defining layermask would be; public Layer$$anonymous$$ask rayLayer$$anonymous$$ask; then you can choose which layers to get affected by the ray.

1 Reply

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

Answer by whydoidoit · Apr 03, 2014 at 07:58 PM

You are passing the layerMask in the distance parameter not the layer mask one.

Comment
Add comment · Show 9 · 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 XartaX · Apr 03, 2014 at 08:01 PM 0
Share

Can you explain a bit more? I mean, look at this screenshot:

http://snag.gy/HmThI.jpg

Looks like a layer mask parameter to me :S

Edit: Oh, never$$anonymous$$d, I think I see. That one lists float distance as the second parameter which is the wrong one. Weird that it did that it'd show that since I already filled in a raycasthit. I'll look into it a bit more.

avatar image whydoidoit · Apr 03, 2014 at 08:05 PM 0
Share

Your picture shows a version of Raycast that takes a distance, you are using the one that has an out Hitinfo - that requires distance and layer mask.

avatar image XartaX · Apr 03, 2014 at 08:07 PM 0
Share

Yeah I see that now. The problem with this is that I'm using this to find a location my mouse is hovering over, so how could I enter the distance as an input before I know where the point is?

avatar image whydoidoit · Apr 03, 2014 at 08:08 PM 0
Share

$$anonymous$$ake it float.PositiveInfinity - that's what happens if you don't pass one.

avatar image XartaX · Apr 03, 2014 at 08:09 PM 0
Share

Oh, the distance parameter is the distance you're checking for a hit in, and not the distance between the origin and the point?

Show more comments

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

22 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

Related Questions

A node in a childnode? 1 Answer

Help with Layermasks 1 Answer

RayCast vision testing for same layer objects? 2 Answers

How do I use layermasks? 9 Answers

Layer mask doesn't work... 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