79 lines
1.8 KiB
GDScript
79 lines
1.8 KiB
GDScript
extends CharacterBody3D
|
|
|
|
|
|
const SPEED = 200.0
|
|
const JUMP_VELOCITY = 10.0
|
|
@onready var animator = get_node("gobot/AnimationPlayer") as AnimationPlayer
|
|
|
|
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
|
|
@export var view : Node3D
|
|
@export var timer : Timer
|
|
var movement_velocity : Vector3
|
|
var rotation_direction : float
|
|
var can_jump := true
|
|
var can_timeout := true
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
handle_input(delta)
|
|
apply_gravity(delta)
|
|
jump()
|
|
handle_animations()
|
|
|
|
if is_on_floor():
|
|
can_jump = true
|
|
else:
|
|
if can_timeout:
|
|
timer.start()
|
|
can_timeout = false
|
|
|
|
var applied_velocity : Vector3
|
|
applied_velocity = velocity.lerp(movement_velocity, delta * 10)
|
|
applied_velocity.y = -gravity
|
|
|
|
velocity = applied_velocity
|
|
|
|
move_and_slide()
|
|
|
|
if Vector2(velocity.z, velocity.x).length() > 0:
|
|
rotation_direction = Vector2(velocity.z, velocity.x).angle()
|
|
|
|
rotation.y = lerp_angle(rotation.y, rotation_direction, delta * 10)
|
|
|
|
func handle_input(delta):
|
|
var input := Vector3.ZERO
|
|
input.x = Input.get_axis("move_left", "move_right")
|
|
input.z = Input.get_axis("move_backward", "move_foward")
|
|
|
|
input = input.rotated(Vector3.UP, view.rotation.y).normalized()
|
|
|
|
velocity = input * SPEED * delta
|
|
|
|
func handle_animations():
|
|
if is_on_floor():
|
|
if abs(velocity.x) > 1 or abs(velocity.z) > 1:
|
|
animator.play("Run", 0.3)
|
|
else:
|
|
animator.play("Idle", 0.3)
|
|
else:
|
|
animator.play("Jump", 0.3)
|
|
|
|
if !is_on_floor() and gravity > 2:
|
|
animator.play("Fall", 0.3)
|
|
|
|
func apply_gravity(delta):
|
|
if not is_on_floor():
|
|
gravity += 25 * delta
|
|
|
|
func jump():
|
|
if Input.is_action_just_pressed("move_jump") and can_jump:
|
|
gravity = -JUMP_VELOCITY
|
|
can_jump = false
|
|
|
|
if gravity > 0 and is_on_floor():
|
|
gravity = 0
|
|
|
|
|
|
func _on_timer_timeout() -> void:
|
|
can_jump = false
|
|
can_timeout = true
|