![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | GodotUser21 |
I am making a 2d platformer with a checkpoint. The character will respawn there after dying. However, I cannot teleport to the checkpoint after dying it teleports me to the wrong place. In a previous question, a user told me to use global_position() I have changed my code to this.
Checkpoint script:
extends Area2D
var checkpoint_pos = self.global_position
func _on_Checkpoint_body_entered(body: Node) -> void:
queue_free()
Vector2(get_global_position())
Player hit checkpoint script:
func _on_Checkpoint_body_entered(body: Node) -> void:
Vector2(get_global_position())
checkpoint_hit = true
Player respawn script:
elif will_die == true and is_on_floor() and checkpoint_hit == true:
self.global_position = CheckpointReal.checkpoint_pos
print("fall damage taken")
will_die = false
Just a note: CheckpointReal is the global variable
The checkpoint script is an area 2d and not A 2D node
The player script is in a kinematic body 2d
Thanks so much
The _on_Checkpoint_body_entered()
function should probably look more like this:
func _on_Checkpoint_body_entered(body: Node) -> void:
# It may be a good idea to check what's coming through the Area2D.
if body is the_player:
# Make sure this variable's value is accessible from the script which puts the player at the checkpoint.
var some_position: Vector2 = body.get_global_position()
queue_free()
The function related to the checkpoint in the player’s scene probably isn’t necessary; the checkpoint script should take care of any respawning.
The code for respawning the player (or putting the player at the checkpoint) should be changed:
...
elif will_die and is_on_floor() and checkpoint_hit:
# Put the player at the checkpoint. As you may guess, the player scene's object is represented by the variable `the_player`.
the_player.global_position = CheckpointReal.checkpoint_pos
print("Going down!")
will_die = false
I don’t know whether this will work. But it should help you to find an answer.
Ertain | 2022-09-06 02:32
I would like to ask about the
the_player.global_position = CheckpointReal.checkpoint_pos print("Going down!") will_die = false
What is “the_player” supposed to mean? Is it what I name my sprite? Should I use self?
GodotUser21 | 2022-09-06 06:28
the_player
is a reference to the player script/scene. The respawning code should get a reference to the player’s script/scene.
Ertain | 2022-09-07 00:04