Godot Version
3.6 beta
I am trying to implement a system that when the player died he will respawn near a ground where he died not the starting position
Godot Version
3.6 beta
I am trying to implement a system that when the player died he will respawn near a ground where he died not the starting position
I am trying to implement a system that when the player died he will respawn near a ground where he died not the starting position
Is there any demo or tutorial for this
I’m of course working on the version 4.2 engine, but I don’t think things have changed that much.
Here is my vision of a solution to the problem:
Create an empty script and name it Global (you can call it something else, but I’ll go by that name now) and don’t attach it to the node.
Go to project settings - autoload and attach the script there, it will automatically become global and available in other scripts.
Next, go to Global.gd and create a variable in it: LastGlobalPosition of Vector2 type.
Then go to your player’s script and add a variable waitToChangeGlobalPos of type Timer.
Then in the player’s script in the _ready function set the following settings for the timer:
waitToChangeGlobalPos.wait_time = “What you want in seconds”,
waitToChangeGlobalPos.autostart = true
waitToChangeGlobalPos.oneshot = false
and add the timer to the scene.
Also, don’t forget to connect the timer end timer signal to the player using connect
Now the timer will automatically start after a certain time.
After in the player script add a function that will be called when the timer finishes and write the following:
Global.LastGlobalPosition = GlobalPosition
You now have the last coordinates of the player saved and updated after a certain amount of time.
If you have a platformer, you can also add a condition to the signal function that you only save if the player is on the ground for example.
Then after the death of a player, create a new one and set GlobalPosition = Global.LastGlobalPosition, or set all values to default and also set GlobalPos = Global.LastGlobalPosition.