Triggering Trigger Volumes

 

Four box colliders surrounding a Paper2D sprite character in Unreal Engine 4.

I want my character to have directional triggers, so that interaction only occurs in the direction that the Hero is facing. In order to do this I'm going to make four separate trigger volumes that will flip on and off based on movement.

Trigger Volumes

Colliders can do some cool stuff. They don't have to be just physics based, though. We need trigger volumes for our Hero.

I created four box collision components and attached them to my Sprite component, then adjusted their sizes and positions to form a cross.

The three collision volume shapes offered in Unreal Engine 4.
Three types of collision shapes.


A list of components on an Actor in Unreal Engine 4.
Set up in the hierarchy.


What we want is to make these into triggers, since we don't want them to physically block anything. Name each one with a proper direction for referencing and then adjust the Collision settings in the Details panel for each with "Collision Presets" set to Trigger.

The collision set to trigger for a box collision component in Unreal Engine 4.



Now these four volumes will send out overlap data. But we don't want them on all the time. The goal here is to have them switch on and off. We only want the volume in the direction our Hero is facing to be on at any moment.

Red Light, Green Light


We previously set up an enumeration type variable that stores our direction data. This can be reused now for our triggers.

In the Event Graph for our Hero's Blueprint make a new function. Name it "SetTriggerDirection". Open it up.

Grab the CurrentMoveDirection variable from the left and Ctrl-Click and Drag it out into your new function. We want to get a "Switch on E_MoveDirection" node. This will execute only one pin at a time based on what the enum variable is set as.

A switch node in Unreal Engine 4 set up with an enumeration type variable.
Night time in the switchin' yard...


As we use our movement keys the CurrentMoveDirection will change. We need set up a series of "Set" nodes for our trigger volumes so that they turn on and off based on that switch's execution.

Ctrl-Click and Drag a reference to each of the trigger volume components. Get the "Set Generate Overlap Events" node for each one. The bool value just needs to correspond with the switch direction.

The "Set" node for generating overlap events for a collision component in Unreal Engine 4.


You'll need all four set for each direction accordingly. For the Up direction, check the bool for the Up trigger volume, and uncheck it on Down, Left, and Right. And continue with this pattern on all directions. 

You should end up with something like this:

A series of "Set" nodes attached to a switch to turn on and off collision volumes in Unreal Engine 4.
Full of triggers.


Now we just need to call this function. We want to do that after we set our animation, where the CurrentMoveDirection variable is set, on every frame. So attach it to the Event Tick after the SetAnimationFlipbook function.

Unreal Engine 4's Event Tick with two attached functions to be called every frame.


And that's it! You now have collision volumes that will turn on and off based on the direction your character is facing. We'll use this to create some interactions with objects and NPCs eventually.

- Matthew

Comments

Popular posts from this blog

Sprites & Tiles

Paper Character Setup & Movement