banner



How To Create A Character For Unreal Engine 4

Create a First Person character in Unreal Engine using C++ from Scratch

Sai Balaji

Hello everyone in this article we are going to see how to create a first person character from scratch using Unreal Engine and C++.

First person Game Mechanic

In video games, First Person is a graphical rendering of the player's character viewpoint to a game player. The most popular type of first-person video game today is the First-person shooter games, simulation-based systems like Flight simulator, etc. Here the game player cannot see the actual game character mesh. Though they may see some parts like hands, weapons, etc. And also games with a first-person viewpoint perspective does not require complex animation.

Some examples for First Person Player viewpoint

Village — BioHazard 8 (2021)

Halo Infinite(2021)

BioHazard 7(2017)

Doom(1993)

Creating a First person character using Unreal Engine

We are going to create a first-person character using C++ and then create a child blueprint class based on that C++ class. The characters we are going to create will look like this

Here as you can see the camera act as the viewpoint and provides a first-person perspective view to the player. The capsule component along with the skeletal mesh component, character movement component which has properties about character movement like walk speed, jump Z velocity, gravity etc comes by default along with the character class in Unreal Engine.

  • Open up the C++ class folder and right click create new C++ class

  • Select Character class and give a name to your C++ class and click create
  • Now Unreal editor will open your source code editor depending upon your preference.
  • Open up the header file of your C++ class as you can see it already comes with some template code for constructor and override methods like BeginPlay() which is called when the game starts and Tick() which is called for every frame . Add the following function declaration in public or protected section.Here each method corresponds to particular movement of the character.
  • Then declare an instance for UCameraComponent which act as the camera to provide first person perspective.Here we specify UPROPERTY() macro which tells Unreal reflection system that the given property should be accesed in Blueprint class created based on this C++ class.

NOTE: Make sure you import necessary header files

The entire header file code is given below

  • Now open up the project settings and under input section we add th key bindings to control the character using keyboard and mouse as the input device.

  • Now open up the CPP file of the FirstPerson character class which has implementation for constructor,BeginPlay() and Tick() function declared in header file.
  • Now we need to initialize the camera instance in the constructor.

Here we create actual camera component by using CreateDefaultSubobject<T>() template method which creates the actual component by specifying the component class type in our case it is a camera component so UCameraComponent class and passing in the name for the component in a TEXT() macro.Then we attach the camera to the root component which is a capsule component in character class.Then in order for our character to crouch we enable th crouch property of the character movement class as true.

  • Next create implementation for the method declared in the header file.

AddMovementInput() : This method Add movement input along the given world direction vector (usually normalized) scaled by 'ScaleValue'. If ScaleValue < 0, movement will be in the opposite direction.The scale value is the axisvalue parameter in the function. Its value can range from +1.0f to -1.0f.

This method takes a parameter WorldDirection which is a FVector.For foward backward movment we obtain forward vector of the character and multiply it with scale value. In our case when player press W the scale value will be +1.0f which is multiplied by forward vector and it becomes a +ve vector and player moves forward similarly when player press S the scale value will be -1.0f which is multiplied by forward vector and it becomes a -ve vector and player moves backward the same concept is applied for right and left movement

AddControllerPitchInput() The rotation around Y-axis as is called as pitch it deals with up and down look of the character

AddControllerYawInput() The rotation arounf Z-axis is called Yaw it deals with left and right look of the character.

Crouch() and Uncrouch() This two methods are built-in methods of character class to perform crouch and uncrouch action.

  • Next we need to actually hook the input keys with those functions. It can be done by using SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) method of character class which comes from pawn class as character class is a subclass of pawn class.Inside that function add this below lines of code

Here we setup actual axis input and action input and hook them with our movement function by passing the reference to the function to be called.

Now compile the code and open up the C++ folder in ue4 editor right click and create a blueprint based on this C++ class.

  • Open up the blueprint make sure you tick Use Pawn Control Rotation in the camera option.This will allow the camera to rotate based on mouse movement as it act as the controller.

  • Now press play button in the editor to see the output

Complete FirtPersonPlayerCharacter.cpp code

If you like this article give some claps and also check out my second game recently released.

How To Create A Character For Unreal Engine 4

Source: https://medium.com/techiepedia/create-a-first-person-character-in-unreal-engine-using-c-from-scratch-291cfef5c194

Posted by: clemensupout1943.blogspot.com

0 Response to "How To Create A Character For Unreal Engine 4"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel