I’m gonna controversially change my answer!
When you cast between two objects, A and B, to check for line of sight:
You must exclude A and B from the cast.
(Depending on your game you might have to temporarily change the layers of A and/or B during the cast.)
Also note: as whydo reminds us you need to cast in both directions to check line-of-sight, since mesh is senso unico.
One more critical note to the OP, you must absolutely limit a cast to the length between A and B or you are not checking line of sight between the two
Note that almost certainly, you could give consideration to using
#CapsuleCast
.
in dealing with the issue in your question. CapsuleCast is a “fat” ray. But note, the 6 sentences above the dotted line here, applies just the same to either capsule cast or a ray cast, no difference.
Hope it helps!!
You mentioned you wanted a general code example of using layers, there are many long and good discussions on here. Here’s some random example code, it happened to be open in the window behind!!
// so, as with all lasers, simply cast to see if we hit the hero ...
var ignoreMask:int;
var theHit : RaycastHit;
ignoreMask = 1<<13; // 13 is surely the hero's layer (but not his lair)
if ( Physics.Raycast ( pp, dir, theHit, playAreaWidthMeters, ignoreMask ) )
{
candc.heroBashedAtPoint( theHit.point, BASHTYPE.laser );
return;
}
yield WaitForSeconds(0.1);
// our lucky players lived with luck smeared in time!
if ( Physics.Raycast ( pp, dir), theHit, playAreaWidthMeters, ignoreMask ) )
{
candc.heroBashedAtPoint( theHit.point, BASHTYPE.laser );
}
hope it helps. there are many questions about the arcane details of physics layers on this site.
unityGEMS.com has outstanding articles on the topic
etc etc