Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm working on a first person game, and I've ran into a problem. In my game, there's the player's gun being rendered, as well as a reticle in the center of the screen - like this:

enter image description here

First, Let's imagine there was no gun model being rendered. Then firing the bullet is easy - you just spawn the bullet exactly where the player's "eyes" are, and then use trig to calculate the velocity based on whatever direction the player is looking.

But now, when we add the gun model... we can't just spawn the bullet where the players eyes are, we have to spawn it at the tip of the gun (right?). So you do that, but then... how do you move the bullet in a way that it will hit wherever the reticle was pointing?

Keep in mind that the bullet in my game (it's actually a rocket, in this case) will be moving slow enough that its movement can be seen, so some workaround that would usually work if the bullet is too fast to be observed will not work. For example, if you didn't spawn it so it looks like it's coming out of the tip of the gun, the player would definitely notice that.

share|improve this question
1  
The answer to your question is, Raycasting =) –  Grey Jan 19 at 2:32
    
It's all smoke and mirrors; any bullets rendered have nothing to do with the actual path or ray of the bullet. –  congusbongus Jan 19 at 2:38
    
@congusbongus I mean, OFC the bullet rendered doesn't have to be exactly lined up with the mathematics of the bullet itself, but if you don't do this to some degree it starts to look bad, and that's where I'm stuck. –  Forgive Goto Jan 19 at 2:45
add comment

2 Answers

up vote 15 down vote accepted

Shoot a ray from the camera through the center/reticle into the world. Find out where in the world it hits. Fire the bullet from the gun's muzzle at that point instead of straight out of the gun.

Bonus points for animating the hands and gun to point in that direction while aiming around so the bullet still looks like it's firing straight out of the muzzle instead of coming out at an angle.

If you do the animation you could just fire the bullet straight out of the muzzle always. Just do your best to have the gun pointing at the point the reticle is aiming at within reasonable limits. It's also helpful to have two reticles in a way that naturally both shows the center of the screen that the player is trying to aim at and the point that the gun is actually aiming at.

Or you can you show a single reticle that is where the gun will be aiming after finishing the animation and to fire the bullet at that direction rather than the place the gun is currently aiming in its animation. This way the reticle always showing what the player will actually hit but won't pretend that you can e.g. fire around a wall just because the camera sees something that the gun can't possibly do.

All of these have been used in real games. Which you prefer is going to depend on the style of game you're making, what trade-off between simulation and game you're aiming for, whether this is multiplayer or not, etc.

share|improve this answer
    
many games do not even use models for bullets but just fire a ray as mentioned to check a collision, what you can do is display a different gun model to other players than what appears in the current players view and line this up with where the bullet is fired from with the character appearing to be aiming down the sights –  Matthew Pigram Feb 5 at 5:56
    
Counter-Strike Source uses this method (it has view, player and world models) –  Matthew Pigram Feb 5 at 5:57
add comment

What about getting rid of the reticle? Then, the bullets could go in a straight line from the gun, just as in reality. Aiming down the sights would then mean more as you would align the target and the barrel of the gun (minus a few inches at the player model).

share|improve this answer
    
While plenty of games do something like this, this is a very broad design decision with implications on the entirety of the game's pacing and combat. –  Katana314 Feb 4 at 18:31
    
To me this is more of avoiding the problem then actually solving it. –  Forgive Goto Feb 5 at 15:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.