I am making a ball projectile move with linear velocity and from research, I’ve seen many use .Touched and heard that it isn’t accurate, and also seen many say getpartsinpart but for strictly hitboxes and idk if they work for projectiles. I’m not sure which to use here, so my question is:
What are my options here for the best hit detection for a ball like projectile?
Sorry if this question has been asked before, I was just confused on what to use. Thanks.
Neither approach is ideal for projectiles. They both rely on direct collisions detected during a physics step. Physics steps are not guaranteed to occur a fixed interval, and the displacement of a projectile by its velocity can be high enough of a degree where it can be on a collision course with an obstacle, but end up on the other side of it on the next physics step. This is why raycasting is best
If its a slow projectile, you can rely on Roblox Collision to detect it easily, so you can rely on .Touched for those specific occasions. Queries should only be made if it impacts something like a player or the environment.
For fast moving objects, use raycasts. This technique is often used for guns to simulate bullets. Each by simply casting a ray from Point A to B, or by calculating the trajectory of a object.
Because they are moving fast, .Touched will be inconsistent.
I’m gonna go out on the assumption that a “ball like projectile” is not moving as fast as a bullet and needs visual delay, so I wouldn’t rely on raycasting in that scenario unless you want to make it a lot more complex
I hear the eco chamber of “.Touched isn’t accurate” thrown around so much and from my experience .Touched is fine, as in if something that should fire .Touched will fire as intended, now that does not mean its always the best way of doing something, raycasting often times will be better, however this is because it either offers more functionality or is simpler to set up for your specific needs and not because .Touched is “unreliable”
If its a physical object that follows some physics you set up for it to follow .Touched would work best, as raycasting at an arch or something would been more difficult, if its more of a projectile like a bullet or something then raycasting would be better
I would either use Raycasting or GetPartBoundsInBox. The reason why I wouldn’t personally use .Touched is because it sometimes fails to detect touch when the projectile is travelling at a high speed.
I don’t really use touched in general, but from what I heard, it’s not used physics aren’t too accurate (they run on a specific interval and if the server/client lags for whatever reason, it might not work too well).
The two other common methods I’ve heard were:
Raycasting
For slow/noninstant projectiles: Cast a ray starting from the part’s last position to its current position.
For instant projectiles: a single raycast in the direction of the projectile.
Pros:
VERY fast
Cons
only small projectiles (like guns), but if you need to you can just use multiple raycasts to simulate a 3d hitbox.
Shapecast (sphere/blockcast) Introducing Shapecasts
This is a raycast, but instead a 3d object. I personally don’t use these, because I prefer using the next the next method.
GetPartsInBounds (or if you want GetPartsInPart)
This lets you find whatever is inside a 3d region.
Depends on what kind of projectile, if you want the projectile to be destroyed after it hits the first object you can use:
Projectile.Touched:Once(function(base_part)
– Code
end)
Or if your projectile is something like a ray of beam that is suppose to hit multiple targets you can use :Connect instead of :Once, :Once basically lets the function/event fire once, but disconnects right after.
There are existing modules that are quite easy to implement that use raycasts and allow for projectiles you can see. FastCast is probably the best open source one.
It helps to deal with possible issues with .Touched events (which definitely do happen, even with projectiles that aren’t super fast). However, if I recall correctly, it uses raycasts and not shapecast/spherecast so if @LeooAi wanted to make something more accurate, they would probably need to modify the module a bit.
Also, while FastCast is simple and reliable, compared to .Touched events it is more difficult to set up. If reliable touched events don’t need to happen 100% of the time, FastCast is probably not worth the trouble.