A Minecraft style clone in Godot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

47 lines
1.4 KiB

class_name Player
extends CharacterBody3D
@export var jump_velocity: float = 4.5
@export var mouse_sensitivity_horizontal: float = 0.003
@export var mouse_sensitivity_vertical: float = 0.003
@export var speed: float = 5.0
@onready var camera: Camera3D = $Camera3D
func _physics_process(delta: float) -> void:
apply_gravity(delta)
handle_jump()
handle_movement()
move_and_slide()
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseMotion:
handle_mouse_movement(event)
func apply_gravity(delta: float) -> void:
if is_on_floor(): return
velocity += get_gravity() * delta
func handle_jump() -> void:
if not Input.is_action_just_pressed("jump") or not is_on_floor(): return
velocity.y = jump_velocity
func handle_mouse_movement(event: InputEvent) -> void:
rotation.y = rotation.y - event.relative.x * mouse_sensitivity_horizontal
camera.rotation.x = camera.rotation.x - event.relative.y * mouse_sensitivity_vertical
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-90), deg_to_rad(90))
func handle_movement() -> void:
var input_dir: Vector2 = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
var direction: Vector3 = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * speed
velocity.z = direction.z * speed
else:
velocity.x = move_toward(velocity.x, 0, speed)
velocity.z = move_toward(velocity.z, 0, speed)