0 votes

I'm trying to figure out how to make sure new instances(asteroids) spawn in front of my "player" when he is moving and the ones that are out of the view get deleted. because for now, each asteroid is spawning to slow behind the player. also how would you make them spin slowly when spawning, in different directions to look more like "realistic asteroids", if the asteroids come from the bottom up it doesn't bother me thats actually good to?
if this looks bad I barely know anything about codeing.

`extends Node2D
var preflyingobject= preload("res://objects/flyingobject.tscn")
var good
to_spawn=true

Called when the node enters the scene tree for the first time.

func _ready():
pass

func _process(delta):
pass

func onArea2Dbodyentered(body):

if "flying" or "player" in body.name:
    good_to_spawn=false

func onTimertimeout():
if good
tospawn==true:
var new
flyingobject=preflyingobject.instance()
get
parent().addchild(newflyingobject)
newflyingobject.position=position
queue
free()`

in Engine by (12 points)

1 Answer

0 votes

This is a partial answer.

In order to delete things that are out of view, you can just add a VisibilityNotifier2D node to your asteroid scene. Then you can listen for screen_exited signal, and then go ahead and remove the node.

Asteroid
    > VisibilityNotifier2D

# Asteroid.gd

# you can put whatever you want here for the range
const ROTATION_SPEED = [5, 90] # in degrees/second
const rotation_amount = 0

func _ready():
    $VisibilityNotifier2D.connect("screen_exited", self, "_on_screen_exited")
    set_random_rotation()

func _process(delta):
    rotation_degrees += delta * rotation_amount

func _on_screen_exited():
    queue_free()

func set_random_rotation():
    randomize()
    var direction = 1 if randf() > 0.5 else -1
    rotation_amount = direction * rand_range(ROTATION_SPEED[0], ROTATION_SPEED[1])

As far as where to instance nodes, can you provide a little more detail about the way things are set up?

by (1,605 points)
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 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 webmaster@godotengine.org with your username.