# In a TPS, how to aim bullets to the direction the camera is pointing, but make the shooting from the player?

(In C#/XNA.) I mean, I can't find an answer nowhere. I'm trying to learn how to make a Third Person Shooter, especially like the one I found in a game called "M.A.R.S.", from Epic Games. I don't want to remake that game, I just want to understand how the Third Person works.

There, the screen aims to a direction, has a crosshair and its center stays right next to the player, slightly above the shoulder. And the player aims to that same direction the screen aims, pointing to that.

What I don't understand is how the bullets the player shoot go to that direction if the player is not aiming from the same position the camera is (or is it?).

For now, in my game, I can only select things that are in front of the player, having his own View Direction (a Vector3) which follows camera's WorldMatrix.Forward and applying a Ray from the player to that direction. So if I change the direction of the camera, the player will select things in front of him, following the camera direction.

And it doesn't work to be a shooter, because if I put a crosshair in the center of the screen, the player won't shoot there but to his own front.

Thanks for any help. I'm still searching, but even here I couldn't find what I want yet.

EDIT: what I found in this link is how to make the Ray cast come out from the camera. But how much should I rotate the character's view direction so its Ray goes to the same point the camera's one goes? So the Rays make something like this: /|.

EDIT 2: Now I'm testing some code. The closest way I created until now is this.

            Game.ActiveCamera.FollowPosition = Player.Position;
Game.ActiveCamera.FollowReference.Y = Player.Race.Height * 0.5f;

Vector3 target = Player.Position;
target.Y += Game.ActiveCamera.FollowReference.Y;
target += (Player.ViewDirection * Game.ActiveCamera.FarPlaneDistance);
//if (Game.Input.KeyHeld(Keys.F2))
//    Game.ActiveCamera.Target = Game.ActiveCamera.Position + Game.ActiveCamera.WorldMatrix.Forward;
//else
Game.ActiveCamera.Target = target;
//MathConverter.Convert(Player.Character.Body.Position + Player.Character.ViewDirection * 10);


I use a chasing camera that follows the FollowPosition. It has an Offset with X and Z, and I change Y to stay right above the character's shoulder.

Then the camera's target, which is the problem, I'm using the Player's Position plus its Forward (ViewDirection) times the camera's FarPlaneDistance, so the camera aims far away to the same point the character aims.

But I still don't know if the bullets are going to respect the crosshair on the screen this way.

• if this is an answer in 2D, how to I make in 3D? EDIT: it's not the answer, but helps a little to understand. Oct 3, 2014 at 19:11

Real-life gunsights have the exact same problem. There are several ways you can deal with it:

1. Do nothing. Just have the bullets fly from the gun in the direction the player is facing, and accept that they won't hit exactly where the player is looking at, but rather some distance below and maybe off to one side.

This is a reasonable choice for characters shooting from the hip, "spray and pray" style, and trusting sufficient firepower to make up for lack of accuracy. Players who want more precision with this type of targeting will just need to learn how much their gun's aim is off, and compensate manually.

2. Align the barrel with the vision. This is what you usually do when aiming a gun in real life. Basically, you hold the gun so that the barrel is straight in front of your face, pointing in the direction you're looking at. This way, any parallax difference between the line of sight and the line of fire is minimized.

Typically, there will be some kind of a sighting device mounted on top of the barrel to help you find and maintain the correct position; there are various kinds of sights, but they all have essentially the same purpose. In a video game, you can just position the gunsight directly in front of the player's camera (and the barrel just below the sight).

3. To get rid of the remaining small amount of parallax (and, more importantly in real life, to compensate for the fact that bullets don't fly perfectly straight), you can pick a specific aiming distance and tilt the barrel with respect to the sight (or vice versa) so that the line of sight and the line of fire precisely intersect at that distance.

In fact, since realistic bullet trajectories are approximately (shallow) parabolic arcs, while line of sight is straight, by slightly tilting up the barrel you can have the bullet follow the line of sight quite closely over a considerable distance. Basically, the bullet will emerge from the barrel a little below the line of sight, rise slowly to a peak just above the line of sight, and then gradually descend again. This gives you two distances where the aim is exact, and a range between (and around) them where it's only very slightly off.

4. Finally, in a game, you can cheat: cast an invisible ray forward from the camera to find the distance to the nearest target, and then automatically rotate the gun so that the bullets, fired from wherever the barrel is, will intersect the line of sight at that distance. (This is, in fact, not completely unrealistic, if the gun in question is an advanced one equipped with a modern computerized fire control system.)

5. Another way to "cheat" is to do the opposite of the above: project a ray from the gun, find out where it hits a surface, and show an aim marker there. This is basically what laser sights do, and there are also advanced systems that can do something like this "virtually", without actually projecting a visible laser beam onto the target.

Of course, while even simple laser sights will let you hold the weapon away from your line of sight and still aim accurately, they won't solve the issue with curved bullet trajectories, or with parallax between the sight and the barrel. If you wish to include such details (which, of course, are completely optional in a game) for the sake of realism, you'll still need to apply some of the techniques above to correct for the errors they introduce.

• Are you a teacher, professor or a game programming professional? Because your answer is perfect! With all these measures, I now know where to start from. Thank you so much! Oct 4, 2014 at 21:04