]> Untitled Git - knight.git/blob - player/player.gd
Getting the look and feel right
[knight.git] / player / player.gd
1 extends CharacterBody3D
2
3
4 @onready var _skin: Node3D = $skin
5
6
7 const SPEED = 5.0
8 const JUMP_VELOCITY = 4.5 * 3
9 const FALL_SPEED = 2.0
10
11
12 var _last_movement_direction := Vector3.FORWARD
13
14
15 func _input(event: InputEvent) -> void:
16         if event.is_action_released("ui_accept"):
17                 velocity.y = 0
18
19
20 func _physics_process(delta: float) -> void:
21         # Add the gravity.
22         if not is_on_floor():
23                 velocity += get_gravity() * FALL_SPEED * delta
24
25         # Handle jump.
26         if Input.is_action_just_pressed("ui_accept") and is_on_floor():
27                 velocity.y = JUMP_VELOCITY
28
29         # Get the input direction and handle the movement/deceleration.
30         # As good practice, you should replace UI actions with custom gameplay actions.
31         var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
32         var direction := -(transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
33         if direction:
34                 velocity.x = direction.x * SPEED
35                 velocity.z = direction.z * SPEED
36                 _last_movement_direction = direction
37         else:
38                 velocity.x = 0
39                 velocity.z = 0
40
41         move_and_slide()
42
43         if is_on_floor():
44                 _skin.forward(velocity.length())
45                 _skin.global_rotation.y = Vector3.BACK.signed_angle_to(_last_movement_direction, Vector3.UP)
46         else:
47                 _skin.fall()