Random spawn in godot 3.5

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By door2020

please help me I am programming on godot 3.5. I need a sprite to appear every 5 seconds in a random place on the screen (node2d → Kinematic Body2D-> Sprite, CollisionShape2D) and if the main character touches it (node2d → Kinematic Body2D-> Sprite, CollisionShape2D) then he teleports to where he was at the beginning. If I somehow put the nodes wrong, then please help, maybe I need to do something differently, I’m waiting for help)

:bust_in_silhouette: Reply From: godot_dev_
  • This post might help you with spawning at a random location
  • To spawn an enemy every 5 seconds, you could use a Timer and connect your spawn function to the timeout signal of the timer. Every time the timer timesout, you could also start the timer again
  • For the collision detection, you want to make sure the collision_mask and collision_layer properties are correctly configured so that your player can collide with the sprite.
  • For teleporting the player back to the beginning, I am not sure what beggining you speak of, but you will need to keep track of the position the player was in at said beginning. If the teleport position will never change, it is relatively straight forward, just set the player.position to the desired position. If the position changes (teleporting to the last checkpoints for example), then you may need logic to track and decide when the player has acheived a new position to update the teleport position.