godot_projects/puzzle/player.gd

43 lines
1.2 KiB
GDScript

extends CharacterBody3D
@export var camera : Camera3D;
const SPEED = 20
const JUMP_VELOCITY = 9
# Get the gravity from the project settings to be synced with RigidDynamicBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta
# Handle Jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
# calculate the direction relative to the camera
var camera_dir = camera.get_global_transform().basis.z
camera_dir.y = 0
camera_dir = camera_dir.normalized()
var move_dir = camera_dir * input_dir.y + camera.get_global_transform().basis.x * input_dir.x
move_dir.y = 0
# calculate the speed based on the input direction
if move_dir:
velocity.x = move_dir.x * SPEED
velocity.z = move_dir.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, 1)
velocity.z = move_toward(velocity.z, 0, 1)
move_and_slide()