Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

Escena del jugador y acciones de entrada

En las próximas dos lecciones, diseñaremos la escena del jugador, registraremos acciones de entrada personalizadas y codificaremos el movimiento del jugador. Al final, tendrás un personaje jugable que se mueve en ocho direcciones.

Create a new scene by going to the Scene menu in the top-left and clicking New Scene.

image0

Create a CharacterBody3D node as the root

../../_images/add_character_body3D.webp

Name the CharacterBody3D to Player. Character bodies are complementary to the area and rigid bodies used in the 2D game tutorial. Like rigid bodies, they can move and collide with the environment, but instead of being controlled by the physics engine, you dictate their movement. You will see how we use the node's unique features when we code the jump and squash mechanics.

Ver también

Para obtener más información sobre los diferentes tipos de nodos de física, consulta Introducción a la física.

Por ahora, vamos a crear un equipo básico para el modelo 3D de nuestro personaje. Esto nos permitirá rotar el modelo más tarde mediante código mientras reproduce una animación.

Add a Node3D node as a child of Player and name it Pivot

../../_images/adding_node3D.webp

Then, in the FileSystem dock, expand the art/ folder by double-clicking it and drag and drop player.glb onto Pivot.

|image1|

This should instantiate the model as a child of Pivot. You can rename it to Character.

image2

Nota

The .glb files contain 3D scene data based on the open source GLTF 2.0 specification. They're a modern and powerful alternative to a proprietary format like FBX, which Godot also supports. To produce these files, we designed the model in Blender 3D and exported it to GLTF.

As with all kinds of physics nodes, we need a collision shape for our character to collide with the environment. Select the Player node again and add a child node CollisionShape3D. In the Inspector, on the Shape property, add a new SphereShape3D.

../../_images/add_capsuleshape3d.webp

The sphere's wireframe appears below the character.

|image3|

Será la forma que use el motor de física para colisionar con el entorno, por lo que queremos que se ajuste mejor al modelo 3D. Redúzcalo un poco arrastrando el punto naranja en la ventana gráfica. Mi esfera tiene un radio de unos 0,8 metros.

Luego, mueva la forma hacia arriba para que su parte inferior se alinee aproximadamente con el plano de la cuadrícula.

image4

You can toggle the model's visibility by clicking the eye icon next to the Character or the Pivot nodes.

image5

Save the scene as player.tscn

Con los nodos listos, casi podemos comenzar a codificar. Pero primero, necesitamos definir algunas acciones de entrada.

Creando acciones de entrada

Para mover el personaje, escucharemos la entrada del jugador, como presionar las teclas de flecha. En Godot, aunque podríamos escribir todas las combinaciones de teclas en código, hay un poderoso sistema que te permite asignar una etiqueta a un conjunto de teclas y botones. Esto simplifica nuestros scripts y los hace más legibles.

This system is the Input Map. To access its editor, head to the Project menu and select Project Settings.

image6

En la parte superior, hay varias pestañas. Haga clic en Mapa de entrada. Esta ventana le permite agregar nuevas acciones en la parte superior; son tus etiquetas. En la parte inferior, puede vincular teclas a estas acciones.

image7

Los proyectos de Godot vienen con algunas acciones predefinidas diseñadas para el diseño de la interfaz de usuario, que podríamos usar aquí. Pero estamos definiendo el nuestro para admitir mandos.

Vamos a nombrar nuestras acciones move_left, move_right, move_forward, move_back y jump.

Para agregar una acción, escriba su nombre en la barra en la parte superior y presione Entrar.

image8

Create the following five actions:

image9

To bind a key or button to an action, click the "+" button to its right. Do this for move_left. Press the left arrow key and click OK.

../../_images/left_inputmap.webp

Bind also the A key, onto the action move_left.

image12

Let's now add support for a gamepad's left joystick. Click the "+" button again but this time, select Manual Selection -> Joypad Axes.

../../_images/left_inputmap.webp

Select the negative X axis of the left joystick.

../../_images/left_joystick_select.webp

Leave the other values as default and press OK

Nota

If you want controllers to have different input actions, you should use the Devices option in Additional Options. Device 0 corresponds to the first plugged gamepad, Device 1 corresponds to the second plugged gamepad, and so on.

Do the same for the other input actions. For example, bind the right arrow, D, and the left joystick's positive axis to move_right. After binding all keys, your interface should look like this.

image15

The final action to set up is the jump action. Bind the Space key and the gamepad's A button.

image16

Tu acción de entrada jump debería verse así.

image18

Esas son todas las acciones necesarios por el juego. Puede usar este menú para designar cualquier grupo de tecla o botón en sus proyectos.

En el siguiente parte, vamos a programar y verificar el movimiento de jugador.