49 lines
1.5 KiB
GDScript
49 lines
1.5 KiB
GDScript
extends CharacterBody3D
|
|
|
|
@export var SPEED = 3.5
|
|
@export var JUMP_VELOCITY = 4.5
|
|
|
|
@export var PlayerSprite: AnimatedSprite2D
|
|
|
|
# Get the gravity from the project settings to be synced with RigidBody 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. (currently removed)
|
|
#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.
|
|
# TODO: replace UI actions with custom gameplay actions.
|
|
var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
|
|
var direction = (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)
|
|
|
|
if direction.x < 0 and !direction.z:
|
|
PlayerSprite.animation = "left-go"
|
|
if direction.x > 0 and !direction.z:
|
|
PlayerSprite.animation = "right-go"
|
|
if direction.z < 0:
|
|
PlayerSprite.animation = "up-go"
|
|
if direction.z > 0:
|
|
PlayerSprite.animation = "down-go"
|
|
|
|
# Check if animation should play
|
|
if (velocity.x == 0) and (velocity.z == 0): # if not
|
|
PlayerSprite.frame = 0 # set to first animation frame
|
|
PlayerSprite.stop() # stop animation
|
|
else: # if yes
|
|
PlayerSprite.play() # play animation
|
|
|
|
move_and_slide() # apply changes to position
|
|
|