+1 vote

Hey guys,

I'm making a platformer game with no prior experience.
I have a kinematic body for my main character and area2D bodies for my enemies.

So far, I've been able to make my character restart at the beginning of the level after she hits various enemies using this method:

Area 2D body is connected with body_enter

character script has this script:

onready var initialpos = getpos()

func kill():
set
pos(initial_pos)

and the main node has this script:

extends Node2D

func onEnemybodyenter( body ):
if body.getname() == "Player":
body.
kill()

so it all works fine but I'd now like now is: after a certain point in the game, when the Player dies she only returns to the halfway point.

How can I achieve this?

in Engine by (24 points)

You could also check the players position at point of death and use that to determine the respawn point.

2 Answers

0 votes
Best answer

You can create an invisible Sprite checkpoint node (or visible if you want) and place it as a checkpoint in the level.

Then, when you want to move the player, try:

func onEnemybodyenter( body ):
if body.getname() == "Player":
body.kill(get_node("checkpoint"))

and in player:

func kill(checkpoint):
setpos(checkpoint.get_pos())

If you want to have multiple checkpoints, then instead of passing getnode("checkpoint"), you can pass a node LatestCheckpoint, which is changed every time a player passes through a new area.

You can create a large collision rectangle for the checkpoint (as a child) to see if a player passed the area. Make it tall vertically so he wouldn't jump over it by accident.

P.S. Let me know what happens.

by (162 points)
selected by

setlinearvelocity is not present for kinematic bodies, you will want to use the move() function described here:

https://godot.readthedocs.io/en/stable/tutorials/2d/kinematic_character_2d.html

Try to also add the velocity code they have so you can use my code on it.

Don't use a Sprite. Position2D nodes were made specifically for things like this.

You're right Position2D would do.

It's just me, but I like to use accurate replicas of my character so I can have a sense of where he will be located after respawn.

Don't even need a node other than area unless you need more visual debug than a shape.

And shape could be a line shape if your game is a strict sidescroll, that way the player won't miss the checkpoint no matter how much tries to cheat :P

please show the whole script

+1 vote

Like a checkpoint?
Create an area that acts as a checkpoint and when player enters that area, register the new position on the player, it can use another variable for checkpoints or replace the value of the initial position with the new one.

The type of area you use and what you do after the player reachs the checkpoint will depend on the design.


A more flexible way could be creating re/spawn zones on your levels and make one active when the player touches it, that way you will de-couple the player from the spawn process leaving it on the level where it may belong (always depending on the design, of course).

by (7,890 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.