Stopping raycast projectiles from phasing through parts

im making a raycast projectile and i’ve had issues with it phasing through parts, after some study ive determined this is probably what happens:


(note that raycasts starting inside a part cannot hit the part, the red circle is the projectile and the blue box is a moving part/player)

things ive tried:
“use shapecasts”: shapecasts have the same problem
“do a spatial query at the projectile position”: i may need RaycastResults.Normal for the projectile, and this method wouldn’t provide it
“move the bullet after the part”: start at slide 2 of the image, same thing happens
“start the ray at the back of the projectile”: this is a partial fix i’ve implemented, but it’s still possible to trigger the issue, it’s just a bit harder

if you need any elaboration ask in the replies because ik this is probably confusing

Hi Dante,

Could you elaborate on a few things? First, clarification on your problem. Is your problem that the projectile collides with characters and does not register a collision? Or just “parts” in general? How often does this happen? Does it only happen in specific cases?

In clarification, I would have to ask from who’s perspective is this collision happening and not being registered, and who is the authority in your game for the physics on the projectile and for detecting collisions.

I would also ask your method for determining collisions in the first place and why you’ve selected this method. I’d also like to know your definition of a “raycast projectile” and the relationship between the projectile and raycasting.

I would say chances are you are not utilizing DeltaTime correctly

this is a relatively uncommon issue as you need relatively precise timing but it happens enough to be a concern
when it happens, the projectile doesn’t at all register the part it clips through, as if it wasn’t there

the projectile is entirely handled on the server, the physical projectile model is only for visuals and the actual behavior is done via a script that handles raycasting and updating the model position

the collision method is a ray from where the projectile is now to where it’s going, selected this because i don’t trust roblox physics to have accurate hit detection, and the projectile is just a model that’s updated for the raycasting. the actual projectile is just a single ray instead of a whole bunch of them

i Am Using deltatime correctly it’s a problem with raycasts not registering parts if it starts inside them, it makes enough sense because there wouldn’t be a good way to return RaycastResults.Normal if you detected it as a hit, but i was hoping someone had this problem before and knew of a way around

You’ll need to use multiple raycasts over the travel of the projectile, you just simply can’t work around it.

worded that really badly mb, when i say a single ray i mean like one ray made up of a bunch of tiny ones, a tiny one is added every frame

You could try starting the ray further back and overlapping them, if that for some reason is not possible, you can calculate a direction vector when using a spatial query by doing something like:

local direction = (hitObj:GetClosestPointOnSurface(bullet.Position) - bullet.Position).Unit