Adds Player controller

This commit is contained in:
Michel 2025-01-31 22:54:00 +01:00
commit ab3890896d
35 changed files with 26403 additions and 0 deletions

44
scripts/player/player.gd Normal file
View file

@ -0,0 +1,44 @@
extends CharacterBody3D
@onready var animationtree: AnimationTree = $Camera3D/Weapon/AnimationPlayer/AnimationTree
const SPEED: float = 5.0
const FALLOFF_SPEED: float = 0.5
const JUMP_VELOCITY: float = 4.5
var lastDirection = Vector2(0,0)
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta
# Handle Jump.
if Input.is_action_just_pressed("player_jump") 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("player_right", "player_left", "player_down", "player_up")
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
var falloffSpeed = FALLOFF_SPEED
if not is_on_floor():
falloffSpeed = 0.02;
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, falloffSpeed)
velocity.z = move_toward(velocity.z, 0, falloffSpeed)
move_and_slide()
lastDirection.x = move_toward(lastDirection.x, input_dir.x, 0.05);
lastDirection.y = move_toward(lastDirection.y, input_dir.y, 0.05);
animationtree.set('parameters/Movement/blend_position', lastDirection);