Best way to setup tracing for hitscan weapons

Hello,

I’m currently working on a multiplayer shooter and need help with line-tracing for my hitscan weapons. My current approach to doing the tracing is to:

  • First trace from my eye-location to 10 000 units in front of my eye-location. This gives me wherever the crosshair is pointing at.
  • Then trace from the weapon’s muzzle to wherever the crosshair is pointing at.

The problem is that, this is a third person shooter, and by tracing from the camera’s location, it would hit objects that are not only in front of the weapon, but also behind the player. Please see the included picture.

So my question was if this really is the best way to trace for hitscan weapons, or if there maybe is another, better way? If there is no better way, then how could I improve what I already have? (Possibly tracing from like 500 units to 10 000 units? Instead of 0-10000 units which is what I have now?

Thanks in advance :slight_smile:

~ Stefan

Why not start your trace at a plane where the character is. You can use the Project point onto plane function. Set the plane base to be your character location and the plane normal to be your camera forward vector. That way your trace will start beside the character and should only be able to hit things in front of them.

I’m not sure if I understand… and what does the “project point onto plane”-function do?
Also, if I’m tracing from my character location in the direction of my camera, wouldn’t it be offset in relashion to the crosshair, in which the crosshair wouldn’t be completely accurate?

I think what he is saying is to create a plane in space at the location of your character. Imagine that this plane is always perpendicular to the forward direction of your camera, but it’s local origin is the origin of the character (maybe even muzzle location of the weapon). When you rotate the camera, the plane rotates also. So when you project the forward vector of the camera onto the plane, you have a reference point which ignores the distance from the camera to anything orthogonal to the character. Use that as your starting point, then trace forward to find a hitresult.

A plane consists of two values, an origin (vector) and a surface normal (normalized vector). So the idea is to cut out all of the geo between the camera and the backspace of the character.

I’ve just seen a youtube video about this and this is absolutely what I’m looking for. So as I understand it, basically what a projection of a point onto a plane does is moving the point forwards and blocking out the space behind the player? Thanks alot to HarryHighDef and ZeroParadigm! :slight_smile:

https://www.youtube.com/watch?v=r5VCChxnLnQ

I know its old topic but here is a visualised usage.