Checkpoints not working

4.3

can someone help me with this checkpoint code when the game launches it immediately sets me to checkpoint 1

extends Area3D

var checkpoint = Vector3(-6.555,7.537,0)

func _on_body_entered(_body: Node3D) → void:
get_tree().create_timer(3)
$“…/…/…/Player”.position = Vector3(checkpoint)
$“…/…/…/2D stuff/Void Death”.visible = true

func _on_checkpoint_1_body_entered(body: Node3D) → void:
checkpoint = Vector3(77.56,7.891,0)

func _ready() → void:
checkpoint = Vector3(-6.555,7.537,0)

Hi there,

Could you please be more specific about the behavior you want to implement ?

From what I see, it seems that the issue is that only the checkpoint value is changed when the game launches. The _on_checkpoint_1_body_entered method is not called so your player position will not change when the game launches.

Fix example:

func _ready() -> void:
	checkpoint = Vector3(-6.555,7.537,0)
	$“…/…/…/Player”.position = checkpoint

I can also see that you’re not waiting for the timer you created to time out.
In Godot 4, you can use the await keyword to wait for the timer to finish before moving the player. For example, change your _on_body_entered function to:

func _on_body_entered(_body: Node3D) -> void:
	await get_tree().create_timer(3).timeout
	$“…/…/…/Player”.position = Vector3(checkpoint)
	$“…/…/…/2D stuff/Void Death”.visible = true

This way, the code will wait 3 seconds before setting the player’s position.

Also, double-check your signal connections to ensure that the _on_checkpoint_1_body_entered function is only called when you actually enter that specific checkpoint area, and not on game launch.

1 Like

The function is launching on launch how would i fix this. It is teleporting me to the second checkpoint 3 seconds after launch.

Just don’t call up the teleport function at the start of the game, but whenever you want to.

Check this article to know more about signals connections.

You said to not call it on start but i currently don’t call them at all. I am about to read that article. here’s my code

extends Area3D

var checkpoint = Vector3(-6.555,7.537,0)

func _ready() → void:
checkpoint = Vector3(-6.555,7.537,0)

func _on_body_entered(_body: Node3D) → void:
await get_tree().create_timer(3).timeout
$“…/…/…/Player”.position = Vector3(checkpoint)

func _on_checkpoint_1_body_entered(body: Node3D) → void:
checkpoint = Vector3(77.34,7.771,0)

I see, the problem is that the _on_body_entered method is called at game startup.
It must be a node that collides with your Area3D node as soon as the game is started.

how would i fix this?

To resolve this issue, make sure that nothing unintended is colliding with your Area3D node at game startup.
Check your collision layers and masks so that only the intended node (for example, the player) can trigger the _on_body_entered method.
Additionally, consider adding a condition in the _on_body_entered function to verify that the colliding body is the one you expect.

Alright I got it fixed. I didn’t realize that it included static bodies and other collision shapes. Thank you so much.

1 Like

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