Okay, so everyone knows you need to raycast from your crosshair on-screen to see where bullets will hit, and aim your gun accordingly. If you always aim at infinity then your aim’s (almost) always off.
That’s cool, and I’m pretty sure it’d always work if your gun shot right from the centre of the screen. Say you can have guns anywhere though; then how do you handle obstacles that the raycast doesn’t see, but the bullet does?
Illustration:
Above, the raycast hits the hill, and the gun aims correctly. The crosshairs are lined up.
Uh oh, here I aimed up a little bit, which caused the gun to aim down, which is pretty counter-intuitive for the player. The crosshair is now aiming at infinity (or a farther hill, in the diagram). The gun aims correctly at the hit point, but it’s actually hitting the closer hill, and it’s actually aiming lower down than before.
Maybe I need to raycast from the gun instead of from the centre of the screen, somehow passing “through” the crosshair…? What I ultimately need is that the two crosshairs always line up, which would mean intentionally aiming “too high” in the second case.
All I can find is newbie questions about raycasting. The Unity Bootcamp demo has the same issue; it’s just way less noticeable because the gun fires from almost the centre of the screen. If anyone could impart their knowledge or point me to some information on the subject, I’d really appreciate it.
Update: I’ve considered this some more and talked to a few people and I’m pretty sure it’s not solvable. Take example 2, the gun is aimed too low to pass through the crosshair visually. Say I manually move it up to meet the crosshair height, it’ll be shooting at infinity too, and it’ll jump to being too high. There’s no magic middleground.
However, sparkzbarca’s suggestion of moving the gun instead of the crosshair should work as an alternative. The other option would be making the gun always aim at infinity. That’ll remove the negative jump in position, but of course now your aim will be off except when looking at infinity.