Creating a Spinning Blade Blueprint

Hey! I’m Ram, Unforgiven’s programmer. Today, I wanted to share a little tutorial with you guys,
because it shows some interesting concepts that you might use while working in the unreal engine.
This blueprint is a part of the new Customizable Deadly Traps, which is coming up soon for Unreal’s Marketplace.

Here, I’ll talk about timelines, events and instanced static meshes. First, lets see what we are going to accomplish :

Any number of blades
~

Normal spinning blade trap

Initial Setup

Let’s start by finding the meshes that we will use. We need the base of the trap and the blade that will be spinning.
You can use the meshes that I used as placeholder when making this blueprint.
(**Meshes can be downloaded from our website : http://www.unfgames.com/blog/2016/1/18/spinning-blade-blueprint-tutorial **just go to the same post, and click on the names of the files)

Now that we are ready to create the blueprint, we will create an actor blueprint with a static mesh component and an instanced static mesh inside.

Change the static mesh in both components

Also, let’s set the z location of the blades to 153.

Construction Blueprint

Here’s the picture of the construction blueprint :

You should check it full size, for that, click here :
~ https://static1.squarespace.com/static/568dac3f40667a4dbfc13dfc/t/56a703dbc21b869ca6854880/1453786085384/SpnBladeConstruction.png

It won’t work properly, but you can use this pastebin as an starting point of the construction blueprint if you don’t want to start from scratch
just click on download and then copy and paste it in the blueprint.

In the construction blueprint, what we want to do is set the blades up, and also the collisions, so we can do something when a pawn overlaps any of the blades.
To start with it, we are going to create a integer variable with the name NumberOfBlades that will be used by the for loop to help us add as much instances of the blades as we need. Set the default value of this variable to 1.
Also create a vector variable with the name ModifyBoxExtentBy and a default value of (0;0;0) just in case we are not satisfied with the box collision extent.

Spawning the blades is easy!

What we need to keep in mind is the rotation and location that we are going to use to spawn each blade,
since our blade has the pivot at the side we will only worry about the location.
Using simple math we take 360 and divide it by the number of blades we have. (ex. 1 blade -> 1 blade with 360 rotation, 2 blades -> 1st blade with 90 rotation and 2nd with 360, etc).

After spawning the blade it is necessary to attach to it a box collision component so we can do something when the blade hits a pawn.
Now that we set that up we find ourselves in this problem:

If we use the same transform for the collision boxes it generates a problem, because the pivot for the collision box is in the center.

To solve this problem, we are going to use polar coordinates.

θ is the angle in the image

With that settled all we need to do is change the conversion from polar coordinates to Cartesian coordinates.
That marks the end of the construction script!

Event Graph

In the event graph, the first thing we are going to do is to create some variables that we’ll be using for our next step:

*Bool IsOn | default value = true | If true the trap will start active.
*Bool IsSpinning | default value = false | If the blades are spinning at full velocity this will be true.
*Float RevsPerSeconds | default value = 1 | Revolutions per seconds.
*Float TransitionRate | default value = 1 | Play rate of the transition. Greater number = faster transitions between on and off.
*Bool UseIntervals | default value = false | If checked the trap will be in the on state for an interval of time before switching off. This behaviour is used for the on and off states.
*Float StateTimeinterval | default value = 3 | Time in seconds that the trap will be on or off.

Now lets take care of the event that is going to fire every time a pawn begins to overlap a box component created in the construction script.

Event BeginPlay

**Make sure you check the full size image : https://static1.squarespace.com/static/568dac3f40667a4dbfc13dfc/t/56a7a7f1a12f446dcabca5a9/1453828083536/SpnBlade_BeginPlay.png **

What we are doing in the event BeginPlay is going through the CollisionBoxes array and binding an event to OnComponentBeginOverlap, then renaming that event to HittedSomething. When fired, the event HittedSomething will cast the actor to a pawn and if the cast is successful it will execute anything we want.
After the loop is completed we will check if a boolean called IsOn is true and if it is, we will call a custom event named TurnOn.

Event Tick

**Make sure you check the full size image : https://static1.squarespace.com/static/568dac3f40667a4dbfc13dfc/t/56a7ad331c1210756e3413ee/1453829428262/SpnBlade_Tick.png **

In the tick event, we will apply the rotation to the blade(s). With a branch node we check if the trap is On and if it’s spinning. Then we proceed to add the relative rotation to the blades in Z. Multiplying the RevPerSeconds by 360 gives us how much rotation should we add in a second and multiplying that value by the delta seconds gives us how much rotation should we add every tick.

Custom Events TurnOn and TurnOff

Right now we have a working blueprint that spins if it is on and when it hits something an event fires but there is no smooth transition between the states on and off.
So these events are going to help us in that task and also provide us with a way to easily change the states during execution.

**Make sure you check the full size image : https://static1.squarespace.com/static/568dac3f40667a4dbfc13dfc/t/56a7b45e4bf1188a1b31f8dd/1453831268350/SpnBlade_OnOff.png **

Let’s look at the event TurnOn first. This event sets the isOn variable to true and then uses a for each loop to activate the collision of all the collision boxes. When this loop is completed the timeline named TurningOn will play from start.

One point at time 0 and value 0 and the other at time 2 and value 1.

In each update of the timeline we will add relative rotation to the blade and when it is finished the direction is going to be checked. If the direction is forward it means that the blade was in a off state, is turning on and now its at full speed so IsSpinning should be true.
Also we should disablethe collision of the blades because if they collide with something they are going to cut it and not bump it.

Finally, we check if we are going to use intervals, if we are using intervals the time will be set through a delay node that will call the TurnOff event when time is right.

The TurnOff event will be similar to the TurnOn event but with minor changes. instead of playing from start the timeline will reverse form end and when it finishes we will set the variable IsSpinning to false, enable the collision of the blades and disable the collisions of the boxes.

And that’s it!

I hope you will find this tutorial useful and don’t be shy to ask any questions and comment if you liked it, so I can keep doing more!
This is a part of our upcoming “Customizable Deadly Traps” UE4 Marketplace package!
If you want to check it out, here it is : Customizable Deadly Traps - Marketplace - Unreal Engine Forums

Check our Blog: http://www.unfgames.com/

That’s awesome, well done!