Existing player vs script created

Hi
i wrote a code that creates the player object on each level the player enters and I want it to also support editor placed player objects. that’s where I have problem, the script created player works properly when i change it’s position with the position variable but for some reason the existing player object goes to wrong coordinates when trying to set it’s position

Here’s the code that handles both player creation and detecting existing player:

func _ready():
	var currentPlayer = get_node("Pelaaja/Palomies") // Try to find existing player

	print("New player! " + (str(currentPlayer) ) + " . " + get_tree().current_scene.name + " . " + (str(get_parent())))

    // If no player create it
	if(currentPlayer == null):
		var scene = preload("res://palomies.tscn")
		var instance = scene.instantiate()
		add_child(instance)
		print("instance " + (str(instance) )  + " " + (str(get_node("Pelaaja/Palomies"))))
		currentPlayer = get_node("Pelaaja/Palomies")

so why does those two player objects behave so differently?

thanks!

(Sorry I forgot the Godot version but should be pretty much latest)

can you show the code of where you set the character position?

My guess would be that you move your player by setting his position.
This will change the players coordinates relative to where he is globally. This would work with the spawned player because his global position is (0,0) initially.

You could try to update the coordinates when you move the player by changing the global_position instead of position.

thanks, changing to global_position fixed the problem

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.