Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 llnk7 · Apr 07, 2016 at 08:11 AM · raycastraycasthit2dlayermask

Make mask layer have priority over other mask layer on raycast

Hi, I would like to know if there is a way to make raycast hit a layer with more priority than other layer.

I have two layers which I can click on them, but I want to raycast on one layer before another one, so is there a way o I have to use raycastAll and then check all elements and code the priority myself. I was hoping with the order in the layer editor the raycast check one layer before another one but it doesn't.

I hope I was clear enough.

Thanks!

Comment
Add comment · Show 4
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 llnk7 · Apr 14, 2016 at 02:25 AM -1
Share

Wow! Thanks everyone for the support! I don't even have time to read all the answers! ... ¬¬

avatar image Fredex8 · Apr 14, 2016 at 02:40 AM 0
Share

It would be helpful if you explained why you need to do this as there are a couple of options but without knowing what the intended behaviour is it's hard to say what will work.

Using a Layer$$anonymous$$ask you can set the raycast to only hit the layers you specify or to ignore the layers you specify and hit everything else. You could use this to raycast against one layer and then the other to get the order you are looking for. However once it finds something on the layer it is looking for it will stop.

RaycastAll is probably a better idea and then just store the hits in the order they come in or calculate the distance between the hit and the source if that's what you are trying to use this for.

In regards to 'priority' the raycast is just going to hit whatever collider it encounters first. If those colliders are on top of each other then I suspect it will be based on scene hierarchy. Unless it is using a layermask, the layers the objects are on isn't going to make a difference.

avatar image llnk7 Fredex8 · Apr 15, 2016 at 01:25 AM 0
Share

Thanks for the answer! Ok I totally forgot to mention that I'm working on 2D. The idea to use 2 raycasts is good but is it expensive for mobiles?

Imagine I have multiple things in my scene, all of them are interactive (for example furniture) and I have also characters there, and when click on one of them I want to prioritize the click on the character even if there is a "furniture" in front of it.

What I'm currently doing is raycastAll and then I have a script that has a priority variable, which I read in the raycastAll method and then order by this variable. Another solution I thought was to move on Z the sprites, and then use the order in the sprite renderer, but I really didn't want to use the Z so I used the raycastAll option.

I just wanted to know if there was a way to tell the raycast to prioritize one layer over other. Thanks!!

avatar image Fredex8 llnk7 · Apr 15, 2016 at 06:10 AM 0
Share

Using a layer mask probably is the best idea then. Changing the Z depth so the character is in front of furniture would work but it'd still cause an issue if you wanted to click furniture behind them.

RaycastAll is probably more expensive I'd think as it is returning multiple raycasts anyway. Whereas raycasting once with layer mask furniture and/or once with character is going to be more simple.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by raybarrera · Apr 14, 2016 at 02:38 AM

The only way I can think of doing this is by having two separate raycasts. I can see how the term "layer' is a bit misleading in this scenario, but as you mentioned, the order has no bearing on how a raycast works (when doing RaycastAll in this manner).

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Layer Mask on raycast2d not working? 1 Answer

Why is RaycastHit2d Not working 0 Answers

Making a raycast ignore specific collider 1 Answer

Raycast Not Working 3 Answers

My Raycasts seem to sometimes miss 0 Answers


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