Question about raycasting in FPS games.

Most games raycast from infront of their gun’s muzzle if I understand correctly but if you dothat the bullet wont hit the crosshair so how do I raycast from the gun muzzle but also make it aim towards the crosshair.

I am looking for : the bullet to look like it comes from the gun and not the camera but also hits the crosshair.

so how do you de that?

You could always mark the crosshairs as being in the Ignore Raycast layer.

Select the crosshairs in your scene, look at the upper right corner of the inspector window, pull down the layer menu, set it there.

That’s unnecessary since raycasts only work on colliders, and nobody’s going to be using a collider on the crosshairs (I hope…).

All you have to do is raycast in the direction that the gun is pointing.

–Eric

Raycast from the cross hairs. Muzzle flash on the gun. Forget the actual bullet. Its about faking it, not getting it right.

To actually get it right you would need to raycast from the crosshairs to a world position, then get the direction from your gun muzzle to that world position. Then raycast from your gun muzzle. This will make some very unusual effects when the target is really close.

This is a big true aspect about development that I don’t see posted often enough

But that wouldnt make it look like the bullet cvame from the gun.

A bullet moving at 1200 meters per second will travel 20 meters in one frame (@60fps). A 5.56 mm bullet at 20 units is barely a speck. No one is going to notice that it came from a foot to the left of the muzzle.
And that’s if you even bother with a ballistics simulation, instead of just a hitscan weapon