]> Untitled Git - mushroom-game.git/blob - player/player.gd
Squashed commit of the following:
[mushroom-game.git] / player / player.gd
1 extends CharacterBody3D
2
3
4 const SPEED = 5.0
5 const CAMERA_ROTATE_RATE = PI / 6
6
7
8 # rotate camera
9 func _unhandled_input(event: InputEvent) -> void:
10         if event.is_action_pressed("camera-rotate-left"):
11                 $cameraPivot.rotate_y(CAMERA_ROTATE_RATE)
12         elif event.is_action_pressed("camera-rotate-right"):
13                 $cameraPivot.rotate_y(-CAMERA_ROTATE_RATE)
14
15
16 func _physics_process(delta: float) -> void:
17         # Add the gravity.
18         if not is_on_floor():
19                 velocity += get_gravity() * delta
20
21         # Get the input direction and handle the movement/deceleration.
22         var input_dir := Input.get_vector("player-turn-left", "player-turn-right", "player-forward", "player-backward")
23         var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
24         direction = direction.rotated(Vector3.UP, $cameraPivot.rotation.y)
25         
26         if direction:
27                 velocity.x = direction.x * SPEED
28                 velocity.z = direction.z * SPEED
29                 
30                 # rotate the character to face movement direction
31                 $player.rotation.y = lerp_angle($player.rotation.y, atan2(-velocity.x, -velocity.z), 0.15)
32         else:
33                 velocity.x = move_toward(velocity.x, 0, SPEED)
34                 velocity.z = move_toward(velocity.z, 0, SPEED)
35
36         move_and_slide()