In this tutorial, we explain how to make a character that is controlled only using the keyboard (forward, backward, turn left and turn right).
Contents |
We developed this tutorial inside an arena named CreatingKeyboardControlledCharacter in the project Free4All. You may clone the arena and see this tutorial in action. Even better, you could create a new arena inside that project and follow this tutorial to create a new character from scratch.
Character Object
Firstly, create an object called Character in the arena, using the default object template.
- Right-click on the arena object in the tree >> new >> object, like so:
- Name the object Character and select the object template default:
If you want to make a multiplayer game, you should consider creating the models, interactions, etc. inside a presence template, because, well, that's what they are made for: to create objects that represent players in the game environment. More about making a multiplayer game here.
Character Model
The first thing you need is a model for your character. Right-click on the object Character >> new >> model. This will open the Model Editor to create a 3d model. Name the model my_character_model, pick a mesh and press "Save". We used a mesh called soldier, which is part of the Media Library Desert Attack. This Media Library is free, you just have to subscribe it in order to use the model.
The physics engine inside ludiloom has several features to manage character control: physics, movement, interpolation, etc.. We will show how to use these features in this tutorial. More about this interface here. After pressing "Save" in the Model Editor, open the Physics Editor (the button 12 of this picture). Choose "Character" in the physics type box.
Configuring the physics is mostly a task of tweaking, so you are more likely spend more time in the physics interface after the character is 'working' than at this point. You can find a more detailed explanation about this interface here, but here are some things you have to do:
- Set the Mass to 100, so your character behaves properly to gravity and other physical forces.
- Set synchronization to global only if the game is multi-player.
- Enable collision and gravity.
- Set the offset, scale and rotation of the capsule. If you look at the 3d model in the game scene while this editor is open, you'll see a green capsule around the model. This capsule is what really matters to the physics engine. Sometimes we have to adjust the capsule to the model. In our soldier model, we adjusted the vertical offset and rotated the capsule 180 degrees.
- Set the speed of the model. We set it to 40 for forward and strafe movements, 30 when going backwards.
- Define how much the character can climb and jump.
- Configure the walk and jump sounds.
Movement
It's time to create some interactions to make the character move. We will create the interactions inside the same object that contains the character model. Right-click on the object Character >> new >> interaction. Each interaction contains a Lua script. Find out more about scripting in ludiloom here. Name the interaction as startWalkForward.
Write the script in the interaction to make the character walk forward:
local m = this:getModel("my_character_model"); m:startMovementForward(); m:playAnimation("front",1,true);
The line 1 binds the model of the character to the variable m. The variable this is the object that contains the interaction (Character), and my_character_model is the name of the character model previously created. Line 2 tells the character model to start moving forward. Finally, line 3 starts playing the animation front in the model with speed 1 and in loop.
You find a complete description of the Lua API here. In this script, we used the functions getModel, startMovementForward- and playAnimation.
Animations come within the mesh. You can see the list of animations a mesh has in the Model Editor. Learn more about exporting models to ludiloom here.
Then, make an interaction to stop moving forward:
local m = this:getModel("my_character_model"); m:stopMovementForward(); m:stopAnimation("front");
Likewise, you have to create interactions to start/stop moving backward, strafe left and strafe right. Based on the examples and if you look at the documentation, you should be able to create the other scripts. To start/stop moving backwards, use the function startMovementBarward and stopMovementBackward. To start/stop rotating, use the functions startMovementRotateLeft, stopMovementRotateLeft, startMovementRotateRight and stopMovementRotateRight.
The interaction to jump, should be:
local m = this:getModel("my_character_model"); m:jump(); m:startAnimation("jump",1,false);
In the end, you should have something like this:
Keyboard Controls
Now it's time to implement the controls. You have to create a listener for every movement: start/stop moving forward, backward, strafe left, strafe right and jump.
To create a listener through the interface, right-click on the arena object in the tree >> new >> listener. You will see the Listener Editor window, like this:
To create a listener to start moving forward, pick the type of listener Key pressed and the key in Action button. Then, in order to choose the correct interaction, press the button more. It will open that Interaction Select window, where you can browse the Tree and pick the interaction. Find the object Character and select the interaction starWalkForward. Click the button New and that's it!
Do the same to stop moving forward. The type of listener is Key released and the interaction is stopWalkForward.
Now create the key pressed and key released listeners for the other types of movement: backwards, left strafe, right strafe and jump.
Or... Save some work and do that with scripting. Create an interaction in the arena object called installListeners with this code:
local im = InterModel.newIM(); Arena:setKeyPressedListener("\17",character,"startWalkForward",im); Arena:setKeyReleasedListener("\17",character,"stopWalkForward",im); Arena:setKeyPressedListener("\31",character,"startWalkBackward",im); Arena:setKeyReleasedListener("\31",character,"stopWalkBackward",im); Arena:setKeyPressedListener("\30",character,"startWalkLeft",im); Arena:setKeyReleasedListener("\30",character,"stopWalkLeft",im); Arena:setKeyPressedListener("\32",character,"startWalkRight",im); Arena:setKeyReleasedListener("\32",character,"stopWalkRight",im); Arena:setKeyPressedListener("\57",character,"jump",im);
This interaction receives an object called character as parameter. Learn how to set parameters in interactions here.
Then, open the console (press F11) and execute the code:
Arena:exec("installListeners",Arena:getObjectsByName("Character")[1]:getID());
Either if you created the listeners through the interface or using scripting, you should end up with an Tree like this:
This will execute the script you just created. If all goes well, you should be able to move the character with the keys w, a, s and d, space bar to jump. Switch to play mode, and give it a go!









