Using the mouse to control objects

Knowledgebase / Using the mouse to control objects

We’re going to detect when the mouse is clicked to trigger actions, and use the position of the mouse to move things. This tutorial builds on concepts learned in Setting an object’s position and Using variables.

Step 1: Constantly setting an object’s position

We’ll start off by using an object as a mouse cursor. Find a game object in your level and right-click on it to add a script to it. (You can use any game object, but this example uses objects from the Boardgames Pack)

Screen Shot 2016-07-25 at 3.44.03 pm

We’ll get this object to constantly follow the mouse by setting its position. First grab the “Constantly” block (from Events), then grab two “set x position of myself” blocks (from Transform), and drag them inside the Constantly block. Change one of the blocks from x position to y position by clicking on the “x”.

Screen Shot 2016-07-25 at 4.23.12 pm

Screen Shot 2016-07-25 at 4.21.04 pm

We want to set both the x position and the y position of this object to match the x and y position of the mouse. Grab two “x position of mouse + camera x” blocks from Sensing, and drag them into the slots where the zeros are. For the “set y position” block, make sure the blocks are changed to “y position of mouse”, and “camera y”.

Screen Shot 2016-07-25 at 4.28.20 pm

Click Play to play your game, and make sure to give your script a name. When you play, the object should follow your mouse.

ezgif-2357145986

Step 2: Using a variable for picking up and putting down the object

Next we’ll add the ability to pick up the object, and put it down. Go back to editing the script by clicking Edit in the top-right corner.

We’ll create a boolean variable (true/false) that we can use to set whether or not the object has been picked up, and if the variable is set to true, then we’ll allow it to follow the mouse, but if it’s false, we won’t allow it to follow the mouse.

Start with a “When created” block (from Events), and drag that block out into the workspace. Then go to the Variables section, and from under Properties, grab the “set true/false i” block and drag it inside the “When created”  block.

Screen Shot 2016-07-26 at 10.49.13 am

Now click on “i” to rename it to “picked up?”. Also grab the “true” block (from Operators) and drag it into the empty slot in the variable block. Click on it and change it to “false” so that the object is not picked up at the start of the game.

Screen Shot 2016-07-26 at 10.51.15 am

To turn this variable on and off, we’ll use the “When the player presses myself” block from Events, so drag that out into the workspace. Then grab an “if” block from Control Flow, and drag it inside the “When the player presses myself” block.

Screen Shot 2016-07-26 at 11.14.33 am

Click on the cog on the “if” block to change to to an “if else if” block by dragging the “else if” block inside the “if” block.

Screen Shot 2016-07-26 at 11.15.18 am

Grab two “true/false picked up?” blocks from Variables, and grab a “true” block from Operators. Drag one of the “true/false picked up?” blocks into the “if” slot, and then drag the “true” together with the other “true/false picked up?” block, and put those in the “else if” slot.

Screen Shot 2016-07-26 at 11.20.34 am

Lastly, grab two “set true/false picked up?” blocks (from Variables), and drag both of those blocks into the “if do” and “else if do” parts of the “if” block.

Screen Shot 2016-07-26 at 11.36.26 am

Then grab two “true” blocks (from Operators), drag one of them into the variable block inside the “else if not” section, and change the other one to “false” (by clicking on “true” and selecting “false”) and drag it into the other block.

Screen Shot 2016-07-26 at 11.35.43 am

What this will do is check if the object is picked up, and if it is, it will stop picking up the object, but if the object is not picked up, it will pick it up.

Step 3: Checking if it’s picked up

Now that we’ve got the variable turning on and off, we’ll get the movement part of the script to move the object only if it’s picked up.

Drag out the “set x position” and “set y position” blocks (inside the Constantly block), grab a new “if” block (from Operators), and place the “if” block inside the Constantly block. Then drag the “set position” blocks into the “if” block.

Screen Shot 2016-07-26 at 11.48.56 am

 

Finally, grab the “true/false picked up?” block from Variables, and connect it to the “if” block.

Screen Shot 2016-07-26 at 11.59.46 am

Now when you play your game, you’ll be able to pick up and put down the object by clicking on it.

ezgif-4022695406

Good work!