Game Development Reference
How it works...
We need to set our base aim rotator to use our point of view. We do this by overriding
the GetBaseAimRotation() function in our TutorialPawn class. It is called by our
GetAdjustedAimFor() function in our TutorialPlayerController class. This
gives our controller an opportunity to adjust the aiming of the pawn.
Afterwards, we set our base aim rotation. Here we are using our point of view rotation,
that is, the rotation from our camera and not the pawn in this situation.
Next, we create the function CheckCrosshairOnFriendly() to draw the crosshair.
We need to draw a trace first and have it check for actors in our way. If it hits an actor,
we draw the crosshair there. If not, we draw the crosshair at the end of our trace,
however long that may be.
This function also makes use of Canvas.Project() , which takes a 3D vector from
our environment and converts it to a 2D vector, and that's what allows us to actually
draw the crosshair on the HUD.
Changing the crosshair color when aiming at
Now that we have a more accurate representation of our crosshair working, why not take
it to the next step and have it change colors to signify that we are pointing at a pawn?
In this next recipe, we'll add behavior to our crosshair that allows us to do just that.
Start by having your IDE open and ready to make some changes. We won't have to create any
new classes, as we'll only have to make changes to an existing function in our TestHUD class.
How to do it...
This change requires us to add an if statement to our CheckCrosshairOnFriendly() .
Let's add it now as shown in the code snippet:
/** If our trace hits a pawn... */
if ((Pawn(HitActor) == None))
/** Draws the crosshair for no one - Grey*/