Collision handlers in CoBlocks

Hi @Outpost_31,

here’s an example on how you can set up collision events for one item against many different ones.

Solution a)


In the scene: a Boy-item walks forward into 5 different cubes.

We create the when item collides with [anotherItem] - block. This block will trigger whenever the boy collides with any other item in the scene.

When you drag this block into the workspace it creates a variable called "anotherItem". This variable can be found in the Data -> Variables category and represents the latest item the boy has collided against.

To create different behaviour for any of the other cubes, we use if-statements to check what kind of cube has been collided against like this:


Here we create if-statements to check which cube was collided, then let the boy say something different depending on the cubes.

With this script in place, let’s move our item forward and see what happens! Make sure that event-handlers are created before you animate items, otherwise the handlers are set after the item is done moving.

Click to see final code & result

Solution b)

We add event handlers on the cubes instead, not the boy. This way we make sure that only one collision handler is on any given item.

Show final code


etc…

Wrapup

Solution a) is more elegant as you can manage collisions from one event-handler. Solution b) is more suitable for quick collision testing between a few single items.

Hope this helped! Click here to make your own copy of the examples by clicking the remix button!

4 Likes