Onscreen buttons

Godot Version

godot engine 4 android app

Question

Hi! I’m a complete beginner and I was trying to make a group of onscreen buttons to move my player character so I made a CanvasLayer and 3 buttons under it like this:

the problem is that the buttons don’t move with the camera so the player character can go far enough from them that they go off screen and and I don’t know how to make them follow the player
is there a way to fix this?

the code I’m using:


# Player movement variables
@export var speed = 200.0
@export var jump_force = -350.0
@export var gravity = 1500.0

# Button states
var button_left_pressed = false
var button_right_pressed = false
var button_jump_pressed = false

func _physics_process(delta):
    # Apply gravity
    if not is_on_floor():
        velocity.y += gravity * delta
    
    # Horizontal movement
    var horizontal_input = 0
    if button_left_pressed:
        horizontal_input -= 1
    if button_right_pressed:
        horizontal_input += 1
    
    velocity.x = horizontal_input * speed
    
    # Handle jump
    if button_jump_pressed and is_on_floor():
        velocity.y = jump_force
        button_jump_pressed = false
    
    move_and_slide()

# Button signal functions
func _on_button_left_button_down():
    button_left_pressed = true

func _on_button_left_button_up():
    button_left_pressed = false

func _on_button_right_button_down():
    button_right_pressed = true

func _on_button_right_button_up():
    button_right_pressed = false

func _on_button_jump_button_down():
    button_jump_pressed = true

func _on_button_jump_button_up():
    button_jump_pressed = false```

You probably don’t want the CanvasLayer (named controls in your code) to be a child of the player. You could, for instance, make the controls node its own scene. (You would then need to send the information about pressed buttons to the player scene via the Message Bus pattern or something similar.)