notSpore/3d-tutorial/3d_squash_the_creeps_starter/player.gd

40 lines
830 B
GDScript

extends CharacterBody3D
@export var speed = 14
@export var fall_acc = 75
@export var jump_impulse = 40
var target_vel = Vector3.ZERO
func _physics_process(delta: float) -> void:
# Move
var dir = Vector3.ZERO
if Input.is_action_pressed("move_left"):
dir.x -= 1
if Input.is_action_pressed("move_right"):
dir.x += 1
if Input.is_action_pressed("move_forward"):
dir.z -= 1
if Input.is_action_pressed("move_back"):
dir.z += 1
if dir != Vector3.ZERO:
dir = dir.normalized()
$Pivot.basis = Basis.looking_at(dir)
# Ground vel. (overwrite y later)
target_vel = dir * speed
# vert. vel.
target_vel.y = 0
if not is_on_floor():
target_vel.y -= fall_acc * delta
# Jump
if is_on_floor() and Input.is_action_just_pressed("jump"):
target_vel.y = jump_impulse
velocity = target_vel
move_and_slide()