Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
0
Question by hsnzkg · May 14, 2021 at 09:40 AM · raycastraycastinglayersraycasthitlayermask

Layer Mask Detection

alt text

I have a two spesific layer mask. When I click the blocks if the block has destructable mask i am firing a rocket but in second section when i click to block that has core mask Unity still returning 0 and firing rockets.

Where I did wrong I define the rays for specific layers but it gives the same results only returnin 0 when I click the any block ?

Thanks In Advance !

adsız.png (36.7 kB)
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 andrew-lukasik · May 14, 2021 at 10:21 AM 0
Share
 if( Physics.Raycast(out var hit) )
     if( hit.collider )

This if( hit.collider ) is redundant as Physics.Raycast()==true guarantees a collider been hit.

2 Replies

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

Answer by Pangamini · May 14, 2021 at 10:37 AM

Mask filtering means that the raycast will pass through colliders that don't match your mask. This means, that if there's an object that matches destructableMask behind the object that matches the coreMask, you will always hit the destructibleMask object in the first raycast. I am just guessing what you are trying to do. But most likely, a better solution would be to have a single raycast, whose mask matches everything you want to hit potentially, then determine what kind of object you've clicked. Also paste your code as text, not as a screenshot, it makes it easier for people to copy and modify it.

 if(Physics.Raycast(ray, out hit, mergedMask))
 {
     int objectLayer = hit.collider.gameObject.layer;
     if (1<<objectLayer  & destructableMask)
         return 0;
 
      // ...........etc
 }

Comment
Add comment · Show 4 · 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 hsnzkg · May 14, 2021 at 10:46 AM 0
Share

If something goes wrong in the future, you can be sure that I will paste the code :)

Thanks a lot !

avatar image andrew-lukasik · May 14, 2021 at 10:46 AM 0
Share

+1

Also, to better understand what is happening here I suggest you add these debug lines there:

 if( Physics.Raycast( ray , out var hit , mask  ) )
 {
     Debug.Log( $"mask 1, collider hit: {hit.collider.name}" , hit.collider.gameObject );
     Debug.DrawLine( ray.origin , ray.origin+ray.direction*hit.distance );
 }

It will draw it as a ray in scene view and also clicking on these console logs should select a gameObjects that was hit.

avatar image Bunny83 · May 14, 2021 at 10:49 AM 2
Share

I was too slow -.-

avatar image Pangamini Bunny83 · May 15, 2021 at 12:01 PM 0
Share

That's why I have to write shorter, more compact answers. I know that you are out there somewhere answering at the same time :D

avatar image
3

Answer by Bunny83 · May 14, 2021 at 10:49 AM

Well, I think you have the wrong idea about the layermask. The layermask does not only filter the results of the raycast, but also only casts against those objects. So your ray becomes an "x-ray" for all objects that do not match the layermask. So for example, if your player object is in a layer player and you cast a ray just against the player layer, you can still hit the player even if there's some other geometry in between. Only things that are included in the layermask will participate in the raycast-


That means if you want the raycast to be "blockable" by default geometry, you have to include the default layer as well, otherwise they will be ignored. The layermask is not for filtering the endresult.


Now it depends on what you want to achieve. In most raycast scenarios you usually want to include blocking geometry. So you have to include all layers that should participate in the raycast. That means you essentially need one layermask and only one raycast. Of course in order to determine what was hit, you have to analyse the hit object. The gameObject of the collider has a layer property which you can use to further filter your results. For this you can use your two layermasks. So the code would look something like this:

 if (Physics.Raycast(ray, out hit, allRelevantLayers))
 {
     int goLayerMask = 1 << hit.collider.gameObject.layer;
     if ((goLayerMask & destructableBlocks.value) != 0)
     {
         return 0;
     }
     if ((goLayerMask & coreMask.value) != 0)
     {
         return 1;
     }
 }
 return 0;

Note I've simplified the code since you did not post the code as code so we can't copy it for the answer. However the overall idea here is that "allRelevantLayers" is a layermask that contains all layers that you want to be able to "hit". So the layers you're interested in as well as blocking geometry (usually the default layer). You could create that layermask by combining your two layermask and add the blocking geometry layers as well by doing


 int allRelevantLayers = destructableBlocks.value | coreMask.value | blockingLayers.value;


Now our raycast will only consider those layers. When we hit "something" from those layers, we determine what we hit based on your two layermasks.


Note I would highly recommend that you avoid magic numbers like 0, 1 and 2. If you want to communicate a certain outcome, use an enum and give the 3 cases meaningful names.

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 hsnzkg · May 15, 2021 at 12:40 PM 1
Share

Really thank you for explaining in great detail!

At the same time, as you said enum logic, the more useful the project grows, the harder it is to understand the integer values: D

Good answer !

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

174 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 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 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 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 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 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 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 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

Ignore one layermask question 1 Answer

Help Can't find the Correct Parameters 1 Answer

Help with Layermasks 1 Answer

Raycast works perfectly, but only in editor 0 Answers

Boxcast vs Raycast Oddities 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