Part four in the development of our MMO-style controller. The original specification with contents and an overview can be found in this post.
We’ll be adding in the options to rotate the character and camera this time, using the A and D buttons. It’s the final basic input axis for controlling the character.
We want to rotate around the character and also have the character rotate as we do so, depending on which way we go. There is one complication: as per our design, if the right-click mouse button is held down, we want to strafe with our character instead of rotating.
Once again, I’ve written this ahead of time, so I know where to split this into useful functions that will be useful later. Add this code in MyPlayerController.
Rotating the Camera
First things first – rotate the camera using the A and D buttons.
This function takes the axis value and rotates the camera boom. We break the rotation into components, and add the scaled axis value to the yaw value. The pitch is kept the same, so keep previous positioning of the camera above the character, and zero the roll, since we don’t want that at all. With that rotation calculated, we set the CameraBoom’s world rotation.
Rotate the Character to Camera
This function rotates the orientation of the character controller to the direction the camera is facing.
This function simply updates the character controller’s rotation to the same as the CameraBoom. In this instance we don’t need Pitch or Roll, since our character can only move (normally) on a plane.
Done! This completes character rotation.
Rotating the Character
To fully implement the rotation function using the two functions above:
Here, we have 3 areas to consider:
First, in the far left box, we check to see if the right or left mouse buttons are pressed down. We branch on this test, to either strafing or rotating.
If RightMousePressed OR LeftMousePressed, we strafe. Since we’ve already written handling the strafing axis, we simply set up the Add Strafe Input function like before.
If no mouse buttons are pressed, we use our two newly written functions, RotateCameraTurn and RotateCharacterToCamera. To finish this off, we also update the actor in the same way as we updated the controller. This could perhaps be another function, but it wasn’t utilised anywhere else, so I did not bother.
Improvements for the future
If you implement things as above, rotating the character on the spot looks a little odd: the actor just spins around without moving.
To remedy this, we’d need to add a rotating animation, and add that to the state machine which controls animation transitions, in MyCharacter.
I’m no animator, and I haven’t found anything I could drop right into the project with minimal fuss (I’ll be honest – I didn’t look).