- Home /
Collider.Raycast is complaining that my ray does not have a normalized direction
I have a bit of code that looks like this. I am investigating behavior when raycasts originate inside colliders, so assume that "point" may be inside the collider.
public static bool foo (Vector3 point, Collider collider) {
Vector3 vectorToCollider = collider.ClosestPointOnBounds(point) - point;
vectorToCollider.Normalize();
Ray rayToCollider = new Ray(point, vectorToCollider);
RaycastHit dummyHit = new RaycastHit();
if (!collider.Raycast(rayToCollider, out dummyHit, float.PositiveInfinity)) {
return false;
} else {
return true;
}
}
I am running into problems because the actual collider.Raycast call is causing this method to break, because apparently vectorToCollider is not normalized. Specifically, I'm seeing:
!isNormalized(ray.GetDirection ())
I'm very confused, since I believe I am correctly normalizing vectorToCollider before passing it in as an argument to the Ray constructor. I've also tried using vectorToCollider.normalized instead, but I've experienced the same problem.
Am I making a mistake, or am I running into a weird edge case with raycasts ?
Having just struggled with some Raycasting of my own, let me ask - is your use case simplified if you can tell your Raycast to ignore colliders on layer 1,2,3 and only report a hit on layer 4,5 (where the layers are defined next to the "Tag" in the editor, these are just examples)
is your use case simplified if you can tell your Raycast to ignore colliders on layer 1,2,3 and only report a hit on layer 4,5 (where the layers are defined next to the "Tag" in the editor, these are just examples)
I am developing a medical ultrasound simulation which will require very precise collision detection on a large number of meshes. The human model I am using (http://lifesciencedb.jp/bp3d/) has several hundred discrete organs, and I would like to be able to perform raycasts on individual organs. This is because different types of tissues have different characteristic textures in ultrasound images.
Worst case, it's certainly an option to group organs of similar tissue composition into the same layer and use the same texture for all of them, if collider.raycast isn't going to work for my purposes.
Answer by aldonaletto · Sep 10, 2013 at 12:49 AM
ClosestPointOnBounds(pos) returns the closest point when pos is outside the collider. If inside the collider, it returns pos. This results in a zero length vector, which can't be normalized - that's the origin of the mysterious error. Anyway, colliders are single faced: a raycast from the inside doesn't detect the collider.
EDITED: As @hoodlma pointed out, ClosestPointOnBounds(pos) returns pos when this point is inside the collider's bounding box - it doesn't matter whether it's inside the actual collider or not.
Thanks - this is what's going on. One correction though - ClosestPointOnBounds(pos) returns pos if pos is inside the Bounding Box of the collider, not necessarily inside the collider. So, for example, checking the inside of a hollow tube will result in this behavior.
Good point! I tested this with a cube without any rotation, the only case where a collider is equal to its bounding box! Should have used any other collider shape, or at least the cube should be rotated.
Your answer
Follow this Question
Related Questions
RaycastHit.transform/collider 1 Answer
If ray in contact with collider... 1 Answer
collider.Raycast problem 2 Answers
Is there a way to detect if an object is being hit by a raycast? 2 Answers