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 Kalbytron · Mar 12, 2014 at 05:53 PM · raycastcolliderlayermasklaser

Why are raycasts so unreliable?

I am having difficult time dealing with raycasts. I'm trying to use it with layermasks to collide and ignore various objects in the scene. I can't get it to ignore a collider without ignoring all other colliders its supposed to hit...

eg. laser using raycast passes through Player layers (as expected), but then after that passes through everything else (not good).

So I need a bit of a lecture of how to use raycasts and layermasks.

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 Owen-Reynolds · Mar 14, 2014 at 01:53 PM 0
Share

Take the code you posted below, as a comment, and turn it into a question. Write down the values for the public variables. Explain what layers you have set, how objects are set, and where the problem is. Give it a descriptive title, such as "raycast with layermask skips everything."

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Owen-Reynolds · Mar 12, 2014 at 10:50 PM

Yes, raycasts are tricky -- the coding is more difficult, and lots of special rules. But they are reliable, once you figure them out.

You can't see them, ever. And if you use Debug.DrawRay, you can easily show the wrong line, making it even worse.

Returning true/false and also the RayCastHit by reference is a standard trick, but not many people are used to it. The 9 overloads are odd, and it's easy to enter distance where the layer mask should have been. Layermasks are in binary, but layers aren't. Sometimes a raycast is "good" if it hits the right thing, or if it misses everything... .

But treat them as something tricky, and go slow, set up simple tests and print a lot. You can find many, many examples here, of how layer masks work, etc.. . Really, there are dozens of "firing a bullet using a raycast" threads.

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 Kalbytron · Mar 14, 2014 at 03:04 AM 0
Share

Alright, so do you know a site where I can figure out how to ignore one layer and not everything after that?

avatar image getyour411 · Mar 14, 2014 at 03:15 AM 0
Share

https://docs.unity3d.com/Documentation/Components/LayerBasedCollision.html

One post that vaulted me past 'how the heck do I use these layers' to 'SWEET!' demonstrated a simple variable definition for my C# class:

 public Layer$$anonymous$$ask layermask;

which then put a dropdown list in the Inspector and simplified most of my Raycast issues to check/uncheck, using 'layermask' variable in the Raycast. I don't know if you've already done that, but if not try it out.

avatar image Kalbytron · Mar 14, 2014 at 04:06 AM 0
Share

I just did some testing it turns out that if I add a layermask parameter, it goes through EVERYTHING. So yeah, what the heck?

avatar image getyour411 · Mar 14, 2014 at 04:07 AM 0
Share

Adding it is step #1, did you then go into Inspector and use the dropdown to check/uncheck ?

avatar image getyour411 · Mar 14, 2014 at 04:37 AM 1
Share

You are bitshifting (I think it's called) and that may or may not be ok but it's not how I use it. Try this ins$$anonymous$$d

 Physics.Raycast (transform.position, transform.forward, out rayHit, maxDistance, mask);
Show more comments
avatar image
0

Answer by RyanZimmerman87 · Mar 14, 2014 at 04:42 AM

I feel your pain with RayCasts haha. My game relies entirely on using a ton of them from the player movement commands, selecting objects, firing laser beams, etc.

It is a LOT of work to set up a complicated game relying on so many RayCasts instead of simple collisions and more traditional player movement controls.

For just ignoring one layer and being able to hit others is very simple though once you know how to do it. Once you have a ton of different layer interactions that's when it becomes so tedious to make everything work perfectly.

Here's a little example of how to do what you want if I'm understanding your question correctly.

This example might be a little excessive (meaning some parts may be unnecessary I'd have to test to know for sure) but it works, some of the stuff I'm doing with Layer Masks seems strange but this is what I learned about it so far and it works at least.

 //start position of Rayshot
 public Transform rayStartPosition;
 
 //ray shot length
 float maxRayLength = 50;
 
 //variables to store the rayHit info
 GameObject rayHitObject;
 String rayStringName;
 Vector rayVectorHitPosition;
 
 //declare LayerMask variables for player
 LayerMask playerLayerMask;
 int playerLayerMaskInt;
 
 void Start()
 {
 //this is what seems weird but it works
 //The "8" is the "User Layer" when you
 //click on the "Add Layer" option in Inspector
 playerLayerMaskInt = ~(1<<8); 
 playerLayerMask = playerLayerMaskInt;
 }
 
 //casting the ray example
 RaycastHit rayHit;
 
 if (Physics.Raycast(rayStartPosition.position, rayStartPosition.forward, out rayHit, maxRayLength, playerLayerMask))
 {
 //get the gameObject
 rayHitObject = rayHit.collider.gameObject;
 
 //get the name of object
 rayStringName = rayHitObject.name;
 
 //get the position of the rayhit
 rayVectorHitPosition = rayHit.collider.gameObject.transform.position;
 }
 
 

You can add more layers to ignore by declaring them like this example and simply adding a & between each LayerMask variable so it ignores every layer you want. Example:

 if (Physics.Raycast(rayStartPosition.position, rayStartPosition.forward, out rayHit, maxRayLength, playerLayerMask & ignoreRayLayerMask))


So if you do it like that it's actually pretty simple for what you are trying to do, but it is strange to learn it, and setting up an entire game around raycasts is very tedious for something like a Diablo 2 style of gameplay where everything is controlled by mouse clicks, or mobile screen taps.

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 getyour411 · Mar 14, 2014 at 04:49 AM 0
Share

Interesting, I see you are also doing some kind of shifting on 21. I declare the layermask as you did on 13 but public, which exposes it in Inspector, then I check/uncheck depending on need and then use a line like yours on 28

 if (Physics.Raycast(rayStartPosition.position, rayStartPosition.forward, out rayHit, maxRayLength, playerLayer$$anonymous$$ask))

but I don't do the bitshift/int stuff you are doing.

avatar image Kalbytron · Mar 14, 2014 at 04:54 AM 0
Share

You know I find it strange that its only able to hit things that are NOT checked to hit!!!! WHY UNITY WHY. IS THIS A BUG?

avatar image Kalbytron · Mar 14, 2014 at 05:00 AM 0
Share

I'm finding out that the 4 factory setting layers work when checked and the user made layers work when unchecked. :| This is messed up unity. ()

avatar image Kalbytron · Mar 14, 2014 at 05:22 AM 0
Share

You know what I'm starting to think that this layermask raycast is not listening to the layer collision matrix. It only seems to collide with objects that are checked in the gameobject script itself. Now I'm seeing how this works.

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 question about "Physics.Raycast()" 1 Answer

Can't get a laser working properly. 2 Answers

LayerMask for RayCast 1 Answer

RaycastHit returns object without collider, in wrong layer 1 Answer

Colliders attached to objects on separate layers are colliding? 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