In this tutorial, we will be adding the "jumping" functionality to the player character that was built in the first part of this series (Creating a Player using C++).
Open Game.sln, which can be found within the solution folder of your project's main directory.
The location of the Game.sln file in the solutions folder
If you have not generated a solution yet, or are unsure about the location of the Game.sln file, follow the first steps of the Creating a Player using C++ tutorial.
private
class within Player.h and add a float
member variable with the name "m_jumpHeight
":The float variable for the character's jump height
Jump "height” technically represents more of a jump “force”. For example, if the gravity in your level is set to something like Moon’s gravity, jump “height” might not be applicable, as the height value would then be quite different compared to Earth. This level is set to standard Earth gravity, so jump "height" will suffice.
In the public
class of Player.h, create a new AddMember
line so that you can modify the jumpHeight
value within CRYENGINE:
desc.AddMember(&CPlayerComponent::m_jumpHeight, 'pjh', "playerjumpheight", "Player Jump Height", "Sets the Player Jump Height", ZERO);
Next, you need to define which input will be used for jumping, and how it is used.
Under InitializeInput
within Player.cpp, copy and paste an existing RegisterAction
and BindAction
line and modify it so that you have a unique eKI
(Space) and name for the jump action:
m_pInputComponent->RegisterAction("player", "jump", [this](int activationMode, float value) {});
m_pInputComponent->BindAction("player", "jump", eAID_KeyboardMouse, eKI_Space);
To avoid being able to theoretically spam your jump action input and fly into space, you will first want to check if the player is on the ground, then apply the jumping velocity. IsOnGround
is part of the pCharacterController
and does exactly that; check to see if the player is on the ground:
Move the empty curly brackets below the RegisterAction line and add in the following if
statement:
if (m_pCharacterController->IsOnGround())
The if statement to check whether the player is on the ground prior to jumping
Even if the jumping action key (Space) is pressed, and the player is indeed on the ground, you are still missing the logic that will modify the velocity which equates to the character's jump.
Add the following line below the previous if
statement:
{
m_pCharacterController->AddVelocity(Vec3(0, 0, m_jumpHeight));
}
This line adds upward velocity via AddVelocity
applied to Vec3
(in this case, only the Z axis) such that when we modify the jumpHeight
value in CRYENGINE, it will apply the given value to the Z axis.
Press Ctrl + Shift + S to save all tabs and build the solution. Once built, launch it in CRYENGINE 5.7 LTS.
Once the solution is built, you can test your player character in the Sandbox Editor.
Open the level you created in the Creating a Player using C++ tutorial, and select the previously placed Player Entity.
If you don’t have the Player Entity still placed, drag an Empty Entity into the scene and add a CPlayerComponent to it.
Player Jump Height in the Entity Component properties
Generally, a "realistic" jump height will fall somewhere between 2-5, but be sure to play around with setting the value much higher to get an idea of how your game should play.
The Air Control Ratio property
With these variables set for your Player Entity’s CPlayerComponent properties, you can now press Ctrl + G and test out your character.
This concludes Part 3 of the Creating a Player using C++ tutorial. To learn more about C++ in CRYENGINE and/or other topics, please refer to the CRYENGINE V Manual.
You can also follow this tutorial series in video form on our YouTube channel:
Tutorial - Creating a Player using C++
Tutorial - Coding in C++ - Creating a Player Controller - CRYENGINE Summer Academy