Godot Version
4.3
Question
Hey can anyone help me with this player death + Respawn ?
Currently it does not take player input away right when player dies so you can still move around. Have tried playing around with the timers and I get a bug where the player is stuck on the player death effect. Any help is appreciated.
func player_death():
var player_death_effect_instance = player_death_effect.instantiate() as Node2D
player_death_effect_instance.global_position = global_position
get_parent().add_child(player_death_effect_instance)
death_sound.play()
set_block_signals(true)
visible = false
await get_tree().create_timer(1.4).timeout
get_tree().paused = true
await get_tree().create_timer(1.5).timeout
respawned.emit()
respawn_sound.play()
get_tree().paused = false
get_tree().reload_current_scene()
visible = true
set_block_signals(false)
HealthManager.increase_health(3)
untitledgamestudios:
set_block_signals(true)
Is this line supposed to stop the player movement? Is your player movement heavily reliant on signals emitting?
1 Like
Depending on the way you collect your input, you will want to use one or more (or all ) of these:
set_process(false)
set_physics_process(false)
set_process_input(false)
set_process_unhandled_input(false)
1 Like
This is how my player is programmed:
@onready var muzzle : Marker2D = $Muzzle
#@onready var hit_animation_player = $HitAnimationPlayer
@onready var animated_sprite_2d = $AnimatedSprite2D
@onready var jump_sound = $Sounds/Jump
@onready var run_sound = $Sounds/Run
@onready var death_sound = $Sounds/Death
@onready var damage_sound = $Sounds/Damage
@onready var respawn_sound = $Sounds/Respawn
@onready var actionable_finder = $ActionableFinder
const GRAVITY = 1000
const SPEED = 1000
@export var speed : int = SPEED
@export var max_horizontal_speed: int = 300
@export var slow_down_speed : int = 1500
@export var jump : int = -300
@export var jump_horizontal_speed : int = 1000
@export var max_jump_horizontal_speed : int = 300
@export var jump_height : float = -250
#change value below for multiple jumps
@export var jump_count : int = 1
@export var max_jump_count : int = 2
@export var character_body_2d : CharacterBody2D
@export var knockbackPower : int = 500
enum State {Idle, Run, Jump, Shoot}
var current_state : State
var muzzle_position
var current_jump_count : int
var character_sprite : Sprite2D
var coyote_jump : bool
func _physics_process(delta: float):
player_falling(delta)
player_idle(delta)
player_run(delta)
player_jump(delta)
player_muzzle_position()
player_shoting(delta)
move_and_slide()
player_animations()
#print("State:", State.keys()[current_state])
func player_falling(delta: float):
if !is_on_floor():
velocity.y += GRAVITY * delta
func player_idle(delta: float):
if is_on_floor():
current_state = State.Idle
func player_run(delta: float):
var direction = Input_movement()
if direction:
velocity.x += direction * speed * delta
velocity.x = clamp(velocity.x, -max_horizontal_speed, max_horizontal_speed)
else:
velocity.x = move_toward(velocity.x, 0, slow_down_speed * delta)
if direction != 0 and HealthManager.current_health != 0:
current_state = State.Run
run_sound.play()
animated_sprite_2d.flip_h = false if direction > 0 else true
if direction == 0:
run_sound.stop()
func player_jump(delta: float):
var jump_input : bool = Input.is_action_just_pressed("jump")
if is_on_floor() and jump_input:
current_jump_count = 0
velocity.y = jump_height
coyote_jump = false
current_jump_count += 1
current_state = State.Jump
jump_sound.play()
if coyote_jump:
character_body_2d.velocity.y = jump_height
coyote_jump = false
current_jump_count += 1
# multiple jumps
if !is_on_floor() and jump_input and current_jump_count != max_jump_count:
character_body_2d.velocity.y = jump_height
current_jump_count += 1
current_state = State.Jump
jump_sound.play()
if !is_on_floor() and current_state == State.Jump:
var direction = Input_movement()
velocity.x += direction * jump_horizontal_speed * delta
velocity.x = clamp(velocity.x, -max_jump_horizontal_speed, max_jump_horizontal_speed)
func player_shoting(delta:float):
var direction = Input_movement()
if direction != 0 and Input.is_action_just_pressed("shoot"):
var bullet_instance = bullet.instantiate() as Node2D
bullet_instance.direction = direction
bullet_instance.global_position = muzzle.global_position
get_parent().add_child(bullet_instance)
current_state = State.Shoot
func player_muzzle_position():
var direction = Input_movement()
if direction > 0:
muzzle.position.x = muzzle_position.x
elif direction < 0 :
muzzle_position.x = -muzzle_position.x
func player_animations():
if current_state == State.Idle:
animated_sprite_2d.play("idle")
elif current_state == State.Run and animated_sprite_2d.animation != "Run_shoot":
animated_sprite_2d.play("Run")
elif current_state == State.Jump:
animated_sprite_2d.play("Jump")
elif current_state == State.Shoot:
animated_sprite_2d.play("Run_shoot")
Thank you this was the solution! Ended up like this:
func player_death():
var player_death_effect_instance = player_death_effect.instantiate() as Node2D
player_death_effect_instance.global_position = global_position
get_parent().add_child(player_death_effect_instance)
death_sound.play()
print_debug("played death")
visible = false
set_process_input(false)
set_process_unhandled_input(false)
set_process(false)
set_physics_process(false)
await get_tree().create_timer(2).timeout
get_tree().paused = true
print_debug("respawn")
respawned.emit()
respawn_sound.play()
get_tree().paused = false
get_tree().reload_current_scene()
visible = true
set_process_input(true)
set_process_unhandled_input(true)
set_process(true)
set_physics_process(true)
HealthManager.increase_health(3)
1 Like