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:
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.
Answer
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.
No comments:
Post a Comment