I've just picked up godot and am trying to make a game where if a player dies(by hitting a spike or any other hazard) it forms a dead body at that position and the player position is reset to its original position. This is the function that gets called when the player hits a spike DeadBody is a kinematicBody2d


func die():
	var DeadBody = preload("res://scenes/DeadBody.tscn").instance()
	get_parent().add_child(DeadBody)
	var temp_pos = position
	position = original_pos
	get_parent().get_node("DeadBody").position = temp_pos

sometimes they spawn at the players location but most of the times they don't

what am i doing wrong?

You can't have nodes with the same name at the same path. So when you create more than one DeadBody, the body is automatically renamed by the engine, so its path will be different. Instead of trying to get the dead body with get_node, I would just use the DeadBody variable that you already have.

    DeadBody.position = temp_pos

Also, something else to add: position is the offset in position relative to the parent node. If both nodes have the same parent, then using position will work fine, but if they have different parents with different offsets, you probably want to use global_position, as its the position relative to the world origin (0,0,0).

2 years later