FindPartOnRay with origin inside a part, that part is ignored?

I have a problem with raycasting/FindPartOnRay. When I create a ray with an origin inside a part, FindPartOnRay won’t detect that part. Is there any way to circumvent this?

Here’s a gif illustrating the issue. The ray is being sent from the grey small part. The console prints show the return values of FindPartOnRay. As you can see it returns nil when the small part is inside the big part:

https://gyazo.com/aab813f1a1e5544d31aeb6a5c56415cb

1 Like

So first of all, you are being very abstract with what you want to happen. All you have given was that FindPartOnRay doesn’t catch parts already inside the origin, but you haven’t been clear on why this is a problem. What’s your goal? I’ve listed some predetermined answers below:

  • Do you just want to get the part/material using raycasting? Just cast a Region3 or use GetTouchingParts.

  • Do you just want a surface position/normal from the ray? Cast the ray from a different position, or in the opposite direction.

Oh, this has to do with part detection and collision. Not just rays experience this kind of behaviour. If you want to get that part, do a backcast; cast outward, then reverse the direction to be the origin.

Your use case is pretty vague though. I’m sure there must be a better way to accomplish what you’re doing right now?

Apologize for being unclear. My objective is to detect an area around a mob that has enough vertical space to fit the mob’s character inside. I basically create 8 points in a circle around the mob. I then raycast upwards continuously, adding each part hit to the ignore list, until I find a gap that is sufficiently high. At that point I raycast down again to verify that this point is good, but without the ignorelist.

The problem occurs when the initial point is inside a part, which is a common occurrence (standing next to a rock for example). If the part has another part inside of it that would count as a valid gap if the large part wasn’t there, it’ll return a point that’s inside the large part. I included a sketch to illustrate this.

Alternative solutions are welcome. Will this also happen if I try to detect parts inside a region3 if the region3 is completely inside a part? If not I think that might solve the problem if I use that to validate the points. On phone rn so will test later.

1 Like

Seems like your first point might indirectly solve my problem. I’ll test it later today.

I never heard of this function before, good to know now, but Workspace:IsRegion3Empty might be the one you need: Workspace | Documentation - Roblox Creator Hub

I also tested Workspace:FindPartsInRegion3 with the Region3 completely inside a part, it did detect the part, so I guess that is the solution.

1 Like

I believe either of those methods will solve my problem in that case - I’ll have to add another validation check, that confirms a gap was found without any parts inside, using one of those methods. Thanks for the help everyone!

1 Like