Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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
5
Question by Varaquilex · Aug 26, 2013 at 02:06 PM · collisionraycastignorecollision

How to ignore a raycast collision with a particular object?

I have a raycast and there can be multiple objects in front of the object that casts the ray. I want to ignore some of the objects, say object A (tag). In the below situation, I want the raycast to hit to the object B (tag):

 Player raycast ---- A ---- B --->

I have seen some answers including "layers". I have no idea if they are necessary in this siutation, and I don't know how to use them or what they are, either. How am I supposed to do this?

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

4 Replies

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

Answer by robertbu · Aug 26, 2013 at 02:44 PM

You want to use layers. Here is a reference page that explains layers:

http://docs.unity3d.com/Documentation/Components/Layers.html

Alternately you can use Physics.RaycastAll(), and then sort through the unsorted list of hits.

Using layers is more efficient. RaycastAll() is more flexible since you can setup specific cirteria.

Comment
Add comment · Show 12 · 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 Varaquilex · Aug 26, 2013 at 10:27 PM 0
Share

I have played around with levels, read the links you provided along with some other answers but I'm sort of having trouble with the layer system. Can you illustrate a little in your answer by giving a tiny example for ignoring the collisions with the object with tag "B" ?

avatar image robertbu · Aug 26, 2013 at 11:55 PM 0
Share

Layers and tags are two different things. You don't use tags with layers. From this limited information, I don't know the needs of your game. Let's assume that all the objects tagged 'A' are placed on a Layer you names 'LayerA', and all the tags objects with 'B' are on their own layer name 'LayerB'. At the top of the file put:

 var layer$$anonymous$$ask : Layer$$anonymous$$ask;


Then raycast will be something like:

  if (Physics.Raycast(ray, hit, 100, layer$$anonymous$$ash) {
  
  } 

You can manipulate the layer$$anonymous$$ask variable in the inspector. Note you can set more than one layer to true, so be sure to check the value after you set/cleared any of the layers. Just whatever layers you want to be included in the Raycast() to true.

avatar image impurekind · Sep 03, 2018 at 05:59 PM 1
Share

Is there a way to do this without using layers, because my object is already assigned to another layer that I need it to be on, but I can't ignore any of the other stuff that is also in that layer when I do this raycast check?

I just want to ignore this specific object (any instances of it) and not anything else on a particular layer. And I can't put it on another layer because I'm already using it with a layer to make it invisible to the camera and a couple of other things.

avatar image Hellium impurekind · Sep 03, 2018 at 06:05 PM 1
Share

As indicated in the answer by robertbu, you cal use RaycastAll and filter the colliders returned in the array according to your criteria.

avatar image impurekind Hellium · Sep 03, 2018 at 10:20 PM 1
Share

I looked at the Unity documentation for this but the example didn't make it clear at all to me how to actually use it. It has some weird array or something, but I have no clue how you get from that all the objects the ray has hit and then ignore certain ones. And does it check all objects in a single straight line and keep going, or what? It's really not clear or intuitive how this works and how I'm supposed to use it.

Show more comments
avatar image impurekind · Jan 10, 2019 at 07:06 PM 0
Share

I can't use a different layer because as soon as I put the object on another layer it just falls through the level (presumably because it's not on the same as the floors and stuff that makes up the level).

So how do I make the raycast ignore the actual object in such a case?

avatar image
8

Answer by girishbs · Oct 19, 2013 at 08:19 AM

Just select Layer "Ignore raycast" for the particular object to not affect your raycast so the u can find the object behind it.

Comment
Add comment · Show 3 · 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 impurekind · Aug 18, 2018 at 04:50 PM 0
Share

Ignore raycast is not making my gun raycast ignore the obect, and when I try to use my own layer that I setup for the gun raycast to ignore, the object is just falling through the scenery.

Any idea why my object might be still be getting hit by the gun's raycast when I set it to layer "ignore raycast" and why selecting my own layer for it would make it fall through the floor?

Note: I don't have that layer set in the Settings to ignore anything like the scenery or whatever.

avatar image impurekind · Jan 10, 2019 at 07:08 PM 0
Share

I can't use this because I need other raycasts to hit the object. Example: I want the raycast for the gun to hit it, but I don't want the raycast for teleporting to hit it. And I can't use a different layer because as soon as I put the object on another layer it just falls through the level (presumably because it's not on the same as the floors and stuff that makes up the level). So how do I make the raycast ignore the actual object in such a case?

avatar image drewjosh · Nov 14, 2020 at 06:45 PM 0
Share

I love simple solutions to an unexpected problem. I had a BoxCollider (to register touches on an object and around a SphereCollider (to detect when a user arrives). The SphereCollider was blocking the Raycast for the BoxCollider. Selecting Layer "Ignore Raycast" worked for me. Thanks! Rewarded ;)

avatar image
4

Answer by Eli-Davis · Mar 24, 2019 at 07:12 PM

If you have a reference to the specific object, instead of creating layers or checking all hits, you can just temporarily disable the object's collider for the raycast, then re-enable it.

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 Akrone · Apr 14, 2019 at 12:55 PM 0
Share

It may seem obvious but at first i didn't think about that and I think it's the cleanest way to handle that i you want to only exclude one entity from the ray cast

avatar image
0

Answer by dylanfries · Nov 14, 2020 at 08:06 PM

From the docs: https://docs.unity3d.com/ScriptReference/Physics.RaycastAll.html

 RaycastHit[] hits;
 hits = Physics.RaycastAll(transform.position, transform.forward, 100.0F);

One thing that can be confusing about Raycasts when you first encounter them.

hits will be a variable of all things you hit, you can access the hit object itself from the hit.

 Transform targetTransform = null; // target assigned in inspector 

 RaycastHit[] hits; // returned hits
 LayerMask mask = new LayerMask(); // set in inspector
 hits = Physics.RaycastAll(transform.position, transform.forward, mask);

 for(int i =0; i < hits.Length; i++){
     if( hits[i].transform == targetTransform){
         // Look for a specific transform instance
     }

     // Or get a specific Component
     Ship theShipWeHit = hits[i].transform.GetComponent<Ship>();
     if( theShipWeHit != null){
         // Found something with a specific script, such as Health or whatever
     }

     // Or get a specific Tag
     if(hits[i].transform.gameObject.CompareTag("Tag")){
         // Found something with a given tag. 
     }

 }

The other concepts that are confusing with Raycasts are the out variable (which basically assign the response to the out var even though it is a parameter) and the QueryTriggerInteraction which simply tells the Raycast to ignore Triggers and can be set with QueryTriggerInteraction.Ignore. The default behavior is to include triggers so you only need that if you want to remove triggers from the Raycast.

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

23 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

Related Questions

making ray ignor certain collider 1 Answer

Obstacle avoidance 1 Answer

Destroy Turret with machine Gun 0 Answers

RayCast Collision between 2 game objects (of the same prefab) 1 Answer

Raycast for 2d with touch. 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