- Home /
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.
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
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?
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).
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!
If this solved your problem, click the checkmark next to the answer to mark it answered.
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?
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.
Your answer
Follow this Question
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
Raycast not hitting collider 2 Answers
Raycast not ignoring layer 0 Answers