Mouse Targetting
Patrick Kelly
12/9/2022
While creating an tower information overlay and universal healthbar for enemies, I ran into the issue of getting the correct object the player's mouse was pointing at. The universal health bar was simple enough since it is only trying to get the target the player is looking at while in their first person shooter phase. All I had to do for that was fire a raycast from the camera location in the direction the camera is facing. However, using the camera as an origin for the tower information overlay presented an issue. When there was a larger object between the camera and the target the player's mouse was over, the overlay would display the larger tower. The issue with this method was that any object could be between the camera origin and the mouse location in the world, even though the player's camera has a clear view of their desired mouse target. I created an example below showing this scenario. Here you can see how the raycast still manages to hit the closer tower when in reality we wish to hit the smaller object.
The solution to this issue is to not use the camera as the origin of the raycast, but to intead use the player's mouse position on the screen as the origin. Unreal Engine has a provided node that can get the mouse cursor poisition and another node that can convert a screen location to a world location. Using this, I can correct the the raycast's offset angle to avoid an incorrect target. Showing another example, you can now see how the raycast is directly firing from a mouse poisition that has a clear line of sight of the smaller tower we wish to be hitting. This fixes the issue found and now the raycast can only hit anything directly under the player's mouse.
Here is the provided blueprinting for this corrected raycast.
Comments
Post a Comment