As of the 19th of April, I’ve used this week to further refine my top down Unreal prototype.
As of recently, I’ve decided to create sliding blocks for my game. These blocks are moved into any direction if the player collides with them at a certain face, it will then accurately move it along a grid using BoxTraceByChannel.
BoxTraceByChannel is a collision function that uses traces for its detection, the trace can detect any objects blocking the way and thus can provide a result.
“Sweeps a box along the given line and returns the first blocking hit encountered. This trace finds the objects that RESPONDS to the given TraceChannel”
An example of how I used this is that for the sliding block, traces are used to check each square in front of the blocks before moving it. If there is nothing blocking the trace, then the block is permitted to move – This is shown by the red colour as seen in Figure 1.
If the traces detect any solid objects such as walls or items, then it will display as green in front of the cube and not allow it to move as seen on Figure 2.
Here is an example of how it should work in game as shown in Figure 3, the sliding block should slide alongside the floor and stop if there is a wall in front of it. There is a trail of red traces alongside some green ones against a couple of walls, this is the collision at work.
To make this work, I gave the sliding block two collision boxes inside of it in Figure 4. As the player has a small pointed bit on its front, it will be able to activate one of these if it faces towards the cube.
If the player were to overlap onto one of these box collisions, it would cast to the player to see if they were pushing the block, and then get their X or Y location to see which side the player was colliding from – this is what can make the box go into different directions. (Figure 5)
Once collided, a box trace will spawn in front of the direction the block will be sliding to check and see if there are any collisions in the way. This is done by just spawning a trace 50 units ahead of the object and snapping it to a certain location with a custom function to make it more accurate. (Figure 6)
If the traces don’t collide with anything, then a Move Component function is used to simply move the sliding block ahead. (Figure 7)
This was a fairly hard thing to do, but with some help and research I was able to get it working.
From this I managed to learn how to use traces for collisions, which can prove to be useful if you’re looking to make objects that move accurately without having to depend on physics.
I also briefly learnt how to use custom functions such as the one to help me with object snapping shown in Figure 8, this would calculate a grid size and offset to snap an object to.
Docs.unrealengine.com. 2021. BoxTraceByChannel. [online] Available at: https://docs.unrealengine.com/en-US/BlueprintAPI/Collision/BoxTraceByChannel/index.html [Accessed 30 April 2021].