]> Untitled Git - knight.git/blob - player/player.gd
Initial skin, player, test
[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
9
10
11 func _physics_process(delta: float) -> void:
12         # Add the gravity.
13         if not is_on_floor():
14                 velocity += get_gravity() * delta
15
16         # Handle jump.
17         if Input.is_action_just_pressed("ui_accept") and is_on_floor():
18                 velocity.y = JUMP_VELOCITY
19
20         # Get the input direction and handle the movement/deceleration.
21         # As good practice, you should replace UI actions with custom gameplay actions.
22         var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
23         var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
24         if direction:
25                 velocity.x = direction.x * SPEED
26                 velocity.z = -direction.z * SPEED
27         else:
28                 velocity.x = move_toward(velocity.x, 0, SPEED)
29                 velocity.z = move_toward(velocity.z, 0, SPEED)
30
31         move_and_slide()
32
33         if is_on_floor():
34                 _skin.forward(velocity.length())
35                 _skin.global_rotation.y = Vector3.FORWARD.signed_angle_to(direction, Vector3.UP)
36         else:
37                 _skin.fall()