Welcome guys! This will be the first post of probably a series of Unreal Engine tutorials. We are making this because we use Unreal Engine to develop our game and we think some of you will like to know how we make things work.
Today we will make a tutorial about a basic running for your character that zooms out when you are running and zooms in when you are not.
Let’s start with the setup. You can start with the "ThirdPersonCharacter" blueprint that Unreal Engine provides or with a character of your own. You will need the basic events that come with the blueprint so your blueprint should look something like this:
We will use our Chloe character for the tutorial, but we started with the same blueprint of the image.
After you are set up, you need to make a new function. This function will be called when your character starts running or stops running so that your camera zooms in or out in a smooth way. Make the following function with the name you desire, we named it "IncDecFieldOfView":
Make sure the variables "FieldOfViewAngle" and "Increase" have the default values 90 and false respectively. These two variables will manage if the camera is zooming in or out (the "Increase" variable) and the angle of the field of view the camera currently has (the "FieldOfViewAngle" variable). When you reach the desired angle (120 for zoom out and 90 for zoom in), you can stop the function with the "ClearTimer" node.
Now let’s make the "Run Event". We will use the Left Shift keyboard key for running. Make sure to put the event in your character blueprint. The event looks like this:
As you can see, we are calling and clearing the function made before in the "Run Event" to zoom in and zoom out the camera. We cleared it at the start of the event because we need to stop the function if it is currently active. If this is not done, it will zoom in and out at the same time. Also, we are using
• The "IsSprinting" variable to know if our character is running or not
• The "Increase" variable to tell the function if we want to zoom in or out the camera
• The "MouseSensitive" variable to determine how fast the camera will move when the character is running or not; and
• We are setting the "MaxWalkSpeed" variable to set the speed at which the character will move.
Make sure to set the variables "IsSprinting" and "MouseSensitive" to false and 3 respectively by default (the "MaxWalkSpeed" is a "CharacterMovement" default variable). Also, notice that we created two custom events ("ZoomInCam" and "ZoomOutCam") that we will call in another event later on.
We need an additional event. This event is necessary to know when the character stops moving even if you have the Left Shift key press and zoom in the camera if that happens. The event looks like this:
In this event we are using the variable "IsSprinting" to determine if our character is running or not (basically if you have the Left Shift key pressed). If it is, then it determines if the character is moving or not based on its speed. If it is moving, we set the variable "Stopped" to true to know that our character did run at some point. If it is not moving, then we see if the variable "Stopped" is set to true, if it is, this indicates that we ran at some point and we need to zoom out the camera.
Make sure to set the "Stopped" variable to false by default and to use the "DoOnce" node or it will keep calling the "ZoomInCam" over and over again.
We are almost done, but we need to do two more things. First, we will adjust the mouse movement to move with a lower sensitive when we are running. This can be achieved by modifying the "MouseInput" like this:
This will adjust the mouse movement with the mouse sensitive we want. This variable was set in the "Run Event".
Second, the only thing we have left is to call the "CharacterStoppedManage" event to know if the character stopped at some point and we need to zoom in the camera. We will do this in the "EventTick" that should look like this:
If you follow the tutorial, you should have something like the following gif (the functionality not the visuals that you can see):
We end the tutorial here. We hope you enjoyed it and feel free to comment if you have any questions and have a suggestion of what to show in the next tutorial.