SchildDerStaerke/scripts/survivor_male_b.gd

53 lines
1.8 KiB
GDScript

extends CharacterBody3D
@onready var anim_tree := $AnimationTree
@onready var anim_state = $AnimationTree.get("parameters/playback")
@onready var spring_arm: SpringArm3D = $SpringArm3D
@onready var model := $Root
var jumping := false
var mouse_sensitivity := 0.006
var rotation_speed := 24.0
var lerp_val := 0.1
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
func _input(event: InputEvent) -> void:
# Move camera
if event is InputEventMouseMotion:
spring_arm.rotation.x -= event.relative.y * mouse_sensitivity
spring_arm.rotation_degrees.x = clamp(spring_arm.rotation_degrees.x, -90.0, 30.0)
spring_arm.rotation.y -= event.relative.x * mouse_sensitivity
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
jumping = !is_on_floor()
# Get the input direction and handle the movement/deceleration.
var input = Input.get_vector("left", "right", "forward", "back")
var direction = Vector3(input.x, 0, input.y).rotated(Vector3.UP, spring_arm.rotation.y)
if direction:
if not jumping:
anim_state.travel("run")
velocity.x = lerp(velocity.x, direction.x * SPEED, lerp_val)
velocity.z = lerp(velocity.z, direction.z * SPEED, lerp_val)
else:
anim_state.travel("idle")
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
if velocity.length() > 1.0:
model.rotation.y = lerp_angle(model.rotation.y, spring_arm.rotation.y, rotation_speed * delta)
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
jumping = true
anim_tree.set("parameters/conditions/jumping", jumping)
anim_tree.set("parameters/conditions/grounded", not jumping)
move_and_slide()