57 lines
1.6 KiB
GDScript
57 lines
1.6 KiB
GDScript
extends CharacterBody3D
|
|
|
|
const SPEED = 2.5
|
|
const JUMP_VELOCITY = 4.5
|
|
|
|
var walk_animation_time = 0
|
|
|
|
@onready var game = get_parent()
|
|
|
|
func _ready() -> void:
|
|
$DreamerBody.look_at(game.get_node("PlayerCamera").global_position)
|
|
$DreamerBody.rotation.x = 0
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if not is_on_floor():
|
|
velocity += get_gravity() * delta
|
|
|
|
if Input.is_action_pressed("jump") and is_on_floor():
|
|
velocity.y = JUMP_VELOCITY
|
|
$DreamerBody/Animator.set("parameters/jump/request", AnimationNodeOneShot.ONE_SHOT_REQUEST_FIRE)
|
|
|
|
var input_dir := Input.get_vector("move_left", "move_right", "move_up", "move_down")
|
|
var direction := (transform.basis * Vector3(input_dir.y + input_dir.x, 0, input_dir.y - input_dir.x)).normalized()
|
|
if direction:
|
|
velocity.x = direction.x * SPEED
|
|
velocity.z = direction.z * SPEED
|
|
|
|
$DreamerBody/Animator.set("parameters/walk/blend_amount", abs(direction.length()))
|
|
else:
|
|
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
|
velocity.z = move_toward(velocity.z, 0, SPEED)
|
|
|
|
$DreamerBody/Animator.set("parameters/walk/blend_amount", 0)
|
|
|
|
if direction and is_on_floor():
|
|
walk_animation_time += delta * 25
|
|
|
|
$DreamerBody.position.y = sin(walk_animation_time + 1) / 8
|
|
$DreamerBody.rotation.z = sin(walk_animation_time / 2) / 16
|
|
else:
|
|
walk_animation_time = 0
|
|
|
|
$DreamerBody.rotation.z /= 1 + (delta * 2.5)
|
|
|
|
$DreamerBody.position.y /= 1 + (delta * 12)
|
|
|
|
if is_on_floor():
|
|
$DreamerBody/Animator.set("parameters/fall/blend_amount", 0)
|
|
else:
|
|
$DreamerBody/Animator.set("parameters/fall/blend_amount", 1)
|
|
|
|
move_and_slide()
|
|
|
|
|
|
$Shadow.global_position.y = $Floorcast.get_collision_point().y + 0.01
|
|
|