Godot 4.5
Question
I’m a newb and I suck at coding, surprise surprise, so I’m not sure about how I should handle killing my player character. Whenever I go into my player script and try to disable input via Godot commands (set_process_input = false, for example) nothing happens. If I set a condition to stop input handling (eg: if is_dead = true, then pass) then it also stops tracking the player’s velocity; this would work okay if I had a death animation in place, but I’m not sure I really want that in the traditional sense. My end goal (eventually) is to have the player touch an enemy, die, and have them get launched in a random direction; the idea is that the momentum of the player before they touched the enemy will remain or something like that.
Anyway, that’s for later. For now, I just want it so whatever direction the player was moving in is retained BUT the player cannot input anymore. How do I make it so the physics process continues where the player left off (ex: they were holding left and died on an enemy) while also disabling player input?
Thanks.
Code:
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
@onready var dude: Sprite2D = $Dude
@onready var hitbox1: CollisionShape2D = $hitbox1
@onready var hitbox2: CollisionShape2D = $hitbox2
var can_receive_input := true
#func die():
#Lifedata.health = 0
#Lifedata.is_alive = false
#func hit():
#Lifedata.health -= 1
#if Lifedata.health <=0:
#die()
func _physics_process(delta: float) → void:
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("spacebar") and is_on_floor(): #and Lifedata.is_alive:
velocity.y = JUMP_VELOCITY
else:
pass
# Get the input direction (-1, 0, 1)
if Lifedata.is_alive:
var direction := Input.get_axis("left", "right")
if direction > 0 and hitbox1 and hitbox2:
dude.flip_h = false
hitbox1.position.x = -55.0
hitbox2.position.x = -23.5
if direction < 0 and hitbox1 and hitbox2:
dude.flip_h = true
hitbox1.position.x = -17.5
hitbox2.position.x = -49
# handle movement/deceleration
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
else:
pass
For context, the enemy code is set so when the player touches the enemy, the player hitboxes are queue_free’d and it plays a sound. A timer node starts when the player touches the enemy. After 2-3 seconds, the timer stops and the player is reset; all hitboxes are un queue_free’d, health is restored, and the scene is reloaded.