- Home /
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)
After looking at your profile, and saw your questions, I saw most of them were answered decently, but you never accepted them. $$anonymous$$ay I ask why?
To be completely honest I didnlt know I could do that. I am reasonably new to using forums.
Answer by Meater6 · Jan 21, 2013 at 03:50 PM
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. :)
Your absolutely right. You should convert it to an answer. :)
Oh, but wait, you'll lose your upvotes. Well, if you do convert it, I'll re-upvote it. Promise. ;)
Thanks, I think I will do that with the sniper rifle shots. The other weapons don't need that level of realism. Can I make a "trail" for the shot to give some sort of visual feedback to the player in the case of the sniper rifle? They may want to see where the bullet is dropping to to adjust their aim accordingly. As for the grenade launcher I have created the weapon classes so that the same function is called regardless of the weapon so I can change the code inside the grenade launchers class separately.
There is the line renderer. And there's the trial renderer. They are ideal for what you just mentioned. Then there's particle too,they can also work if that's what your looking for.
Line Renderer: http://docs.unity3d.com/Documentation/Components/class-LineRenderer.html
Trail Renderer: http://docs.unity3d.com/Documentation/Components/class-TrailRenderer.html
Answer by sparkzbarca · Jan 21, 2013 at 05:43 PM
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)
Your answer
Follow this Question
Related Questions
Rotate RayCast/Object right on instantiation? 1 Answer
Raycast help..... 2 Answers
Turret that shots a raycast to detect my character then shots at him. 1 Answer
ray cast not working ? 2 Answers
Trouble with Projectiles, Raycasting, and 'Gravity' 0 Answers