Godot Version
GODOT 4.7.2
Help
Hello i have a problems with the killzone in my game , i wanted the player that if he touche an ennemis he have the slowmotion effect death and restart the level. This is the line of code i mean :
Engine.time_scale = 0.5
body.get_node("CollisionShape2D").queue_free()
timer.start()
Thanks to brackey , links: https://www.youtube.com/watch?v=LOhfqjmasi0
But i don’t know and figure out how to fix it , i can’t make the player die when he falls in the killzone , he just stays in the voids and can’t restart the game , but when i touch a ennemies and my hp drain to 0 it restart but with not the effect death because i replaced it to fix the problems i currently have . I already did make groups but it didn’t works wich it confuse me
Here is the codes
killzone.gd
extends Area2D
@onready var timer: Timer = $Timer
func _on_body_entered(body: Node2D) -> void:
if body.is_in_group("Player"):
body.meurto()
print("i fell in void")
else:
print("aaaaaaaaaahhh")
Engine.time_scale = 0.5
body.get_node("CollisionShape2D").queue_free()
timer.start()
func _on_area_entered(area: Area2D) -> void:
var en = area.get_parent()
if en.is_in_group("Ennemi"):
var dgt = en.degat()
var player = get_tree().get_first_node_in_group("Player")
player.take_damage(dgt)
func _on_timer_timeout() -> void:
Engine.time_scale = 1
get_tree().reload_current_scene()
green_slime.gd
extends Node2D
const SPEED = 20
var direction = 1
var a_fait_demi_tour = false
@onready var ray_cast_right: RayCast2D = $RayCastRight
@onready var ray_cast_left: RayCast2D = $RayCastLeft
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var ray_cast_downright: RayCast2D = $RayCastDownright
@onready var ray_cast_downleft: RayCast2D = $RayCastDownleft
@onready var degat_timer: Timer = $DegatTimer
var prend_degats = false
var vie = 10;
var maxvie = 10;
var est_mort = false
@onready var bardevieennemie: ProgressBar = $Bardevieennemie
func _process(delta: float) -> void:
bardevieennemie.setvie(vie , maxvie)
if est_mort:
return
if not ray_cast_downright.is_colliding() and not a_fait_demi_tour:
direction *= -1
animated_sprite.flip_h = direction < 0
a_fait_demi_tour = true
if not ray_cast_downleft.is_colliding() and not a_fait_demi_tour:
direction *= 1
animated_sprite.flip_h = direction > 0
a_fait_demi_tour = true
if ray_cast_downright.is_colliding() or ray_cast_downleft.is_colliding():
a_fait_demi_tour = false
if direction == 1 and not ray_cast_downright.is_colliding():
direction = -1
animated_sprite.flip_h = true
elif direction == -1 and not ray_cast_downleft.is_colliding():
direction = 1
animated_sprite.flip_h = false
if ray_cast_right.is_colliding() :
direction = -1
animated_sprite.flip_h = true
if ray_cast_left.is_colliding():
direction = 1
animated_sprite.flip_h = false
position.x += direction * delta * SPEED
if not prend_degats:
animated_sprite.play("default")
func take_damage(damage: int):
if est_mort:
return
vie -= damage
if vie <= 0:
vie = 0
est_mort = true
direction = 0
animated_sprite.play("meurt")
return
prend_degats = true
animated_sprite.play("blesse")
degat_timer.start()
func _on_hurt_box_area_entered(area: Area2D) -> void:
if area.name == "Area2D" and not est_mort :
print("okokkok")
var player = get_tree().get_first_node_in_group("Player")
if player != null:
player.take_damage(degat())
if area.name == "Epee" and not est_mort:
print("aie")
take_damage(area.degat())
print(vie)
func degat():
return 3
func _on_degat_timer_timeout() -> void:
prend_degats = false
func _on_animated_sprite_2d_animation_finished() -> void:
if animated_sprite.animation == "meurt":
queue_free()
#func _on_killzone_body_entered(body: Node2D) -> void:
# if body.is_in_group("Player") and body.vie <= 0:
# body.meurto()
player.gd
extends CharacterBody2D
var transition_cartoonesque = preload("uid://rvjao2ktxeya")
const SPEED = 130.0
const JUMP_VELOCITY = -300.0
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
var epee: Area2D
@onready var degattimer: Timer = $Degattimer
var dash = 600
var dashing : bool = false
var acceleration = 0.2
@onready var dash_timer: Timer = $DashTimer
@onready var encore_dash_timer: Timer = $encoreDashTimer
var peut_dash = true
var vie = 5;
var maxvie = 10;
@onready var bardevieplayer: ProgressBar = $Bardevieplayer
var prend_degats = false
var attaque = false
var mort = false
@onready var actualiserbardevieplayer = get_tree().get_first_node_in_group("Interface")
var stop = false
func _ready() -> void:
pass
func take_damage(damage : int):
if mort:
return
vie -= damage
prend_degats = true
animated_sprite.play("aie")
degattimer.start()
if vie <= 0:
vie = 0
print("je suis 0 vie ")
mort = true
get_tree().current_scene.add_child(transition_cartoonesque)
return
func _physics_process(delta: float) -> void:
bardevieplayer.setvie(vie , maxvie)
actualiserbardevieplayer.barredevieenlive(vie, maxvie)
if mort:
print("lalalal")
respawn()
return
if epee != null and Input.is_action_just_pressed("attaque_epee") :
print("jai une epee")
epee.attaque()
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction := Input.get_axis("move_left", "move_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if direction > 0:
animated_sprite.flip_h = false
if epee != null:
epee.rotation_degrees = 0
elif direction < 0:
animated_sprite.flip_h = true
if epee != null:
epee.rotation_degrees = -100
if Input.is_action_just_pressed("dash") and peut_dash:
dashing = true
peut_dash = false
dash_timer.start()
encore_dash_timer.start()
if dashing:
velocity.x = lerp(velocity.x, direction * dash , acceleration)
print(velocity.x)
else:
velocity.x = lerp(velocity.x, direction * SPEED , acceleration)
if prend_degats:
animated_sprite.play("aie")
elif dashing:
animated_sprite.play("jedash")
elif is_on_floor():
if direction == 0:
animated_sprite.play("idle")
else:
animated_sprite.play("run")
else:
animated_sprite.play("jump")
move_and_slide()
func _on_area_2d_area_entered(area: Area2D) -> void:
if area.name == "Epee":
epee = area
area.reparent(self)
area.position = Vector2(0, 0)
func _on_dash_timer_timeout() -> void:
dashing = false
func _on_encore_dash_timer_timeout() -> void:
peut_dash = true # Replace with function body.
func _on_degattimer_timeout() -> void:
prend_degats = false
func meurto() -> bool:
if vie <= 0:
print("dead")
mort = true;
print(mort)
return true
else:
print("dead void")
#mort = true;
#print(mort)
return false
func respawn():
position = %Checkpoint.position
get_tree().reload_current_scene()
#Ajoute une autre barre de vie a l'interface du joueur (meme chose avec nombre de pieces)