This is my approach on setting a third person character for a RPG game I am developing. By building it from scratch using C++, I wanted to gain complete control over how the character moves, interacts, and feels within the game world.
This is just a mini post to explain my thought process and step by step procedure of building this.
github repo : https://github.com/RahulChandra99/shadowfire-unreal
A. Creating the Character Class
I started by defining the class 'ASFCharacter'. This would serve as the blueprint for my character, handling all the core aspects like movement and camera control. This class inherits from the base class 'ACharacter'.
1. Setting Default Values and Attaching Components
In the constructor, 'ASFCharacter()', all the essential components are initialized:
Explanation:
Spring Arm Component:Â
The spring arm is essential for third-person games because it helps manage the camera’s distance from the character, ensuring a smooth and responsive follow mechanic.
By attaching the camera to the spring arm and enabling 'bUsePawnControlRotation', I ensured that the camera follows the character's movements while also allowing the player to control the view.
Character Orientation:Â
The setting 'bOrientRotationToMovement' allows the character to face the direction it’s moving, which is crucial for immersion in an RPG. This way, if the player moves the character forward or sideways, the character’s model turns accordingly.
2. Implementing Movement Controls
I implemented two functions for movement: 'MoveForward'Â and 'MoveRight'.
Explanation:
MoveForward() :Â
This function takes a float value representing the player's input and moves the character forward or backward depending on the input's sign.
I made sure to zero out the pitch and roll of the control rotation to keep movement strictly horizontal, which is what you’d typically want in a third-person RPG.
MoveRight():Â
This function handles strafing. By calculating the right vector from the control rotation, the character can move left or right relative to where the camera is pointing.
3. Binding Inputs
This is done in the 'SetupPlayerInputComponent'Â function:
Explanation:
Binding Axes: I used BindAxis to connect the input axes (like "MoveForward" and "MoveRight") to their corresponding functions. This allows the character to respond to the player's analog stick or keyboard input in real-time.
Turn and LookUp:Â These functions were bound to the default Unreal Engine controls for camera movement, allowing the player to look around freely while moving.
Note: Will switch to new Enhanced Input Action System later in the project
Comments