Bullets, raycasts, and obstacles

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:

in-game1
diagram1

Above, the raycast hits the hill, and the gun aims correctly. The crosshairs are lined up.

in-game2
diagram2

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.

EDITED TO INCLUDE ANOTHER OPTION

raycast from the inside of the gun barrel to the tip.

that will give you what the gun is aiming at.
to draw the cross hair its drawn on screen at the x and y co-ordinates of the raycasthit.point

Now as it regards how to change what the gun is aiming at.

Dont aim at the crosshair.

monitor the mouse input and use the change in the mouse to change the angle of attack on the gun.

have 2 pivot points on the gun. The X axis and the Y axis pivot points.
rotate on those pivot points along those axes and you’ll create a gun that works like people think it should because it works like a real gun does.

It will also have crosshairs that always point at what your aiming at.


SECOND METHOD


if you want to /insist on aiming at the crosshair you cast a ray from

(mousepos.x,mousepos.y,0)

to

(mouse.pos.x,mousepos.y,0) + camera.transform.forward

that gives you a flat plane its x and z may change but its 2 demensional because its height doesnt change.

you then hit something like maybe that back hill back there.

you dont aim yet.

you cast a ray from the gun barrel to the point you hit, like maybe that point on that back hill.

the thing is this ray cast wont hit that back hill point. it’ll hit the front hill. you use that to get the depth basically.

if the purple ray doesnt hit the same object as the red ray does.

then the modified aim point is

(purpleRayHit.point.x,RedRayHit.y,PurpleRayHit.z)

thumbs up cause i gave you a diagram!!
alt text