v4.3.stable.official (Version 4.3) - Godot Version
For starters, I wanna say I’m following Brackey’s Godot Tutorial video for the kill zone scene. Basically, I have a scene that is an Area2D node which when entered, a 3second timer will begin (which is set inside the scene) and when the timer reaches 0 it restarts the scene. Now, I don’t have a CollisionObject2D inside of the scene, as I set it externally, inside the main scene or the level.
This worked perfectly for when my character fell off the platforms! (I set up a WorldBorderCollision and set it as the body for the Kill Zone). However when I tried adding a spike area with a Collision2D with a shape of a rectangle to where my spikes were located, the character immediately died as soon as the level loaded. When I disabled the Collision2D for the spike, I didn’t die at the start, but when I touched the spike I didn’t die.
P.S. I added a debugging system that tells me what object I died to for every kill zone, and the system told me it was the Spike Area I was dying to.
Apologies because I did have a video although I am a new user so the forum disallowed me from sending any attachments. Although I could give the code I used:
Kill Zone code:
extends Area2D
@onready var timer = $Timer
@onready var myself = $"."
signal marioDied
func _on_body_entered(body: Node2D) -> void:
print(myself.name)
Engine.time_scale = 0.5
emit_signal("marioDied")
timer.start()
func _on_timer_timeout() -> void:
Engine.time_scale = 1
NewTransition.transition()
get_tree().reload_current_scene()
Player code:
extends CharacterBody2D
@onready var animatedSprite = $AnimatedSprite2D
@onready var jumpSFX = $Jump
@onready var skidSFX = $Skid
@onready var collision = $CollisionShape2D
@onready var deathSFX = $Death
@onready var music = $"../Music"
@export var jump_buffer_timer:float = .1
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
var acceleration = 0.25
var jumpBuffer:bool = false
var died:bool = false
func _physics_process(delta: float) -> void:
# Sets the direction of movement
var direction := Input.get_axis("left", "right")
if direction < 0:
animatedSprite.flip_h = true
elif direction > 0:
animatedSprite.flip_h = false
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
if died == false:
animatedSprite.play("jump")
else:
if died == false:
if direction == 0:
if acceleration < 0.75:
animatedSprite.play("idle")
else:
animatedSprite.play("skid")
if skidSFX.playing == false:
skidSFX.play()
else:
animatedSprite.play("default-walk")
if jumpBuffer:
jump()
jumpBuffer = false
# Handle jump.
if Input.is_action_just_pressed("jump"):
if is_on_floor():
jump()
else:
jumpBuffer = true
get_tree().create_timer(jump_buffer_timer).timeout.connect(on_jump_buffer_timeout)
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
if died == false:
if direction:
velocity.x = SPEED * acceleration
else:
velocity.x = move_toward(velocity.x, 0, SPEED) + (acceleration * 20)
if is_on_floor():
acceleration = move_toward(acceleration, direction, delta * 2)
else:
acceleration = move_toward(acceleration, direction, delta * 0.25)
else:
velocity.x = 0
move_and_slide()
func jump() -> void:
jumpSFX.pitch_scale = randf_range(0.85, 1.15)
jumpSFX.play()
velocity.y = JUMP_VELOCITY
func on_jump_buffer_timeout() -> void:
jumpBuffer = false
func _on_kill_zone_mario_died() -> void:
if died == false:
deathSFX.play()
music.volume_db = -100
get_tree().queue_delete(collision)
velocity.y = -500
died = true
animatedSprite.play("death")