Crosshair check

How can I check if the crosshair is on an object/enemy in code? The crosshair is created as image in the Blueprint widget

Simply use a sphere trace that starts from the player character and ends further away. You can do that by multiplying the forward vector of the player character by a big number like 5000, adding the player character’s location onto the result and then plugging the final result into the End pin of the Sphere Trace by Channel node. And of course the player character’s location should go into the Start pin without anything being added to it, unlike the multiplied forward vector. Also don’t forget to set the Radius pin to the radius of the crosshair. (Or if it’s just a dot, you can use a Line Trace as well)

Once it’s all set, you can break the hit result (either by splitting the Out Hits pin by right clicking on it and clicking on the Split Struct Pin option or by using a Break Hit Result node), check if the Hit Actor is valid or not by using an Is Valid? node (with the question mark) and if it is, check if the hit actor is an enemy or an object or whatever (by using tags or display names) using the “==” (object) node.

Hope this helps! :innocent:

1 Like

Thanks a lot. How can I take crosshair radius? :astonished:

Didn’t try it myself, apparently you can get the size of a widget by using the Get Cached Geometry and Get Local Size nodes (plus casting to the widget and then dividing to get the radius) but that wasn’t what I meant by setting the radius of our sphere trace to the radius of the crosshair. It’s probably not just a thin circle anyway, but rather something with a design that surrounds the meaningful part. So just choose a debug type from the sphere trace node to make the traces visible (persistent makes it permanent once it’s drawn, for duration removes it after the desired duration, and for one frame makes it visible for just a single frame thus is only used when it’s drawn continuously) and play with the value until you’re satisfied with it :blush:

1 Like


I also did this thing. The line did not stop at the character, it went through, so you also need to check this box for the mesh

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.