Creating the Game Player Controller part 1
The PlayerController class (CustomPlayerController.uc) part 1
With our project descriptor created and incarnated in the form of the game info class we carry on to create the player controller. Again, the player controller is the place where you will want to handle player input in the form of game controller, keyboard keys and mouse input. The player controller can have multiple state depending on the current gameplay in work.
Now if you need more information on how the game player controller you can visit the udk documentation website and check their tutorial for a first game here.
For making the pawn shoot in the direction of the mouse pointer we will need a couple of variables that are again akin to the iso tutorial for clicking and moving. This tutorial will let you move using the standard WASD keys like in the UT game.
Create a new file and name it CustomPlayerController.uc. Change the extend Object to extend UTPlayerController. This will make us use all the functionality of the UT player controller given by Epic’s UDK.
Add the following declarations for the variable on top of the file :
//Hold calculated mouse position (this is calculated in HUD) var Vector2D PlayerMouse; //Hold where the ray casted from the mouse in 3d coordinate intersect with world geometry. We will use this information for our movement target when not in pathfinding. var Vector MouseHitWorldLocation; //Hold the normalized vector of world location to get direction to MouseHitWorldLocation (calculated in HUD, not used) var Vector MouseHitWorldNormal; //Hold deprojected mouse location in 3d world coordinates. (calculated in HUD, not used) var Vector MousePosWorldLocation; //Hold deprojected mouse location normal. (calculated in HUD, used for camera ray from above) var Vector MousePosWorldNormal; /***************************************************************** * Calculated in Hud after mouse deprojection, uses MousePosWorldNormal as direction vector * This is what calculated MouseHitWorldLocation and MouseHitWorldNormal. * * See Hud.PostRender, Mouse deprojection needs Canvas variable. * * **/ //Hold calculated start of ray from camera var vector StartTrace; //Hold calculated end of ray from camera to ground var Vector EndTrace; //Hold the direction for the ray query. var vector RayDir; //Hold location of pawn eye for rays that query if an obstacle exist to destination to pathfind. var Vector PawnEyeLocation; //If an actor is found under mouse cursor when mouse moves, its going to end up here var Actor TraceActor;
PlayerMouse
Each frame the mouse position will be aquired in the MyHud::PostRender function. It is not much relevant to keep the variable inside the controller per se, but since all variables for calculating mouse are located here, we might as well group them, since the controller is pretty much available to all classes in the game, it might be usefull one day. You can note that the rendering of the 2d texture for the cursor will take place in the HUD.
MouseHitWorldLocation
This variable holds the vector of a calculated Raytrace (Trace function) in the MyHud::PostRender function. Think of it has the point in 3d where the mouse hit a geometry object. This variable represent the point where the mouse exist in 3d coordinate and will be extensively used to make the pawn move.
MouseHitWorldNormal
This variable is a pre-calculated normalized vector of where the mouse hit the geometry. This is used for advanced effects and will not be of much use in this tutorial, but we keep it for advanced math related to mouse position in 3d world space. Again this is calculated from the MyHud::PostRender function.
MousePosWorldLocation
This variable will hold the deprojected 2d mouse coordinates. Basically from the transposition of a pixel in 2d space to 3d coordinates. We will we not use this variable much, we are more interested in the direction vector than the actual location. You should always use MouseHitWorldLocation in preference to this variable. We keep it for futur references also.
MousePosWorldNormal
This is the second most important variable in the mouse calculations. It calculates the direction in which the deprojection goes. This might be advanced if your not very good with vector math. You should know that we will use this direction to cast a ray from the camera to have precise mouse 3d coordinates.
StartTrace, EndTrace and RayDir
These variables will hold the result of the ray cast from the camera to know where the mouse intersects with 3d geometry. They are kept in the controller for futur references.
PawnEyeLocation
This variable is used to calculate a ray in direction of the pawns eye to see if movement in straight line to mouse click would cause a collision. If no obstacles are detected with tha FastTrace function, then the pawn can simply move without using Path-finding.
TraceActor
This variable will keep a reference to what would be hit under the mouse ray at each frame. So when you move the mouse over an actor or geometry, you could show a special effect in the hud at the position of the actor. We will leave advanced functions out of this tutorial for this variable, but I am sure you will find plenty of stuff to do like pop-ups and game play information.
Note that for the cursor, everything happens in the next section, inside the HUD.
Creating the Game Player Controller part 2