You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.4 KiB
GDScript
49 lines
1.4 KiB
GDScript
extends Node3D
|
|
class_name Move
|
|
|
|
const SPEED = 1.5
|
|
const JUMP_VELOCITY = 3
|
|
|
|
@onready var character = (get_owner() as Character)
|
|
@onready var status = (%Status as Status)
|
|
# Get the gravity from the project settings to be synced with RigidBody nodes.
|
|
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
|
|
|
|
func _physics_process(delta):
|
|
update_on_floor()
|
|
update_gravity(delta)
|
|
update_jump()
|
|
update_move()
|
|
|
|
character.move_and_slide()
|
|
|
|
func update_on_floor():
|
|
status.is_on_floor = character.is_on_floor()
|
|
if status.is_on_floor:
|
|
status.is_jumped = false
|
|
|
|
func update_gravity(delta):
|
|
if not status.is_on_floor:
|
|
character.velocity.y -= gravity * delta
|
|
status.speed_y = character.velocity.y
|
|
|
|
func update_jump():
|
|
if Input.is_action_just_pressed("ui_accept") and status.is_on_floor:
|
|
character.velocity.y = JUMP_VELOCITY
|
|
status.is_jumped = true
|
|
status.trigger_jump = true
|
|
|
|
func update_move():
|
|
var input_dir = status.move_dir
|
|
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
|
|
if direction:
|
|
character.velocity.x = direction.x * SPEED
|
|
character.velocity.z = direction.z * SPEED
|
|
else:
|
|
character.velocity.x = move_toward(character.velocity.x, 0, SPEED)
|
|
character.velocity.z = move_toward(character.velocity.z, 0, SPEED)
|
|
|
|
status.speed_xz = Vector2(character.velocity.x,character.velocity.z).length()
|
|
if status.is_free_turn and direction.x != 0:
|
|
status.is_right = direction.x > 0
|