Portal Like Game

Hello Everyone again,
I am currently trying to make a game similar to portal in which I am able to pick up blocks and place them on triggers and things happen. When I try to pick up the block I used Benny’s Example listed here:

My code looks like this but I dont seem to understand why the object does not follow the camera.

Is this possible or will I have to make button based puzzles instead of picking up and placing objects.
I am looking forward to your replies.

Hi Luc,

using variables that are defined inside one JavaScript block to another is unfortunately not possible. JavaScript blocks can only use Blockly variables.

Below is a Blockly script that helps you getting started with picking up items with click, and then dropping the same item with click.

Explanation of the code
We define two function-blocks with one parameter for the item we like to set the pickup / drop behaviour on.

The function block setPickupBehaviour parents the item you click on to the camera. To achieve that we use the JavaScript code Scene.getCamera().add(item). The method Scene.getCamera()always returns the currently active camera, so you don’t need to look for your camera by item ID.

When the onActivate event from setPickupBehaviour is triggered, we call the drop-function to set the dropping behaviour.

The function block setDroppingBehaviour sets a new onActivate event. When the item is clicked, it’ll be detached from its parent (our camera). After the item is detached, we call the function setPickupBehaviour to pick up the item later again.

These two functions are all we need! At the start of the scene you simply call setPickupBehaviour for the items you want to be pickup-able.

Here’s the script in action

You can even pick up more than one!

1 Like

Hi Stephan,
Thank you for your reply, this works wonders. I am making an adventure puzzle game in which I will post once it is complete. This make my job so much easier.
Thank you so much again.

Hi Luc,

glad to be of help. I’m looking forward to see your puzzle game in action!

Hello Stefan,
I have found a great way to optimize your code with physics. So that when the blocks are released in mid-air they do not just stay there idle.
Here is the new code that I used:
The main optimization is that it is added to physics on drop and removed on pickup

1 Like

Hey Luc,

great addition! Now the items feel really physical!