(This all may seem obvious to anyone having worked on this before but this is my first time encountering it and it took me a little bit to work through the issues.)
The premise is pretty straightforward: With a third person shooter, how can I ensure that projectiles coming from the character will have the expected trajectory when the actual aiming is done with a crosshair?
I have a crosshair drawn in the middle of my HUD, in the center of the screen. It indicates the center of the camera for the character. My camera is above and behind my character (to the right) so that I can see the character clearly.
https://i.imgur.com/6eeo04Y.png (Crude MSPaint representations drawn from above to better show the issue Iâm facing)
When I want to fire a projectile, I do a Line Trace by Channel from the cameraâs location (center of the crosshair) to a point 5000 units away (using the cameraâs âforwardâ vector). This gives me the location of either a colliding object or the location of the end of the trace.
I use the above location and the characterâs world location in a Find Look At Rotation to find the correct rotation of the projectile and spawn it at the characterâs location.
The end result is that projectiles fired from the character will hit the point where your crosshair is pointing. This is good and works well as long as you are accurate with your crosshair.
So let me describe a scenario. Youâre shooting at an enemy, headshot headshot headshot because youâre a pro. But then your cat bumps your arm and your crosshair is slightly off - just to the left of the playerâs head. But instead of the projectile whizzing by their ear, it ends up being several feet to the left. The reason for this is that the initial line trace went out 5000 units, way beyond the target and hit open air. The projectile is then launched towards that far-away point and the trajectory makes it miss the player by a LONG shot rather than barely missing.
In a similar scenario: Youâre shooting at an enemy but youâre terrible because you just found out your catâs on fire and itâs bothering you. Youâre firing over and over at a stationary enemy but you canât aim right and your crosshair is a good deal to the right of the player. Despite this, you are landing shots on the player every time. Since the line trace from the camera went beyond the target, the impact location ended up behind the target so the projectile is launched toward it and manages to hit the target anyway.
Effectively, what is seen here is the same as if you were to hold a gun in your right hand, hold it way out to the side and fire it where youâre looking. Donât think about the logistics of trying to comfortably hold a gun that way - thatâs not the point.
The takeaway is this: The greater the distance between the camera location and the firing location, the greater the error will be when firing. I have compensated for this a bit by adjusting the projectileâs rotation but this only works if the crosshair is pointing exactly at where you want to hit. Otherwise, it becomes a mess. As far as I know, there is no way to fix this problem keeping the current setup. There is no way to simply know what it is you meant to be aiming at and this implementation will always feel clunky. If you disagree, I would be happy to learn a new way to deal with this.
So - what else can be done? Thatâs what Iâve come here to ask. So far, my only idea is to reduce the distance between the camera and the player.
I have seen games where, when you are aiming, the camera is effectively âzoomed inâ to be above the gun, either so youâre looking down the sights like you would in a first person shooter or close enough that it doesnât make a difference - possibly all youâd see is a part of the characterâs head/shoulder and the gun. However this method doesnât look good for all styles of games and, depending on the gameplay, constantly zooming in and out will get jarring.
It is also possible to just try to keep the character and camera closer by default. At the moment, my projectiles are firing from the center of the character which looks weird and isnât permanent but itâs a basic implementation. If I add a firing animation where the character holds up their weapon to their right shoulder, the distance between the camera and firing location is reduced a good amount. Likewise, I can probably squeeze the camera in a little more without TOO much negative impact. But I donât want the camera to be so close that you canât see your character anymore and I donât want most of your screen to just show the back side of your character. So getting this balance will be tricky. Overall, this will reduce the error but will not fix it consistently. It will always make sense to aim to the right side of things because, if you miss with your aiming, your projectile might still hit.
Another solution would be to have a targeting system so youâre not relying on being accurate with the crosshair but this reduces some of the âskillâ needed to be accurate.
Iâm certain others have encountered and dealt with this scenario. I am trying to find other third person shooter games where I can test how it works there as well. Does anyone have any good examples? Suggestions? Thoughts? It kinda feels like most of the questions/discussions I post here go unanswered so Iâm not truly expecting a response ⌠but Iâm still hopeful.
Thanks for reading
Edit: I did some searching (through Google instead of directly in the forums) and found a lot of people referencing âaim offsetâ. I found some tutorials on it and will go through tonight to see if that helps ⌠sounds like what I need so Iâm hopeful.
Edit2: Oh, this isnât actually addressing what Iâm talking about, this is just making the character face the correct direction which I will also need but it doesnât resolve the issue Iâm seeing above. On this page, the animation process is described: Creating an Aim Offset | Unreal Engine Documentation but ultimately this works because the camera is already behind the gun.