Files
batrix/scripts/player/player_movement.gd

43 lines
1.2 KiB
GDScript

class_name PlayerMovement
extends RefCounted
const MOVE_SPEED: float = 10
const MOVE_ACCELERATION: float = 100
const MOVE_DECELERATION: float = 50
const FALL_SPEED: float = 20
const FALL_ACCELERATION: float = 25
var move_input: Vector2
var _move_direction: Vector3
func lateral_movement(velocity: Vector3, delta: float) -> Vector3:
move_input = Input.get_vector("move_left", "move_right", "move_up", "move_down")
if move_input.length() > 0:
_move_direction = Vector3(move_input.x, 0, move_input.y).normalized().rotated(
Vector3.UP, Referencer.main_camera.rotation.y
)
var new_velocity := _move_direction * MOVE_SPEED
new_velocity.y = velocity.y
velocity = velocity.move_toward(new_velocity, MOVE_ACCELERATION * delta)
else:
var new_velocity := Vector3.ZERO
new_velocity.y = velocity.y
velocity = velocity.move_toward(new_velocity, MOVE_DECELERATION * delta)
return velocity
func vertical_movement(velocity: Vector3, delta: float, is_on_floor: bool) -> Vector3:
if not is_on_floor:
var new_velocity := velocity
new_velocity.y = -FALL_SPEED
velocity = velocity.move_toward(new_velocity, FALL_ACCELERATION * delta)
else:
velocity.y = 0
return velocity