Improving our Character
This content is not available in your language yet.
Changing the Controls
Section titled Changing the ControlsFirst, let’s fix our control scheme. If you want, you can leave your controls as the arrowkeys, or make them whatever you like. But it’s a good idea to understand how to change them. Thankfully, Godot has a robust Input System.
-
Let’s open our input settings by going to Project > Project Settings… > Input Map from the top left menu. Using the ‘Add new Action’ field, add five actions. ‘Forward’, ‘Backward’, ‘Left’, ‘Right’, and ‘Jump’
-
Using the ’+’ button next to each direction, simple press the key you want to assign for each. Let’s do ‘W’, ‘S’, ‘A’, ‘D’, and ‘Space’ respectively. Makes sure you’re not holding shift or control, otherwise these will become part of the input.
Great! But if you hit play now, you’ll notice nothing has changed. This is because we need to tell our controller script to listen for these inputs.
Let’s go back to our character controller script and change out the default inputs, to our new ones.
-
We’ll change
var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")to
var input_dir = Input.get_vector("Left", "Right", "Forward", "Backward") -
and we’ll change
if Input.is_action_just_pressed("ui_accept") and is_on_floor():to
if Input.is_action_just_pressed("Jump") and is_on_floor(): -
Great! Run your game and test these changes, make sure every movement input, and jump works, and double-check any that don’t.
In the future, if you want to change the controls, all you’ll have to do is change the assigned inputs under the Input Map.
Fixing the Camera
Section titled Fixing the CameraThe final thing we need to do to have a fully functional Character controller, is to get our camera moving!
If we were working on a large long-term project we would likely want to make this its own script, but let’s keep things simple for now and just add it to our character controller script.
-
Let’s open our character controller script by clicking on the CharacterBody3D node and opening the script tab.
-
Below the existing variable declarations, let’s add a new line.
@export var camera:Camera3DThe export tag allows us to assign variables/nodes from within the inspector. If you’ve used unity, this works the same as [SerializeField] We’ll take another look at this soon.
-
We’ll need two variables to keep track of our rotation, and control our mouse sensitivity, add these underneath the other variable declarations.
var camera_rotation = Vector2(0, 0)var mouse_sensitivity := 0.005 -
You’ve probably noticed by now, that when launching the game, the mouse stays visible, when most games ‘capture’ the mouse. Thankfully this is an easy addition using godots Input system.
func _ready() -> void:Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)The _ready() function is called by godot when the object is first instantiated, so its a great place to do things like this.
-
Trying running the game now, you’ll notice your mouse is gone! To quit you can alt+tab out, and press stop in the top right menu.
But let’s make sure we can get our mouse back.
func _input(event) -> void:# If escape is pressed reveal the mouseif event.is_action_pressed("ui_cancel"):Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)The _input(event) function is called by godot whenever input is detected, this let’s us check what was pressed. In this case we’ll be checking for ‘ui_cancel’. You may notice that we didn’t create any input called ‘ui_cancel’ This is another one of Godot’s default inputs, and is bound to ‘esc’
Great! Let’s try running our game again, using Esc to get our mouse back.
-
Because this function checks for input, we can use it to tell if our mouse has moved, as Godot considers this input.
Let’s add some more to the _input(event) function.
if event is InputEventMouseMotion:# Get how much the mouse has moved and pass it on to the camera_look functionvar relative_position = event.relative * mouse_sensitivitycamera_look(relative_position) -
meaning the full function would look like this:
func _input(event) -> void:# If escape is pressed reveal the mouseif event.is_action_pressed("ui_cancel"):Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)# Get the mouse movementif event is InputEventMouseMotion:# Get how much the mouse has moved and pass it on to the camera_look functionvar relative_position = event.relative * mouse_sensitivitycamera_look(relative_position)The event.relative variable tracks the difference in the position of the mouse from the last frame, which tells us how much the mouse has moved. We then multiply this by our sensitivity value.
-
You’ll notice we’ve called a method called camera_look() that doesn’t exist, we’ll create this now.
func camera_look(movement: Vector2) -> void:# Add how much the camera has moved to the camera rotationcamera_rotation += movement# Stop the player from making the camera go upside down by looking too far up and downcamera_rotation.y = clamp(camera_rotation.y, deg_to_rad(-90), deg_to_rad(90))# Reset the transform basistransform.basis = Basis()main_camera.transform.basis = Basis()#The player and camera needs to rotate on the x and only the camera should rotate on the yrotate_object_local(Vector3.UP, -camera_rotation.x)main_camera.rotate_object_local(Vector3.RIGHT, -camera_rotation.y)This looks complicated, but really what’s happening is:
- We keep track of the total amount the player is rotated by
- We ensure that the Y rotation can’t go over a certain value, to prevent us doing flips with the camera (In this case the value has to stay between -90 and 90 degrees)
- We zero the basis’ of the camera and player, to making adding rotation easy
- We rotate the Player on the X-axis, and the Camera on the Y-axis. These are separated. as if we simply rotated our player, as we looked up, our player would lean backwards until it falls over.
-
Finally, we need to revisit that @export statement. make sure you save your script, then go back to our Player scene. Click on the CharacterBody3D and you’ll notice that over on the right in the inspector, is a slot called ‘Camera’ Just drag and drop our Character’s camera from the left panel, over into the inspector, and our camera is assigned!
When we play our game, we can properly walk and look around! Now it’s time to get some gameplay in our game!
Some things to try
Section titled Some things to try- Try increasing and decreasing the Camera sensitivity, change it to a value that feels best for you.
- See if you can change the button we use to get our mouse back to something else