Hey all! Working on a simple AI script but for some reason the raycasting that I am using is not finding my player.
Code:
if (Physics.Raycast(sightDetector.position, Vector3.right, sight, 12))
{
state = EnemyState.Attack;
}
I run a Debug.DrawLine to see if it works. The line is drawn so I am fairly certain it is running. the 12 is the Layermask and my player is on the 12 Layer. So I am not sure what I am missing. Any ideas?
The raycast expects a layermask, not an integer for which layer you want to do the cast on. Which makes sense, since we may want to do the cast on some 5 layers at the same time, not just one. So what happens now is that your 12 is taken as its binary value of 0b1100. Meaning, if iām not mistaken, you are doing your raycast on layers 2 and 3 only.
Based on the example here, where they want to cast in the player layer, you can do a 12 bitshift to only cast on layer 12: Unity - Manual: Layers
What @Yoreki says is certainly correct layer-wise.
Also, Physics.Raycast() is one of the worst most heavily-overloaded functions. I always recommend explicitly naming all your arguments to make sure you are passing what you expect, like so:
RaycastHit hitInfo;
if (Physics.Raycast(ray: theRay, hitInfo: out hitInfo, maxDistance: 4, layerMask: myMask))
{
}