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 MultiSpoone · Jan 30, 2015 at 05:51 PM · raycastcolliderboolean

Make Raycast ignore collider on boolean

Hey guys! very simple question. how do i make that my raycast ignore some kinds of colliders and just pass through them? I got tags for those colliders but i dont know how i can make that i can switch it on and off.

 var hit : RaycastHit;
 var fwd = transform.TransformDirection(Vector3.forward);
 var Raycastignore : boolean;
     
 if(Physics.Raycast(transform.position, fwd , hit, rayLength))
 {
   if(!Raycastignore)
   {
     if(hit.collider.gameobject.tag == "collider")
      {
       do stuff
      }
    else
    {
     if(hit.collider.gameobject.tag == "collider")
     {
      just ignore and pass through and then do stuff there.
     }
    }
 }


On the forum I just found on making raycast ignore objects but I didnt foudn how to make it with a boolean. thanks for help!

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

Answer by pansapiens · Jan 31, 2015 at 02:01 AM

Rather than use tags, you should use Layers. Put the objects you want to ignore in a different Layer (or Layers), then do something like this:

 var hit : RaycastHit;
 var fwd = transform.TransformDirection(Vector3.forward);
 var hitLayers : LayerMask;
 var ignoreLayers: LayerMask;
 private var _raycastLayers : LayerMask;
 var RaycastIgnore: boolean;

 if (RaycastIgnore) {
     var invertedHitLayers = ~hitLayers;
     _raycastLayers = ~(invertedHitLayers | ignoreLayers);
 } else {
     _raycastLayers = hitLayers;
 }
      
 if(Physics.Raycast(transform.position, fwd , hit, rayLength, _raycastLayers))
 {
     if(hit.collider.gameObject.CompareTag("collider"))
     {
          if (RaycastIgnore) {
            // do stuff
          } else {
            // do other stuff
          }
     }
 }

hitLayers are all the layers you normally want the raycast to hit. ignoreLayers are the layers to ignore when RaycastIgnore = true. These will appear as a dropdown in the Inspector where you can select the layers required.

We do the actual raycast using the _raycastLayers variable - toggling RaycastIgnore will change this from ignoring those layers to hitting those layers.

Note the use of the tilde (~) - this is a bitwise operation that inverts a LayerMask so that the ray will not hit those layers. The pipe | symbol is a bitwise OR, which effectively combines two layer masks. The lines: var invertedHitLayers = ~hitLayers; _raycastLayers = ~(~hitLayers | ignoreLayers); inverts hitLayers, adds ignoreLayers, then inverts everything - if my logic is correct, this will remove ignoreLayers from hitLayers.

A simpler alternative if you want to avoid the bitwise operations could be to just define one LayerMask for when RaycastIgnore = true and another for when RaycastIgnore = false.

Related Unity docs: "Casting Rays Selectively".

Comment
Add comment · Show 6 · 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 pansapiens · Jan 31, 2015 at 02:02 AM 0
Share

Also note that I'm using CompareTag rather than ==, since it's apparently more efficient.

If you are doing lots of manipulation of Layer$$anonymous$$asks (eg inversion etc), I suggest taking a look at Layer$$anonymous$$askExtensions on the Unity wiki.

avatar image MultiSpoone · Jan 31, 2015 at 07:30 PM 0
Share

thanks. but how i make it switchable with an boolean?

avatar image pansapiens · Jan 31, 2015 at 11:03 PM 0
Share

A simple way would be to toggle between using two layer masks, depending on what you want to hit in each state. I've updated the answer with a version of what I think you are trying to achieve.

avatar image MultiSpoone · Feb 01, 2015 at 10:21 AM 0
Share

thanks man. thank you very much! i think I´ll be able to do now what i want

avatar image pansapiens · Feb 01, 2015 at 10:49 AM 0
Share

Cool. Be sure to accept the answer if it solves your problem.

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

RayCast Boolean Help 1 Answer

How to use a Raycast to see the distance traveled within a collider. 2 Answers

Move player to specific location and avoid obstacle. 2 Answers

Im trying to create a word game with a twist 0 Answers

Raycast not detecting ANY HITS AT ALL when starting inside 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