- Home /
Other
How to create User layer that acts like the "Ignore Raycast" layer?
Im trying to create another layer that acts exactly like the built-in layer "IgnoreRaycast". Ive been trying to figure out how to correctly do this but still not sure how. I have two layers Ive created that I would like to act as Ignore Raycast layers. The layers I would like to ignore are layers 20 & 21. The layer "unitmask" is picked inside the inspector when looking at the character.
Below: OnMouseOver works correctly but when leaving the character's "unitmask" the OnMouseExit doesn't work because it is hitting the layers I want to ignore. How can I Ignore layers 20 & 21?
public LayerMask unitmask;
public int IgnorelayerF = ~(1<< 20);
public int IgnorelayerE = ~(1<< 21);
public void OnMouseOver(){
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit, Mathf.Infinity, unitmask)) {
print ("Hit unit mask");
}
}
public void OnMouseExit(){
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit, Mathf.Infinity, IgnorelayerE) || Physics.Raycast (ray, out hit, Mathf.Infinity, IgnorelayerF)) {
print ("Not hitting unitmask");
}
}
Set a Layer$$anonymous$$ask to exclude the layer and use with you Raycast.
Use Physics.RaycastAll to get all colliders a ray hits (depending on the Layer$$anonymous$$ask), and the nloop through them and ignore a collider that has the layer you don't want to hit.
Answer by kumar3087 · Jun 24, 2018 at 02:20 AM
You can do it in another way like this, make a variable of type LayerMask, it will be same as camera's culling mask var, now deselect all the layers which are not required to hit, and pass this variable in your raycast.
I wish it was like that but its not I can only pick one layermask and thats the one the character can get assigned to @kumar3087
please check this link, I've selected more than one layer, and only these selected layers will be hit, other will be ignored. https://www.dropbox.com/s/needfforqsh4jy0/Screenshot%202018-06-25%2001.37.54.png?dl=0
Follow this Question
Related Questions
LayerMask for RayCast 1 Answer
Layermask (raycast) wont work... 4 Answers
Detecting that I'm clicking a unit even though I'm not? 0 Answers
Can't Ignore a Layer when raycat 0 Answers
Ignoring an object (using raycast with layers) is not working 1 Answer