- Home /
Ignore one layermask question
First time trying to use layermasks, am I doing it right? It doesn't appear to be working. I want the enemy to be able to locate the player even through other enemies but not through walls and other things on the default layer.
void Update ()
{
RaycastHit hit;
Vector3 rayDirection = player.transform.position - transform.position;
if(Physics.Raycast(transform.position, rayDirection, out hit, 1<<LayerMask.NameToLayer("Enemy")))
{
if(hit.transform.tag == "Player")
{
Debug.Log("Hit Player");
}
}
}
Answer by oStaiko · Apr 21, 2017 at 06:37 AM
int layerMask =~ LayerMask.GetMask("Enemy");
Physics.Raycast(...,LayerMask);
LayerMasks are in binary, and the =~ (~= ?) opperator inverts a binary number. You'd get the opposite, meaning hitting everything but the enemy.
Thanks for the response, but I did try that already and it stopped hitting the player even when there was nothing between the Enemy and the Player which made me try this way ins$$anonymous$$d. I'd suggest that I had something somewhere else in the script messing it up but I removed everything so it's only this update and a start to locate the player but still nothing.
Eh, I used something like this just yestersay and it worked fine for me. Just manually select all layers then by adding each string except for enemy. If that doesnt work, you know for sure its something else causing it.
Your answer
Follow this Question
Related Questions
Layer Mask Detection 2 Answers
Help with Layermasks 1 Answer
Raycast works perfectly, but only in editor 0 Answers
How do I use layermasks? 9 Answers
Layer mask doesn't work... 1 Answer