Debian Codegraph @

modules/gdscript/editor/script_templates/CharacterBody3D/basic_movement.gd

raw
1 # meta-description: Classic movement for gravity games (FPS, TPS, ...)
2
3 extends _BASE_
4
5
6 const SPEED = 5.0
7 const JUMP_VELOCITY = 4.5
8
9
10 func _physics_process(delta: float) -> void:
11 # Add the gravity.
12 if not is_on_floor():
13 velocity += get_gravity() * delta
14
15 # Handle jump.
16 if Input.is_action_just_pressed("ui_accept") and is_on_floor():
17 velocity.y = JUMP_VELOCITY
18
19 # Get the input direction and handle the movement/deceleration.
20 # As good practice, you should replace UI actions with custom gameplay actions.
21 var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
22 var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
23 if direction:
24 velocity.x = direction.x * SPEED
25 velocity.z = direction.z * SPEED
26 else:
27 velocity.x = move_toward(velocity.x, 0, SPEED)
28 velocity.z = move_toward(velocity.z, 0, SPEED)
29
30 move_and_slide()