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 :
Lets 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. ->Blade mesh<- and ->trap mesh<- (Click to download)
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.
Here's the picture of the construction blueprint
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 raw 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.
To solve this problem, we are going to use polar coordinates.
With that settled all we need to do is the conversion from polar coordinates to Cartesian coordinates.
That marks the end of the construction script!
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.
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.
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.
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.
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!
Now you have everything you need to make your own Spinning Blades using Blueprints.
If you liked this tutorial, give us a like, share it with your friends and let us know what would you like to see in the future for more tutorials!