add: smooth move

This commit is contained in:
aurinex
2025-08-08 16:36:54 +05:00
parent b7ccc4e3d3
commit 5145bdfa9e
6 changed files with 137 additions and 47 deletions

View File

@ -5,8 +5,10 @@ class_name Character
@export var normal_speed: float = 6.0
@export var sprint_speed: float = 10.0
@export var jump_velocity: float = 10.0
@export var gravity_scale: float = 1.0 # Позволяет регулировать гравитацию
@export var gravity_scale: float = 1.0
@export var rotation_speed: float = 10.0
@export var acceleration: float = 10.0 # Сила ускорения
@export var deceleration: float = 8.0 # Сила замедления
@export_group("References")
@export var body: Node3D = null
@ -14,24 +16,28 @@ class_name Character
@export var respawn_point: Vector3 = Vector3(0, 5, 0)
var _current_speed: float
var _target_velocity: Vector3 = Vector3.ZERO # Целевая скорость
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
var _has_input: bool = false
var _can_sprint: bool = true
func _ready():
$SpringArmOffset/SpringArm3D/Camera3D.current = false
func _physics_process(delta):
var current_scene = get_tree().get_current_scene()
func _physics_process(delta):
# Обработка гравитации
if not is_on_floor():
velocity.y -= gravity * delta
if is_on_floor():
if Input.is_action_just_pressed("jump"):
velocity.y = jump_velocity
_can_sprint = false # В воздухе нельзя ускоряться
else:
velocity.y -= gravity * delta
_can_sprint = true # На земле можно ускоряться
_move()
# Обработка прыжка
if is_on_floor() and Input.is_action_just_pressed("jump"):
velocity.y = jump_velocity
_can_sprint = false # При прыжке сразу отключаем спринт
_move(delta)
move_and_slide()
if body and body.has_method("animate"):
@ -40,13 +46,9 @@ func _physics_process(delta):
func _process(_delta):
_check_fall_and_respawn()
func freeze():
velocity.x = 0
velocity.z = 0
_current_speed = 0
func _move() -> void:
func _move(delta: float) -> void:
var input_direction = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
_has_input = input_direction.length() > 0.1
# Получаем направление движения относительно камеры
var camera_basis = $SpringArmOffset/SpringArm3D/Camera3D.global_transform.basis
@ -55,23 +57,33 @@ func _move() -> void:
_update_speed()
if direction:
velocity.x = direction.x * _current_speed
velocity.z = direction.z * _current_speed
# Устанавливаем целевую скорость с учетом направления
_target_velocity.x = direction.x * _current_speed
_target_velocity.z = direction.z * _current_speed
# Плавно изменяем скорость в сторону целевой
velocity.x = lerp(velocity.x, _target_velocity.x, acceleration * delta)
velocity.z = lerp(velocity.z, _target_velocity.z, acceleration * delta)
# Поворачиваем тело персонажа в направлении движения
if velocity.length() > 0.1: # Только если есть значительное движение
var target_rotation = atan2(velocity.x, velocity.z)
body.rotation.y = lerp_angle(body.rotation.y, target_rotation, rotation_speed * get_process_delta_time())
if velocity.length() > 0.1:
var target_rotation = atan2(_target_velocity.x, _target_velocity.z)
body.rotation.y = lerp_angle(body.rotation.y, target_rotation, rotation_speed * delta)
else:
velocity.x = move_toward(velocity.x, 0, _current_speed)
velocity.z = move_toward(velocity.z, 0, _current_speed)
# Если нет ввода, плавно уменьшаем скорость
_target_velocity = Vector3.ZERO
velocity.x = lerp(velocity.x, 0.0, deceleration * delta)
velocity.z = lerp(velocity.z, 0.0, deceleration * delta)
func is_running() -> bool:
return Input.is_action_pressed("shift") and _current_speed == sprint_speed
func _update_speed() -> void:
_current_speed = sprint_speed if Input.is_action_pressed("shift") else normal_speed
# Ускорение работает только когда персонаж на земле
if _can_sprint and Input.is_action_pressed("shift"):
_current_speed = sprint_speed
else:
_current_speed = normal_speed
func _check_fall_and_respawn():
if global_transform.origin.y < -15.0:
@ -80,3 +92,5 @@ func _check_fall_and_respawn():
func _respawn():
global_transform.origin = respawn_point
velocity = Vector3.ZERO
_target_velocity = Vector3.ZERO
_can_sprint = true