0 votes

I'm creating a game like google dinosaur, tell me how to make it so that after CollisionShape2D.is_colliding () becomes false - a new TileMap will spawn from the list at the RayCast2D position?

https://imgur.com/a/vhFtlqJ

Godot version latest
in Engine by (14 points)

1 Answer

+1 vote

CollisionShape2D does not seem to have any is_colliding() function, did you mean Area2D.get_overlapping_bodies().size() > 0?

You could do something like this:

var my_area: Area2D
var my_raycast: RayCast2D
var options := [preload("res://..."), ...]

func _ready():
    my_area = $Area #Replace with actual path to the nodes
    my_raycast = $Raycast
    my_area.connect("body_exited", self, "check_count")

func check_count(_body_which_exited):
    if my_area.get_overlapping_bodies().size() > 0:
        var chosen_tilemap := randi() % options.size()
        var new_tilemap: Node2D= options[chosen_tilemap].instance()

       #Pick one of the two lines
        new_tilemap.position = my_raycast.position #For the actual raycast's position
       new_tilemap.position = my_raycast.get_collider().position #For the position of what it collides with.
        add_child(new_tilemap)

Edit: wrote my_area.get_overlapping_bodies instead of my_area.get_overlapping_bodies().size().

by (2,684 points)
edited by

Ok, I did everything, thank you so much!
It remains only to make the function of deleting Tilemaps that the player has already passed

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.