Getting guns to shoot where I aim at

I don’t quite understand that…

If you take a look at this little sketch:

How do I get the rotation of my gun right so it actually aims at the target.

I’m working on a 2D top down shooter prototype, and I’m wondering how this is usually solved?

Do you just spawn the projectile directly in front of the character and take the forward vector?
Do you rotate the gun (or the whole character) so the gun actually points at the target (which would cause constant rotation whenever you aim at different distances)?
Or would you just spawn the projectile at the guns muzzle, leaving the guns actual rotation, and just use some LookAt rotation to a line trace HitResult (which might end up looking weird, cause the gun doesn’t really fire where it’s pointing)?

For the 2D project it might not be that big of a problem, but how would that be for first person shooters or AI, especially?
I also worked on a 3D tower defense with gunmen instead of towers. I kind of ran into the same problem… my units looked at the enemy but missed each shot because their gun wasn’t pointing at their target.

Is that what AimOffset is there for or is that just for animation?

Thanks in advance! :wink:

AimOffset is just for animations. Most, if not all first person shooters shoot the bullet out of the center of the camera, and just try to line the gun up as close as possible.

As for your original question, either option would work. It just really depends on how you want your game to function. You’re absolutely right that having the gun off center would mean that it is wildly rotating when switching targets at different distances.

Hiya,
Give this a whirl…

Unreal Engine 4 - (Tutorial) Building Basic Weapon Mechanics 2 ?v=eFps5e7NSP0
If you like what you learn in this video be sure and give my patron page a look

Gotta take a look at it today, thanks for your help

The way I do it:

  • shoot a raycast between gun muzzle and camera forward * range
  • I spawn the projectile at the gun muzzle location looking forward the impact point of the previous raycast (or its end if it did not hit)