Some, but not all, raycasts going through a wall
I am working on what should be a simple part of a script to check if a tile is within line of sight. First I use OverlapSphere, then for each colldier in that Sphere I raycast to it. If the item it hits is tagged with "Tile", the tile is marked as attackable.
This is only partially working. It seems that at some angles the ray passes through the box collider that should be in the way, but other angles it works fine. this is allowing me to still select a tile that is directly behind a wall.
In my attached photo you will see the blue line extends through the object in the wall. the rightmost yellow tiles are ok, the wall does not extend far enough to block those lines.
I've now spent several hours trying to figure out why this won't work. any suggestions?
![if (los)
{
Collider[] objectsInRange = Physics.OverlapSphere(currentTile.transform.position, range);
foreach (Collider obj in objectsInRange)
{
if (obj.transform.CompareTag("Tile"))
{
RaycastHit hit;
Debug.DrawRay(transform.position, (obj.transform.position - transform.position), Color.blue, 10);
if (Physics.Raycast(transform.position, (obj.transform.position - transform.position), out hit, range))
{
if (hit.collider.transform.CompareTag("Tile"))
{
obj.GetComponentInParent<Tile>().attackable = true;
}
}
}
}
}][1]
[1]: /storage/temp/150999-capture.jpg
Your answer