Standing Inside The TriggerVolume

Danny Padron
6 min readJun 11, 2021

“Triggers” and “TriggerVolumes” what are they in Unreal and what can they do? When talking about “Triggers” within UE4 it is basically an Actor that is used to cause an event to occur when they are interacted with within your world by another Actor or object. A “Trigger Volume” is the same as regular Trigger except that it has a designated amount of space or “volume” that can detect when things are entering or leaving the designated area. The default triggers that can be used within UE4 are generally all the same only varying in the shape used for the volume, the three types are “Box Trigger”, “Capsule Trigger”, and “ Sphere Trigger”. We are going to look at adding a Trigger Volume within our world that is activated by the Player walking into the designated area and opening the door. Once we have this setup we will then also make it so that the door will close once the player leaves the designated volume. To get started with this, first we have to have a Trigger Volume within our World, within the Unreal Editor we will look within the “Place Actors” tab and find the side tab named “Volumes”. From here we will find the “Trigger Volume” and drag and drop it into our world and name it “PressurePlate”, because essentially that’s what it is going to be like when we are finished, the player will “step” on the plate opening the door and when he “steps” off the door will close. After adding this new Trigger Volume you will notice in your “World Outliner” tab that you will have “PressurePlate” listed there, now we have to connect it to our “OpenDoor” component.

To connect the “PressurePlate” trigger to the “OpenDoor” component we first have to add some things to our OpenDoor.h . We will be using ATriggerVolume* DoorTrigger to declare our volume as a pointer named DoorTrigger , so we need to include a header file with :

#include "Engine/TriggerVolume.h"

Once the include is added we will add a another UPROPERTY to our OpenDoor component. We will declare it and name it like so :

UPROPERTY(EditAnywhere)
ATriggerVolume* DoorTrigger;

Now we go back to our Unreal Editor and compile our code and if we open our OpenDoor component we will see a new drop down option.

And we will set the newly added drop down named “Door Trigger” to the PressurePlate that we added in our world. The OpenDoor component is now linked to the PressurePlate trigger volume we have in our world. The next step that we need to do is to make so that our player is allowed to trigger the PressurePlate and have it open the door when stepped into the volume. The easiest way to do this will be to give our OpenDoor component yet another dropdown option so we could simply select ourselves as the “trigger” for the Trigger Volume. To do this we go back to our header file and add another UPROPERTY like so :

UPROPERTY(EditAnywhere)
AActor* OpeningActor;

These two lines of code will create yet another dropdown now within our OpenDoor component.

If you go to open the dropdown and select yourself to be the Actor that triggers the volume you will notice that nowhere in the dropdown or in the “World Outliner” tab will you see an Actor that is the actual “Player” character within the world. The reason this doesn’t appear is because in the current state of our game our “Player” character ONLY exists when we actually hit “Play” within the Editor and that is because it is only a Temporary Pawn that is created whenever “Play” is hit. So how do we set the Trigger Volume to recognize the “Player”. When we hit play you will notice that “Default Pawn” is your “body” per say but it’s only temporary within the world, where as the “PlayerController” persists, it’s like your “mind”. Now we add some extra header files within our OpenDoor.h file and we can get access to that temporary pawn that is created when the game starts.

Getting access to the “Default Pawn” is fairly easy we just need to have the correct header files added so that we can use certain functions and pointers to “Get” that default pawn. First header file we will add is going to be

#include "Engine/World.h"

This will give us access to use GetWorld() which when checked you will see that it is a pointer, so we can use the -> operator on it. The next header file we want to add is going to be

#include "GameFramework/PlayerController.h"

This header file will give us access to the pointer GetFirstPlayerController() . So we have GetWorld()->GetFirstPlayerController() , which means that in this level GetWorld() references the particular level we are working on because a game can have multiple levels at any given time, and GetFirstPlayerController() will get the very first PlayerController, because again there can be multiple characters that can be controlled within certain levels or games. Then once we have the first Player Controller we can get the pawn that is our “player” when the game starts and set it as the OpeningActor like so:

OpeningActor = GetWorld()->GetFirstPlayerController()->GetPawn();

The final GetPawn() function returns a pawn which is helpful because a pawn can inherit from an Actor which is what we are doing by setting OpeningActor to this temporary pawn that is getting created. Once this is setup within our OpenDoor.cpp file under our void UOpenDoor::BeginPlay() function we will then be able to start the game and the door will appear closed and then if you move your “Player” into the area that you placed the Trigger Volume the door should open once you walk into the Volume and you will be able to walk out the door. But for a game, that’s too easy, so we have to set it up like an actual Pressure Plate where once you “step” out of the volume the door begins to close and the player then needs to rush to the door and try to get out before the door closes.

Having our void UOpenDoor::OpenDoor(float DeltaTime) function makes closing the door fairly simple and easy. We can simply copy the open door function just have to change the name of the function first, then there is one other switch that we need to make to it. Instead of having our FMath::FInterpTo set to TargetYaw which the target is to have the door open we want to set it to InitialYaw so that it will go back to the closed position that it started at. So here is what both our OpenDoor and CloseDoor functions will look like :

void UOpenDoor::OpenDoor(float DeltaTime)
{
CurrentYaw = FMath::FInterpTo(CurrentYaw, TargetYaw,DeltaTime, 1);
FRotator DoorRotation = GetOwner()->GetActorRotation();
DoorRotation.Yaw = CurrentYaw;
GetOwner()->SetActorRotation(DoorRotation);
}

void UOpenDoor::CloseDoor(float DeltaTime)
{
CurrentYaw = FMath::FInterpTo(CurrentYaw, InitialYaw,DeltaTime,1);
FRotator DoorRotation = GetOwner()->GetActorRotation();
DoorRotation.Yaw = CurrentYaw;
GetOwner()->SetActorRotation(DoorRotation)
}

The only thing to do is to add this function to our OpenDoor.h header file then these two blocks of code will be what create the animation of opening and closing the door. Then the final step to have the door open and close when our Player is inside the volume is within our void UOpenDoor::TickComponent function we add the if/else functionality so that if our player is in the volume the door will open else the door will close. The function will look like this :

void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFucntion)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if(DoorTrigger->IsOverlappingActor(OpeningActor))
{
OpenDoor(DeltaTime);
}
else
{
CloseDoor(DeltaTime);
}
}

Now within the Unreal Editor if you compile your code and hit play you will notice the game will start with the door closed which is good, means you are “trapped” within the room. You can then move your player into the trigger volume area and the door will open and when you try to race and run out the door to escape and leave the volume the door is going to begin to close making it a little more difficult to escape the room instead of just leaving the door opened.

--

--

Danny Padron

Full stack web developer with experience in Ruby on Rails, JavaScript, React and Redux and learning more!