How to make spikes/stuff that kill you in a platformer

4.3

So I have been making platformer lately and I like just need spikes before I upgrade the map does anyone know how to make spikes?

What do you have so far? What do your spikes need to do?

Using an Area2D’s body_entered signal you can call a function to deal damage to your player/CharacterBody2D.

thanks my spikes need to send the player back to the start of the map.


Cool, you can store the starting position on _ready() and re-set that when the player is spiked.

extends CharacterBody2D

var starting_position: Vector2
func _ready() -> void:
    starting_position = position

func die() -> void:
    position = starting_position

The spike will only have to call body.die() on applicable player collisions.

So this is the code for it???

A sample for your player yes. Your spikes will need an Area2D as I’ve described here

ok do you recommend any tutorials that is up to date?

Not much else too it, if you are having trouble connecting signals through the editor you can check this tutorial from the official docs

This spike script could be this

extends Area2D

func _on_body_entered(body: Node2D) -> void:
    if body.name == "Player":
        body.die()

There are many solutions to this problem which you can find multiple tutorials on youtube for, here is one I think is nice to implement and is quite simple:

To setup your character for taking damage/dying there are 2 steps:

  1. Add the player scene to a scene group called “Player” via the right side in the Node tab in the Groups sub-tab
  2. Add a new function in the player script, that you will make the damaging objects in your game to call, so make it something generic with the option for paramters if you want to make it more verstiale, but just a “die” function should do the job in this speficic case

Now, create a spike scene that can interact with the player:

  1. Create a new scene, with a root node being a Sprite2D
  2. Add an Area2D and a Shape to the area that fits your sprite
  3. Go in your right side of the editor to the node tab and to the signals sub-tab
  4. In there, find the signal called “body_entered” (assuming your character is a CharacterBody2D), and connect it to the scene’s script
  5. in the function that is created check if the body that entered is_in_group(“Player”), and if it is, make it call the function that you created (for example you named the paramater body_entered, you’d write body_entered.die() )

And that’s it! Now, you can write whatever code you want in the “die” function so the player does an animation and then emits a signal to the level to restart, or just change it’s position to the start of the level or whatever you want to happen.

But the thing is that I’m not using 2 scenes I have a camera that follows the player out of the rendering zone so that’s how my game works! I will send you a picture soon of my game setup