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 Anymeese · Aug 26, 2014 at 08:47 AM · physicsraycastcolliderrayline of sight

Don't allow raycasts to go through colliders?

How can I do a raycast but limit the ray to Line of Sight?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by Tehnique · Aug 26, 2014 at 09:04 AM

Raycasts don't go through colliders by design, that is their purpose, to detect the first collider the ray intersects. If your raycasts go through colliders, there might be a problem with your setup (maybe you ignore some layers by passing a layer mask to the ray, or you have "IsTrigger" enabled on the colliders).

You can also specify the range for the raycst, if that helps in any way, look at the "distance" parameter when creating a ray.

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
avatar image
0

Answer by AngryBurritoCoder · Aug 26, 2014 at 09:05 AM

Do you mean just raycasting a normal ray ? like this...

 Physics.Raycast(Raycast_Start_Position,Raycast_Direction,Distance);
 

EDIT: If you included layerMask in your ray function, this can cause trouble, since masks allow for rays to go through colliders ( the ones you choose or all if in default)

and maybe a stupid thing to say, but make sure your objects have colliders too xD

Comment
Add comment · Show 2 · 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 Anymeese · Aug 26, 2014 at 06:13 PM 0
Share

Yeah, I forgot to mention that the raycasts all use layer masks! I want a raycast on layermask 1 to stop and return false if it hits a collider on layer 2 before hitting anything on layer 1. Is there an actual way to do this and keep the layermask, or should I do a normal raycast then check hit.collider.gameobject and see if it's on that layer, or what's the best way to handle it?

EDIT: and the colliders that I want to stop it are disabled although the GameObject holding it is active. can i leave them disabled or do I have to keep them enabled?

avatar image AngryBurritoCoder · Aug 27, 2014 at 11:11 AM 0
Share

easiest way to use layermask and raycast ( personally i find this extremely easy) is to make the Layermask public. public Layer$$anonymous$$ask layersToCheck;

What that gives you is an option in the editor to choose which layers you dont want to ignore, then apply that layersToCheck to the raycast and it will work.

so if you want to ignore wall but hit floor, you would check the floor option in the editor window and it will only hit the floor colliders

avatar image
0

Answer by kacyesp · Aug 26, 2014 at 06:32 PM

You must provide the layer you want to hit as the layer mask argument. You must also make sure the colliders you wish to hit are enabled.

Now, given your requirement of "return false if it hits a collider on layer 2 before hitting anything on layer 1" with no other assumptions, what I would do is something like:

 RaycastHit2D rayHit1 = Physics.Raycast( rayOrigin, rayDirection, rayDistance, layer1 );
 RaycastHit2D rayHit2 = Physics.Raycast( rayOrigin, rayDirection, rayDistance, layer2 );

 if ( rayHit1 && rayHit2 )
     if ( rayHit2.distance < rayHit1.distance )
         return false;
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

25 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

Related Questions

why are child colliders sometimes ignored? 2 Answers

Passing values from Shader to C# script 0 Answers

Prevent shooting when gun is inside wall 1 Answer

Raycast 2D with origin inside the collider 1 Answer

Raycast hits when missing a collider, misses when hitting a collider? 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