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 BitwiseGuy · Apr 15, 2015 at 09:33 AM · raycastlayermask

Raycast can't detect objects on layermask

I have two main layers being used in this particular part of my code - Player and StaticWorld.

During play, I try to run a raycast from the player to a location, and see if there's anything that would collide with that raycast in the StaticWorld layer.

If I use

 RaycastHit2D hit = Physics2D.Raycast(gameObject.transform.position, clickDirection, clickDistance);
 if (hit)
 {
     Debug.Log ("Hit on layer " + hit.transform.gameObject.layer + ", Target layer: " + LayerMask.NameToLayer("StaticWorld"));
 }

Then when it hits objects in the StaticWorld layer, I get the message

"Hit on layer 8, Target layer: 8".

If I try to use

 RaycastHit2D hit = Physics2D.Raycast(gameObject.transform.position, clickDirection, clickDistance, LayerMask.NameToLayer("StaticWorld"));
 if (hit)
 {
     Debug.Log ("hit on layer " + hit.transform.gameObject.layer + " Target layer: " + LayerMask.NameToLayer("StaticWorld"));
 }

then I cease to get any raycast hits on the same objects from the same location. I've also tried using the static value 8 for the layermask with the same results. Am I missing something here?

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

Answer by YoungDeveloper · Apr 15, 2015 at 09:37 AM

Are you sure LayerMask.NameToLayer("StaticWorld") is correct? If you layer is with index 8, try this.

 int mylayer = 1 << 8;
 
 if(Physics2D.Raycast(gameObject.transform.position, clickDirection, clickDistance, mylayer){
     Debug.Log ("hit");
 }

To see what and where you are casting you can use ray debug.

 Debug.DrawRay(gameObject.transform.position, clickDirection, Color.red);


Comment
Add comment · Show 7 · 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 BitwiseGuy · Apr 15, 2015 at 09:46 AM 0
Share

I was using Debug.DrawRay previously just to make sure that the ray was actually there - I still have it, it's just outside the 'problem code." Shifting the layermask value seemed to do it - Does this mean that I should be doing (1 << Layer$$anonymous$$ask.NameToLayer(layerName))? Even setting a layermask variable seems like it should return 8 in this case.

avatar image YoungDeveloper · Apr 15, 2015 at 10:05 AM 0
Share

I've never used Layer$$anonymous$$ask.NameToLayer(layerName), looks like too much for me. Use bit shifting 1 << 8 or simply declare public Layer$$anonymous$$ask layer; and toggle what layers you want to cast from inspector.

avatar image BitwiseGuy · Apr 15, 2015 at 10:27 AM 0
Share

Right, what I was asking there is how does declaring public Layer$$anonymous$$ask layer work when referencing it when gameObject.transform.layer is still spitting out 8 and the layer is actually 256 (1 << 8)?

avatar image YoungDeveloper · Apr 15, 2015 at 10:30 AM 0
Share

It's 'spitting' out it's current active layer which has nothing to do with bit shifting 1 to position 8.

avatar image YoungDeveloper · Apr 15, 2015 at 10:56 AM 1
Share

Here's a very good explanation of how it works on lowest level.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

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

RaycastHit returns object without collider, in wrong layer 1 Answer

Using a raycast to get transform on specific layer 3 Answers

Raycast should ignore everything but player, yet still gets interrupted 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