Tutorial: How To Make a Cube Roll On A Grid in Unreal Engine 4


In this tutorial, I will show how to make a cube roll from side-to-side on a grid. This project took me around 3 days to complete, though much of that was wrapping my head around basic things in UE4. Some of the areas explored here include setting up key bindings and action mapping, setting and clearing timers, and rotating around an arbitrary point.

First, you want to start by setting up the action mapping. What this does is basically binds an input button (like a keyboard key) to a particular action (really just a name that you make up). For this test I needed 4 keys for up, down, left and right. Since the arrow keys and WASD were already used by the default Unreal fly camera, I choose to employ the IJKL keys to move the cube around. These options are found in “Settings->Project Settings->Engine->Input->Bindings”.

UE4_Cube_Grid_01

Next, we will need to connect those actions to a function in order to make something happen on the screen. You can do this by creating an “InputAction” Blueprint and choosing the name you created (like “Action_Up”). I set these actions to call a function I created for this example called “StartMoving”. In some cases, you may want to bind the action to a custom event, which may simplify the code and allow for a cleaner Blueprint (as you can use events to effectively “jump” to other points in the same Blueprint without a line connecting them). In this case, I had to pass in some state (to know which direction the cube is moving) so using a function was better suited for the job.

UE4_Cube_Grid_02

The “StartMoving” function is not that complex. All it does it check first if the timer is active (to avoid registering input while the cube is already moving), save some state on the cube itself (the direction of movement and it’s last position), and then creates a timer to handle the animation.

UE4_Cube_Grid_03
The key part is the creation of the timer itself, using “SetTimer”, which is shown below. You can think of a timer as spawning another process or thread that can run concurrently with your main logic. The timer just calls a function of your choice (“MoveTween” in this example), at a specified time interval (use 0.0166 for a 60Hz animation cycle, which looks smooth enough). You can also set whether it loops or not, we use looping in this case.

UE4_Cube_Grid_06
Finally, we have the “MoveTween” function, which is what is called by the timer and what does all the math calculation to rotate around a particular axis and point in space. It’s also what animates the cube, and clears the timer at the end itself. It’s very useful to be able to spawn a timer, and then have it terminate itself when necessary. The full function is shown below.

UE4_Cube_Grid_04

Really, the core math of the method is done by two function included in UE4, “RotateVectorAroundAxis” and “RotatorFromAxisAngle”. We first create a axis based on the movement direction. Then we subtract the pivot point (in our case, the bottom edge of the cube), rotate around that axis, and then add the pivot point back to the location. The rotation around axis logic is actually based on an example shown on the Unreal Answer Hub, though it was modified to fit my purpose.

UE4_Cube_Grid_05

And that is basically it. I’ve even included the full project file below for you to download and experiment. I imagine this type of motion could be used for a type of puzzle game created in Unreal Engine 4. Download Link: UE4.7.4 Cube Motion Project Files

4 Comments

  1. Thanks,
    Hope this builds into a UE4 tut series for VR,eventually.

  2. Hi.. Thanks for the info…
    i have something similar. but i kick cubes around a grid..
    But i was wondering if you could help me out with some direction.
    I’m able to kick cubes around a grid.. ie 1 grid per kick.. and can kick them when they are multiple cubes lined up and i know what group of cubes to kick if there is a gap between cubes etc.. But now im trying to do what a long kick..
    Which is to be able to kick the cubes the length of the grid. (no probs)
    But im struggling on how to add more cube to my trace array once the cubes have started to move … and if there is more cubes in the way they need to get pushed too…
    Not sure if i explained it so good. Hopefully you may have some direction..

  3. Hi,

    Can you brief about the Move Tween function components?? Specially, “Horz Direction”, “Vert Direction”, “Pivot Point”. How are you getting /Calculating these values. And can you please explain how are you defining the new axis of rotation ??

    Eagerly waiting for the reply.

  4. Thanks for this superb tuto (hardly) and your blueprint… Please make an serie of tutos or make a mouvement dungeon master/grimrock style…

Leave a Reply

Your email address will not be published. Required fields are marked *