Physics.Raycast returns odd results when collider is not visible to camera
Edit: I also discovered that the results are the same with Physics.linecast. I fear unless there is a way to make the raycast work properly when the collision point is not visible to the main camera then raycastAll is the only option I have.
Edit 2: I also just found out that sendMessage has no receiver when using a simplified version of the code. I imagine if I tried to sendMessage(takeDamage) at any point on the objects which are not visible, then the same error would be thrown. In other words, the raycastHit.collider.gameObject is not a receiver? I have no clue.
It seems that raycasts return weird results whenever the object the ray is colliding with is not visible to the main camera.
for (int i = 0; i < collisionPoints.Length; i++)
{
Ray explosionRay = new Ray(transform.position, collisionPoints[i].transform.position - transform.position);
float maxDistance = Vector3.Distance(collisionPoints[i].gameObject.transform.position, transform.position);
//RaycastHit[] explosionHits = Physics.RaycastAll(explosionRay, 1.5f);
bool targetReached = false;
while (!targetReached && Physics.Raycast(explosionRay, out RaycastHit explosionHit, 1.5f))
{
print(explosionHit.distance);
if (explosionHit.collider.gameObject == collisionPoints[i].gameObject)
{
explosionHit.collider.gameObject.SendMessage("takeDamage", 225);
targetReached = true;
}
else
{
if (explosionHit.collider.gameObject.tag != "Enemy")
{
targetReached = true;
print("Hit a " + explosionHit.collider.gameObject.tag.ToString());
}
else
{
//raycast again. distance decreases by previous raycast's collision point distance from origin.
targetReached = true;
}
}
}
}
Physics.RaycastAll seems too finnicky, returning odd results such as the same hit twice in an array, as well as requiring a sorting algorithm to properly utilize. With physics.raycast in a loop, I'm not using an array and I know the hits will come back in order. Unfortunately, based off extensive testing, the above code's raycasts actually don't hit anything(?) when the object's supposed collision point is not visible.
In this image, the projectile lands centered in the middle of all five objects, and since the faces with which the raycast are colliding with are visible to the camera, the code works fine.
In this image, the projectile lands in the same spot, though the main camera can not see the faces of two of the cubes, and as a result, I get the weird "hit a untagged" message in console. The cubes are all tagged as "Enemy" and their layer is "damageable", so I don't know why this would be occuring.
I've been stuck trying to make these explosions work for an eternity; How can I make it so the raycasts work properly even when the objects are not visible to the camera?
Didn't read the code, but off-camera raycasts work fine. Raycasts don't care about any camera stuff, only colliders. And camera rendering tricks won't change those.
Something else is probably funny. For example, another script is moving or de-activating stuff that goes off-camera. Or maybe you have some collider that tracks the camera, which is blocking raycasts from outside to inside.
Try printing more - like the name and position of what you hit. Or, just to see, pull/widen the camera so nothing is off-camera.
Hmmm. Looks like the grenade itself, and all its individual parts, are being hit by the raycastAll.