Welcome to another article about game development in Unreal Engine. For the past days I have been making a “motion matching” system to make the movement of my character a little bit more natural. Explaining motion matching is a little complicated on text, but this YouTube video explains what it is. This time I will tell you how I made the “tilting” the character makes when turning subtly. The same can be used to cause a more drastic change in direction, but I haven’t done that yet.
There may be different methods to achieve this effect. What I did was to first project two rotators. The first one is the character’s rotator, the one that shows the direction the character is facing. The second one is the “desired rotation” rotator, and this shows the direction we want the character to face as we move the camera. I am showing both rotators as arrows coming from my character’s pelvis. The purple one indicates the character’s facing direction, and the red one indicates the desired direction (you see they are slightly off, that’s because I moved my character’s camera slightly to the side and then switched to a debug view so I could show both arrows with a slightly different rotation).
Getting these rotators is pretty easy. First you need to create both arrows as children of your “Mesh (CharacterMesh0) (Inherited)” component and then decide which one is which. To get the “character facing” use the following to get the world rotation. This will create a rotator that will tell you where the arrow is pointing at all times (this tiny network is part of a bigger network you will see below, when I talk about the "Delta (Rotator)" node).
The next one is the trickier one. This one is supposed to tell you where your character is supposed to face. When Amelia is walking, and the camera moves, she changes direction so she always moves forward. This one is a combination of the movement input and the camera rotation, as you can see in the image below. My “Forward Axis Value” is the value of my “InputAxis MoveForward” node, and “Right Axis Value” is the value of “InputAxis MoveRight” (these come with the default third person template).
Then, all you have to do set this resulting rotation as the red arrow’s world rotation (I have called this arrow “Input Direction Arrow”). You also have to plug this to your Tick Event, so the rotations are updated on every frame. If you don’t do this, nothing will work.
If you move the character now, you will see the red arrow rotates as you move the camera. If it doesn’t work, make sure you made the connections correctly and also connected all this to your Tick.
And now the fun begins. The idea is to get the angle between both arrows, and use that to tilt your character in one direction or another. To get this angle, I use this network of nodes. As you can see, this comes after I get the desired rotation angle and set that rotation as world rotation for my red arrow (as I explained three paragraphs above).
First, I check if the rotation of both arrows is the same, within a 0.0001 threshold, and use this as a bool for a Branch that will decide whether or not perform the next actions (whether or not the arrows are aligned). Right now, skip the “Lean” variable and focus on the next Branch. Using a Delta (Rotator) I get the value of a subtraction between both rotators (not exactly the “angle” but it does the trick), and then break the resulting rotator so I only get the Yaw rotation. Using a “Greater than 0” test I can check the sign of the “angle”, and know whether the character is turning to one side or the other.
As you can see, the “Greater than 0” test is connected to a final Branch. I can connect the outputs to Print nodes and display a custom message so I know when the character is turning to the left or right.
For reference, the following image shows the full network, so you can see where I connected what. The red node at the top is a Custom Event that is fired from my Tick Event. I decided to do it this way so I didn’t have to deal with a long chain after my Tick, since I have a lot of other things already running on every tick.
One thing to keep in mind is the order of the vectors going into the Delta (Rotator) node. If you don’t put the rotators in the same order I did, you may get a situation where “your left is my right and your right is my left”. I would also like to explain briefly what the “Lean” variable is. As you saw above, Yaw of my Delta (Rotator) is stored as a variable called “Lean”. This variable will be used in the future, to drive the actual tilting of the character, making it tilt to one side or the other depending on the value and sign of the variable. I will explain how to use this in the next part.
Next part of this series is coming soon.