Physic SphereCast hits null
So I have the following code:
private bool Look(StateController controller)
{
RaycastHit hit;
Debug.DrawRay (controller.eyes.position, controller.eyes.forward.normalized * controller.viewDistance, Color.green);
int b = LayerMask.GetMask("Terrain");
if (Physics.SphereCast (controller.eyes.position, controller.fieldOfViewAngle, controller.eyes.forward, out hit, controller.viewDistance, b)
&& hit.collider.CompareTag ("Player"))
{
controller.mCombatant.Target = hit.transform;
controller.chaseTarget = hit.transform;
return true;
} else
{
return false;
}
}
What I want with this is basically to ignore the layer Terrain and cast a ray to see if I hit my player.
However, even if I step directly in front of this cast the hit is null
Can anyone tell me what I've done wrong?
Answer by jeango · Nov 01, 2017 at 09:31 AM
You're using the layermask incorrectly. Here your sphere cast will only collide with "Terrain" if you want to collide with all but terrain, you sould invert the mask
int b = ~LayerMask.GetMask("Terrain");
Your answer
Follow this Question
Related Questions
Doom/Wolfenstein 3D-style targeting system. Detect enemies in camera view? 0 Answers
Raycast on left side and right side of the player 1 Answer
unity 5 physics.raycast ignores mesh collider 1 Answer
RaycastHit.normal and Vector3.reflect problem 0 Answers
why this code doesnt work for jumping ?!(Raycasting) 0 Answers