• 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 Jerkenhag · Mar 14, 2013 at 05:26 PM · raycastcolliderparticlesignore

Make raycast ignore some objects.

I want my raycast to ignore the particles made from the character and then stop at the next collider. I have added the particles to a layer named "particles" and the raycast is made to see if the player is in front of the character although now the character can see through the walls, which is not good. How can I make the raycast ignore the particles and then make the next collider it hits be applied to a variable that I can use in a if()-statement to see if its the player? I have looked in the docs.unity without success, any answer or reference much appreciated.

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

2 Replies

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

Answer by robertbu · Mar 14, 2013 at 06:28 PM

Typically this is done by placing object on different layers (as you have done with "particles"). Then you use a layer mask in the Raycast() call. A layer mask is a set of bits, with one bit per layer, so you can set the mask so that the Raycast() can see only the layers you want it to see. That is the mask allows the Raycast() to see multiple layers while at the same time excluding some layers.

This post here will give you info on constructing layer masks:

http://answers.unity3d.com/questions/8715/how-do-i-use-layermasks.html

Comment
Add comment · Show 5 · 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 Jerkenhag · Mar 14, 2013 at 07:04 PM 0
Share

I have read the whole thing there before and did it once again and I came to this code:

 var goal : Transform;
 var player : Transform;
 var source : Transform;
 
 var hit : RaycastHit;
 
 var layerPlayer : int = 8;
 var layerWalls : int = 9;
 var layerParticles : int = 10;
 
 var layermask1 : int = 1 << layerPlayer;
 var layermask2 : int = 1 << layerParticles;
 var layermask3 : int = 1 << layerWalls;
 
 var finalmask : int = layermask1 | layermask2 | layermask3;
 
 var chasingPlayer = false;
 
 function Update () {
 
     var ray = new Ray(source.transform.position, transform.TransformDirection(transform.forward));
     
     if(chasingPlayer == true){
     
         GetComponent(Nav$$anonymous$$eshAgent).destination = player.position;
         
         GetComponent(Nav$$anonymous$$eshAgent).speed = 7.5;
         
         Debug.Log("Chasing Player");
     
     }
     
     else if(Physics.Raycast(source.transform.position, transform.forward, hit, 300.0, finalmask)){
         
         if(hit.transform.tag == "Player"){
         
             GetComponent(Nav$$anonymous$$eshAgent).destination = player.position;
         
             chasingPlayer = true;
             
             Debug.Log("Found Player");
             
         }
         
         else{
         
             GetComponent(Nav$$anonymous$$eshAgent).destination = goal.position;
             
             Debug.Log("1");
         
         }
     
     }
     
     else{
     
         GetComponent(Nav$$anonymous$$eshAgent).destination = goal.position;
         
         Debug.Log("2");
     
     }
 
 }

But now it wont see the player. It doesn't see the player through walls though but now it doesn't see the player at all. Where am I wrong?

avatar image robertbu · Mar 14, 2013 at 07:17 PM 0
Share

I don't spot anything wrong here. Does you player have a collider? Players can sometimes be composed of multiple game objects, is the game object with the collider set to the correct layer?

Note as a simpler solution to setting up your bitmask take a look at Eric5h5's solution using the Layer$$anonymous$$ask class (last entry on the page at the link I gave you).

avatar image Jerkenhag · Mar 14, 2013 at 07:28 PM 0
Share

Sorry, I put the wrong component as "source" so the ray went over the player and couldn't hit it. It seems to work now, thank you for the help!

avatar image robertbu · Mar 14, 2013 at 08:06 PM 0
Share

If this solved your problem, click the checkmark next to the answer to mark it answered.

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
6

Answer by girishbs · Oct 19, 2013 at 08:14 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 · 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

12 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

Related Questions

Using different paricle emitters depending on the tag of the object raycast hits? 1 Answer

Raycasts ignoring colliders based on Collider Name 2 Answers

Raycast with doubleCollider 3 Answers

function OnMouseOver() ignore selected raycast 1 Answer

Internal collisions 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges