Creating the PlayerController class

Our game shell is done, now we need a way to control our character once we actually get it on screen. To simplify things, the character you control on screen is generally known as the Pawn.  From the Unreal Wiki:

“Pawn, the base class of all actors that can be controlled by players or AI.

Pawns are the physical representations of players and creatures in a level. Pawns have a mesh, collision, and physics. Pawns can take damage, make sounds, and hold weapons and other inventory. In short, they are responsible for all physical interaction between the player or AI and the world”

This new class will be called “SideScrollingGamePlayerController.uc”. Quite simply, the PlayerController class is what allows us to control and manipulate our pawn.  Take this code, and insert it into a blank text file which we will call SideScrollingGamePlayerController.UC  Don’t be scared – it looks daunting at first, but it is pretty well annotated, and once you get the hang of it you will be able to make changes to suit your needs as well, therefore I won’t cover it again here.

[code]
class SideScrollingGamePlayerController extends GamePlayerController;

simulated event PostBeginPlay()
{
super.PostBeginPlay();
`Log("I am alive !");
}

DefaultProperties
{
CameraClass=class’SideScrollingCamera’
}

// For some reason or other, the standard StartFire() is not functioning.
exec function StartFire(optional byte FireModeNum)
{
if(Pawn != None) Pawn.StartFire(FireModeNum);
}

// This is identical to the original UpdateRotation(), except that it makes sure we use the PAWN’s rotation, not the controller’s rotation.
function UpdateRotation( float DeltaTime )
{
local Rotator DeltaRot, newRotation, ViewRotation;
if(Pawn == None) return;

ViewRotation = Pawn.Rotation;
if (Pawn!=none)
{
Pawn.SetDesiredRotation(ViewRotation);
}

// Calculate Delta to be applied on ViewRotation
if (PlayerInput.aStrafe<0)
Viewrotation.Yaw=-16384;
if (PlayerInput.aStrafe>0)
Viewrotation.Yaw=16384;

// THIS ALLOWS OUR PAWN TO LOOK LEFT AND RIGHT, INSTEAD OF BACKPEDDLING.//
if (PlayerInput.aForward<0)
Viewrotation.Yaw=-32768;
if (PlayerInput.aForward>0)
Viewrotation.Yaw=0;

ProcessViewRotation( DeltaTime, ViewRotation, DeltaRot );
//SetRotation(ViewRotation);

ViewShake( deltaTime );

NewRotation = ViewRotation;
NewRotation.Roll = Rotation.Roll;

if ( Pawn != None )
Pawn.FaceRotation(NewRotation, deltatime);
}

// This makes our weapons aim to the pawn’s rotation, not the controller’s rotation

function Rotator GetAdjustedAimFor(Weapon W, vector StartFireLoc) {
if(Pawn != None) return Pawn.Rotation;
else return Rotation;
}

// This is overridden to ensure that we use the CONTROLLER’s rotation for references to movement – this makes our "move forward" always use the same forward, no matter what rotation our pawn is facing. It is otherwise identical to the original

state PlayerWalking {
function PlayerMove( float DeltaTime )
{
local vector X,Y,Z, NewAccel;
local eDoubleClickDir DoubleClickMove;
local rotator OldRotation;
local bool bSaveJump;

if( Pawn == None )
{
GotoState(‘Dead’);
}
else
{
GetAxes(Rotation,X,Y,Z);

// Update acceleration.
X.X=1;
Y.Y=1;
NewAccel = PlayerInput.aForward*X + PlayerInput.aStrafe*Y;
NewAccel = Pawn.AccelRate * Normal(NewAccel);

DoubleClickMove = PlayerInput.CheckForDoubleClickMove( DeltaTime/WorldInfo.TimeDilation );

// Update rotation.
OldRotation = Rotation;
UpdateRotation( DeltaTime );
bDoubleJump = false;

if( bPressedJump && Pawn.CannotJumpNow() )
{
bSaveJump = true;
bPressedJump = false;
}
else
{
bSaveJump = false;
}

if( Role < ROLE_Authority ) // then save this move and replicate it
{
ReplicateMove(DeltaTime, NewAccel, DoubleClickMove, OldRotation – Rotation);
}
else
{
ProcessMove(DeltaTime, NewAccel, DoubleClickMove, OldRotation – Rotation);
}
bPressedJump = bSaveJump;
}
}
}
[/code]

Most important for our purposes is this:

[code]

// THIS ALLOWS OUR PAWN TO LOOK LEFT AND RIGHT, INSTEAD OF BACKPEDDLING.//
if (PlayerInput.aForward<0)
Viewrotation.Yaw=-32768;
if (PlayerInput.aForward>0)
Viewrotation.Yaw=0;[/code]

This is what makes Mega Man run from left —> right while facing the right side of the screen, then when running from right —> left he will be facing the left side. By default, Unreal doesn’t support this. Furthermore, and most importantly, the code above prevents players from using the mouse as an input device.

5 thoughts on “Creating the PlayerController class

  1. This is an awesome tutorial, I have been following it for a while, I have managed to get the camera to line up with the Editor. (Just so its easier to create maps), But I cannot get the player to run straight. I changed the following lines:

    X.X=-1;
    Y.Y=-180;
    NewAccel = PlayerInput.aForward*X + PlayerInput.aStrafe*Y;
    NewAccel = Pawn.AccelRate * Normal(NewAccel);

    This was in attempt to get him to run more straight, alas after a while he strays off the line marker I have placed do you have anythoughts?

    • Sorry that it took so long for me to get back to you, I’ve been away for a bit.
      Thanks, I’m glad you found it useful!

      As far as as your problem, I’m not quite sure of what it could be. When you say, “run more straight”, what do you mean in particular? The way it was before, he would run at a slight angle? If that was the case, then you may not have laid the level out correctly. For example, in my level i started by laying everything out on the flat on the Y-axis, so that when I hit “W” (up) on the keyboard, my character runs straight. If you didn’t do this, then that may be the situation. The “NewAccel” value shouldn’t affect anything, but it may have to do with the change in X.X or Y.Y. Your best bet may be to bring it to the forums :/

  2. Hmm for example: at the moment my level looks exactly like your screen shots above, but over a long enough path the player will fall off the edge closest to the camera if walking left or right. After following your guide the set-up of the level was if I added a simple cuboid in similar to yours the player would run diagonally and was running in the inverse direction. e.g. he ran right but faced left I managed to fix all this by multiplying by -1 (but this may have been a version issue you know how the new releases are) Any way thanks for your fast reply and ill have another look at this tomorrow.

    • Hmm, I’m still not quite sure. I mean at some point you were able to get your pawn to face and run in the same direction, correct? But you’re saying that if the platform he is running on is thin, that after a certain point in time he will run off one of the sides (either right or left)?

  3. Hey, i’ve read through your code and I i didnt seem to figure out which line of code that disabled the mouse. I really have no coding experience, but im making a game for my exam, and im in dire need of the code that disables mouse input. Could you possible post the code in a comment here? It would be most apreciated. Anwais, hope to hear from you soon.

    -Ørjan M

Leave a Reply to Ørjan Midthun Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.