Opening Doors With Unreal Engine
This week’s blog is about getting not just doors but Actors within your world to rotate or move, we will be opening and closing a door as an example on how this all works within the Unreal Engine. We will do this with the help of FRotator, which is a container for rotation information, all values are stored in degrees. A rotator is a collection of three floats stored inside one variable that represent the rotation in a 3D space. It does this with the X, Y, Z values. These values are also referred to as “Roll”, “Pitch”, “Yaw”, the rotator is limited to a value of 0.0 to 360.0. To start the process of writing the code to make the door open we need to add some variables to our header file so that we can use them and initialize them within the “.cpp” file.
float CurrentYaw; UPROPERTY(EditAnywhere)
float TargetYaw = -90.f;
UPROPERTY(EditAnywhere) gives the “Details” panel of the Actor access to the modified variable within in the editor itself. Now once the header file is set up with our variables we will set the value of
CurrentYaw = FMath::FInterpTo(CurrentYaw, TargetYaw, DeltaTime, 1);
Lets take a look at what
FInterpTo is doing and what is being passed into it as parameters.
FInterpTo works almost the same as
Lerp but the difference between them is that with
Lerp you pass in the starting position, the end position and the percent increase for each time it updates. With
FInterpTo you pass in the current position, the ending position, the
DeltaTime , which we will talk about in more detail, and the speed at which you want the interpolation to happen. With
FInterpTo it interpolates the float from the current position to the target position, and it is scaled by distance needed from that current position to the target position. It will have a quick start then it will slowly ease to the target position as it gets closer.
Some more in depth look at what is
DeltaTime and how it works. When talking about DeltaTime in Unreal it is basically how long it takes Unreal to calculate a particular frame. So for example if we had three separate frames from a game, in each frame the objects are moved or rotated. The time that it took Unreal to move from “Frame 1” to “Frame 2” is stored in what is called Delta Time. Using Delta Time also makes it frame rate independent, which is import when making your games. If certain things in your game were frame rate independent your game might run into some issues within the world. If you had a game that drove a car for instance, and it is not frame rate independent, the speed and distance traveled by that car can vary between different machines. If you set the speed of the car to go 10mph and go a distance of 1 mile, on a processor that runs the game at 30fps (Frames Per Second) it will take 30 frames for the car to reach the 1 mile distance. Now if the game gets played on a better processor that can run it at 60fps and you have it frame rate dependent then the speed and distance for your car has now doubled. So it is very important to use Delta Time so that you can make sure that it stays frame rate independent and can avoid any issues with it being ran on multiple different machines.
CurrentYaw value has been set correctly using our
FInterpTo , we can create an
FRotator for the rotation of the door. As we discussed earlier the
FRotator is going to store X, Y, Z values of the door for us. We are only going to need to change the value of Z so that we can get it to open and close properly. Because we create this
FRotator we now have access to change only the “Yaw” value of
DoorRotation like so :
FRotator DoorRotation = GetOwner()->GetActorRotation();
DoorRotation.Yaw = CurrentYaw;
This will constantly keep the “Yaw” value updated to the current position of the door which will be moving closer and closer to the target position thanks to our
FInterpTo from earlier. The last and final step to get the door set up to open and close properly is to actually SET that new
DoorRotation.Yaw value to the Actor itself. We do this by simply passing in our
FRotator into the
SetActorRotation function :
FInterpTo are ways to make movement and rotations to your Actors depending on how you want to move things whether it is simply swinging open a door or moving a character in a 3rd person world.