How can I make my player spawn at a certain place when it touches a certain area 2d?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By analienfrommarsy

So basically I’m making a platformer and I want the player to respawn at a certain place when it touches an area 2d under the lava.

:bust_in_silhouette: Reply From: Inces

There are things called signals in Godot. Areas and bodies have built in signals “on_area_entered” “on_body_entered”, they trigger the moment other physical object crosses borders of their collision shape. You want to connect those signals to a node of choice and implement respawning under this signal.

hey! I can’t figure out how to change the location when the onbodyentered is triggered

analienfrommarsy | 2021-10-29 06:32

Just override global_position to coordinates of your choice.

global_position = Vector2( something, something )

Inces | 2021-10-29 07:24

It’s simple just add a line of Code like this

func _on_Lava_area_entered(area):
	if area.is_in_group("Enemy"):
		$Player.position.x = "Position X"
		$Player.position.y = "Position Y"

Where it says “Position X” goes the position.x you want, the same in “Position Y” but with the position.y what you want

RubRub | 2021-10-29 07:27

Or you can add a Position2d in the position you want

func _on_Lava_area_entered(area):
    if area.is_in_group("Enemy"):
        $Player.position =  $Position2d.position

RubRub | 2021-10-29 07:35

Hey! um it still isn’t working

analienfrommarsy | 2021-10-29 09:06

You need to learn how to read errors. Print screen is too small to see, but I am sure error says, that invalid type has been asserted onto float value. position is of type Vector2, and it consists of two type floats - x and y. You tried to change them into type String - which is indicated by “” or yellow color in editor. Enter numbers, without “”.

And don’t use position, use global_position !!

Inces | 2021-10-29 12:31

Oh sorry if you did not understand me but ah the positions should not be put in quotes.

This is the real code

$player.position.x = 104
$player.position.y = 528


RubRub | 2021-10-29 14:14

Hey! I had already tried that out but didn’t work but no problem instead of changing the position I’m making the scene reload whenever it touches the bottom of the lava

analienfrommarsy | 2021-10-29 15:36

ok, but if you checked the comment about $ position2d? that works% 100

Or you can add a Position2d in the position you want

func _on_Lava_area_entered(area):
    if area.is_in_group("Enemy"):
        $Player.position =  $Position2d.position

RubRub | 2021-10-30 00:45