Question about FPS and Raycasting Javascript

I am making a weapon system for a FPS (not the whole game) that controls everything to do with weapons such as picking up, switching, animations etc. I am currently working on the firing and I am firing a circular “bullet” and then testing its collider for if and who it hits. Is this the best way to simulate weapon fire? The more I look around I see a lot of people using ray casting and wondered what the benefits/drawbacks of doing this are. Bear in mind that I am not just working on one type of weapon so I need to know could the same technique be used for weapons with a considerable amount of bullet drop (sniper rifles/grenade launchers and mortars) or is it exclusive to weapons where the precision of a realistic simulation isn’t vital (such as small arms and automatic weapons)

With grenade launchers, rocket launchers, etc. I would instantiate an object and use it as the projectile. This makes it more realistic. For weapons like assault rifles, shotguns, and (usually) sniper rifles, I would use a raycast.

If you want a real (as in, gravity, air velocity, drag, travel time, etc.) for weapons with a very fast initial fire velocity, you can still use raycasts, but create it with segments. The first raycast starts at the gun barrel, and moves straight (or whatever direction your pointing at) for speed * Time.deltaTime. Then the next frame calculate gravity, air velocity and drag, that would alter the course of the bullet for over 1 frame, and calculate the new direction and length of the ray. Then cast the ray from the end of the last raycast. Repeat.

I make it sound hard, but its really not.

Hope this helps. :slight_smile:

the important things to note are.

Raycasts are way less intensive than an actual object and there is no getting around that. a 1000 bullets firing vs 1000 raycasts, you want the raycasts.

Second
bullet travel REALLY FAST. So fast in fact that basically trying to simulate them with actual objects wouldn’t work. See each frame update a object has to be touching or be inside an object for it to realize it’s colliding and start calculations to make everything play out right. BUT if you go to fast you can actually pass through objects.
If the width of the object and your width is small enough, and your speed is high enough, you can pass through in less than frame.
The collision may never occur.

Bullets have a really small width, they travel very fast, they may not get detected hitting objects which are narrow at the point of impact. You obviously don’t want that. So you pretty much have to use raycasts because they dont fail. (there is another option by the way, you can increase the frequency of physics steps, if you check every 1/120th of a second instead of 1/60th you can catch an object going twice as fast, but then you are doing twice as many physics checks a second and that can get really intensive really fast)