How to properly create projectiles with RayCast?

Hello,
today I had nothing to do, so I decided to try and make a gun. It was all fun and easy, and I used a BodyVelocity for my projectile. But we all know how BVs lag and overall aren’t the best way to do a ranged weapon. I also tried with TweenService: simple, easy but seriously, using it on the server isn’t good at all, and using :FireAllClients() is not the method I want to use.
I never really tried using RayCast() to create a ranged weapon, so I’ll have to ask for some help to you guys: how to create a projectile with RayCast? And also, is RayCasting actually better than BodyVelocities? And if yes, why? (Yeah, I’m really curious behind the physics now.)
I knew how to create a Ray, but not to how to make it so the projectile would use it to travel, but the old Ray.new method got deprecated so i’m even more confused now lol.

1 Like

You are asking the dev forum to create a script/tutorial, which is off-topic. However, browse through the roblox dev forum resources to find tutorials on raycasting. I also recommend reading through the roblox developer article on raycasting (this is where I first started). Also, check out this raycasting tutorial on youtube. And lastly, if you want to make a good gun, check this tutorial out. It’s pretty long, but it covers a lot of the raycasting stuff.

Trust me, raycasting is not as hard as it looks. I spent weeks researching it (there was no proper documentation at that time), but I got there eventually. It is pretty simple stuff. All you are doing is selecting a point, giving it a direction, and seeing where it hit. It is much more flexible than that, so experiment with it.

4 Likes

Oh alright. Yeah, maybe I should’ve specified I didn’t want a script, but thank you for the informations! I’ll check them out and see what I can learn with them. Thanks again bud.

2 Likes

If you want to make a projetile you need to have continous collisions, because of how physcis engine handle things using discrete collisions which isn’t suitable for fast moving objects. This is why .Touched actually doesn’t detect for fast moving objects (not because it’s unreliable it’s because of how physcis engines work). This is a problem called Tunneling.


https://gyazo.com/cd0aeafdef03b244f877b6c078789f57

Basically with discrete collisions this is what happens with high velocity.


https://gyazo.com/83e10ccb847a7bc27a9965c2a3f7d7cc

Now we have something called continuous collisions which is implemented via raycasting in roblox.


https://gyazo.com/9a83adaffcde8efd9de2da282f526248

This time there is a line drawn to the next object to see if it’s collided when the posittion is updated from the oldposition.

SO Now what’s the best way to use raycasting with projectiles and not hitscan guns. Simple raycast to the next point the projectile moves to from the oldposition, move this projectile every heartbeat to the next position via CFrame. This is also what a module called fastcast does.

This is tunneling explained:


https://gyazo.com/e7bd5afb757ae36242143253421ebcea

5 Likes

Thanks for the explanation, very useful! :smile: