- Home /
Got confused about logic over an FPS game. Gun pointing, laser sight, raycasting etc.
Hello everyone, I need a little bit help in order to write the most logical fps setup. So, as I know, on an FPS game, weapon stays in some hip position as a child to the camera & other necessary hierarchy objects like weapon kick, cam kick etc. And also weapon has a aim position both are set by the user / developer. So, when the player shoots, a trajectory is set by raycast, in Unity mostly raycast is used, position as a muzzle position of the weapon, and direction as the direction of the ray created by using ViewportToRay or ScreenPointToRay. So, adjustments like accuracy are done by altering the direction Vector of the ray. Since crosshairs are set on the middle of the screen, there are no problems about hit points & crosshair's placement. So far, so good. But here comes the problem : What if I like to do something like a laser attachment to the weapon. Think about it, if the laser points to the forward of it's position ( the position is likely to be under the weapon's barrel ) when we shoot, we may not be shooting where the laser points. ( Since the weapon has no restriction like pointing to the middle of the screen, laser does not have it too. ) In this situation, a solution comes up on my mind. Lets assume the laser is done using Line Renderer. We can ofcourse set the line renderer's ending position as the middle of the screen, again using ScreenPointToRay and some TransformDirection. But in this case, laser won't be looking good since it won't be pointing to it's forward, likely to happen some visual bug in some cases like realoding or pointing the gun downwards or anywhere which is no close to the middle of the screen. ( Since the laser's start position will be under the weapon's barrel, and the end position will be the middle of the screen, the laser would look like it's angled, which actually it is. ) To prevent these unwanted visual behaviours, ofcourse we can set up some boolean, make the line renderer's end position something like 0,0,10 in local space when we are reloading, pointing the gun down or anything like playing animation or such things. But then another problem will come up, making the end position of line renderer something like 0,0, 10 and making the end position as the middle of the screen are two completely different things, thus the length of the line renderer will differ in these 2 situations. If the length of it differs, so we need to alter the Start Width & End Width paremeters each time we change it's end position, and actually altering them to look in the same thickness can be a huge pain, it won't be accurate over the code. So as you can see, I have some messed up & confused thoughts about laser sight. Same thing applies to the bullet tracers. When we hit something via raycast, ofcourse we can set the tracer particles' or tracer objects' target as a hit point, but if the weapon is looking somewhere like ground while the camera is still ( most like to be happen on the animation of somekind ) the tracers will come out of the barrel and head directly to the wrong direction which wouldn't happen in reality. To sum up, I need someone to tell me how does the game industry handle these kind of problems, what is the real logic behind an fps game in the terms of camera / weapon facing / projectile facing etc. Thanks :)
Answer by vk007 · May 26, 2015 at 03:52 AM
I encountered this problem and the easiest solution is to make the laser end at whatever a ray hits from screen center. I can cite code if you want a specific example, but that should get you started in the right direction.
Yeah I started doing it like that, but as I told, I still suspect there will be more issues on the times when the laser not supposed to look forward, gonna try to do the best if can't find any proper workout about this matter.
Your answer
Follow this Question
Related Questions
Raycast hitting below mouse position :( 0 Answers
Shooting script no working correctly 1 Answer
Shoot Projectile 0 Answers
Angle Of Ray 2 Answers