If you’re making a game, there is a big change that you want to be able to move your character around with keys from the keyboard. For example during a platformer game, where you can endlessly run/jump and where you need to avoid certain objects. But how do you that? In five simple steps, we’ll explain it to you.
In this tutorial you will learn how you can work with forces and sensors.
Open a new game and a bunny, which you can find in the bundle Tutorial. Give him a proper size, set him in the middle of your game and give him a name: Bunny.
Let’s start with letting our bunny going to the left, for this we need a Force. A force pushes an object to a direction you choose. In the upper left corner you can find the element Force. Set it somewhere on the left, outside the camera.
Give it a name, like: ‘ForceToLeft’ and turn enabled off. If you leave it on, our bunny will run out of the game without anyone touching any key.
Our Bunny will only walk in the right direction, when the force has the power to push the bunny. To give the force that strength, we need to go to the element settings of the force and go force. You will see two bars behind force with a zero (0). The first one represents the X-as and the other one the Y-as. By filling in a number at the X-as let the bunny make a horizontal movement and a number in the Y-as makes a vertical movement.
To make our bunny go to the left, we need a negative number, something like -3. Which means that the bunny will walk left three pixel per second.
The force is not yet connected to our bunny. You can connect them, by going to the element settings of the force and click on the star behind elements and next you need to click on the bunny. Also, you can write down the name of the bunny in the bar behind elements: =#Bunny.
Still our bunny won’t move yet, because there is no key that tells the bunny to move to the left. To add a key, we need a sensor (element section). Give the sensor a name: ‘ToLeftForce’ and place it somewhere near the force. In the element settings of the sensor, you can choose which key you want to use at Detect, we choose Left. This is the left arrow key.
The last thing we have to do, is to make sure the bunny only moves when we press a key. When we stop pressing the key, the bunny needs to stop too. To arrange this, we need two actions from the sensor to the force.
The first one from the sensor to force will enable the action: Event on OnDetect and Action on Enabled.
The second one will disable the action when the key isn’t used: Event on OnDeteced and Action on Disabled.
As you might have noticed, you only see one arrow instead of two, though you have made two actions. There is just no room to show them both, but in the element settings of the sensor you can find both of them and you can change them anytime by clicking on the chains and click on the cross if you want it deleted.
Repeat the steps to make your bunny go right, up or down. You can copy the sensor and the force while selecting the object and dragging it while pressing the control button. You do need to change the name force and chosen key. Also the actions need to be added again.
Well done! You finished this lesson. Test your game and see your bunny walk by pressing the left and right keys of your keyboard.
During this tutorial you’ve learned the following things:
- How to make you object move by using keys.
- How to use the force and sensor element.
Click here if you like to play this tutorial game and if you like to see how it's build, click here.
Please share your creations with us, we would love to see what other games you come up with. Good luck!