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)
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”.
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”.
Click Play to play your game, and make sure to give your script a name. When you play, the object should follow your mouse.
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.
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.
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.
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.
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.
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.
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.
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.
Finally, grab the “true/false picked up?” block from Variables, and connect it to the “if” block.
Now when you play your game, you’ll be able to pick up and put down the object by clicking on it.