# smart cross hair

Hello everyone I am working on a 3rd person shooter and I was just making my cross hair and for some reason when I shoot the gun the bullets don’t go where the cross hair is. Is there a script I need to write to make the bullet go to the cross hair?

Well I assume you have some code already set up to shoot the bullets currently. If you are shooting them in the direction transform.forward of the character then they will always go where the character’s forward axis is pointing.

Probably the best way to do you what you want to do is find the point where the cross-hair is pointing using

``````Vector2 crosshairPosition = Input.mousePosition //or wherever else your crosshair is, in Screen space.
Vector3 bulletTrajectory;
float maxDistance = 10000; //if we aren't going to hit anything, just shoot it in the direction of our ray
RaycastHit hit;
Ray ray =Camera.ScreenPointToRay(crosshairPosition);

if (Physics.Raycast(ray, out hit, maxDistance)) {
bulletTrajectory = hit.point - gunObject.transform.point;
} else {
bulletTrajectory = (ray.normalized * maxDistance) - gunObject.transform.point;
}
``````

I would use a ScreenPointToRay from the center of the screen (or whatever the cross hair position is) and use it in a Physics.Raycast. Then use it in the direction of your fire. This way a gun can shoot a little off and still be considered shooting straight.

Actually, scratch the Raycast, and use RaycastAll to the max range of the gun. That way, you can use a Vector3.Dot to gauge if the object you are thinking of shooting at is less than like .9 or so in front of the person. If it is, then don’t shoot at it. Then you pick the first target that is there. Do a raycast from the gun to it and find out if that is the first target the enemy can see. If it is, then you can hit it, other than that, you hit something else.

Edit: Very similar to Antitheory’s post, but using two rays, the first is a RaycastAll.

LOL, I was writing the exact same post as you until I thought about the camera position not being the player position. Then I stopped and was wondering how to get that information transposed onto the player without something affecting the camera’s view.

A cool feature would be to have the player’s aim in that direction be independent from where the player is walking. That would involve a bit of work with the player’s gun and torso. Totally doable but a little more complex.

In Dawn of War, you had overlapping animations. Two of which were the horizontal and vertical aiming animations. These controlled the aiming and extents of aiming. Then they simply overlay-ed the animations on top of the character. So if he was standing, he could aim and not have to rotate the character to do it. I am actually interested to find out if Unity can do that type of animation stacking. If so I am not far off from creating some truly phenomenal animations. LOL

Do you mean having the gun always pointing at where the shot will hit according to your above double RayCast method?

You could possibly do this without animations with a rudimentary IK system like the one used in the HeadLookController. You just need to constantly update the target-position with wherever the bullet would hit according to your ray-casts. It would mean that you would need to be calculating the hit-point on every-frame, but a couple of raycasts are not very expensive.

Oh yes. Like the second part of what you posted said. IK it over and make it “look” like its pointing in that direction. The cool thing is if you used a crosshair on a mouse, you could always point to the location of the mouse where the person could see and have it still be kind of fps, kind of over the shoulder or full third person.

Go a bit further, and you could create a 3d person who is IK’d to point at the mouse position he can best see by using a Plane.Raycast, Physics.RaycastAll and a Physics.Raycast to get an average aim location then a Physics.OverlapSphere in that area would then give you everything he could aim at, and then pick the best one.

Then you can control his legs apart from his torso, then up and down and he can become the best little AI soldier you could dream of… lol

Thanks guys. I got it to work! :]

I tried something like this for a mouselook system in a 3rd person game I am making right now. It didn’t really look all that good unfortunately and I went with something different but similar, where the mouselook target was always the same distance away from the characters head regardless of whether anything was actually there or not.

The problem with your suggested method is that when the camera moves so do the crosshairs… and thus the spot where the crosshairs were pointing changes (and so on…)

I guess it would be in whatever type of game you were making. To be honest it is so open on how to do things. Even the smallest differences can make huge differences in the gameplay.

I guess I overthink things sometimes.

Heh no worries. I nearly drove myself crazy trying to get a good crosshair system working for my 3rd person game. I ended up allowing the player to move the crosshairs position on the screen while they are moving, but if you stop then moving the mouse brings the crosshairs back to the center and instead moves the position of the camera. It sounds complicated (and it is, the cam script is 250 lines long!), but it ends up being pretty intuitive to control.

I was really surprised by how much problem solving was required to get a decent 3rd person camera working… now I know why people make first-person shooters!