Home Game Development collision detection – How can I change scenes when the player touches another CollisionShape2D in Godot 3.5?

collision detection – How can I change scenes when the player touches another CollisionShape2D in Godot 3.5?

0
collision detection – How can I change scenes when the player touches another CollisionShape2D in Godot 3.5?

[ad_1]

I suspect the reason why your scene transition triggers immediately is the signal you’re currently intercepting.

I assume the method _on_Area2D_body_entered() is in the player script. The body_entered() signal is emitted when a PhysicsBody2D or a TileMap enters an Area2D, so your player is probably triggering it by simply standing on the floor, if any.

…when the player touches another CollisionShape2D.

Actually, CollisionShape2D only provide a collision shape to CollisionObject2D-derived Nodes. While based on CollisionShapes, collisions occur among Nodes. There are two main categories of collision signals: areas and bodies. They allow you to know just what area/body a given Node collided with, or both the involved area/body and which particular collision shape collided.

If your colliding object only uses one CollisionShape2D and requires no physics, you can keep things simple and detect the area_entered() signal instead. Moreover, you can move the scene transition logic to the “teleporter” Node in a new script, since scene changing has now become its responsibility, and the player will implement the logic for collisions that only affect its movement or interaction.

Here’s a handy script for your “teleporter” object, which in fact I called Teleporter and saved as a separate scene:

extends Area2D


export var go_to_scene: PackedScene


func _on_Teleport_area_entered(area: Area2D):
    if area.name == "Player":
        get_tree().change_scene_to(go_to_scene)

In the Teleporter scene (NOT your level scene), if you connect the area_entered() signal to the method above, you’ll have it detect the player entering its collision shape wherever you instantiate the teleporter. Also, exporting the go_to_scene variable as a PackedScene allows you to quickly select the next scene from the Inspector, by clicking on its input field, then Quick Load:

enter image description here

[ad_2]