Reputation: 65
I am making an game that is like asteroids and I want to place an asteroid around the player but not always in the same place and not to far or close.
this is my current code(planet is another asteroid with a rigid body sprite and collision shape):
func create_asteroid():
var planet_instance = asteroid.instance()
asteroid_instance.position = self.position
get_tree().get_root().call_deferred("add_child", asteroid_instance)
Upvotes: 1
Views: 1675
Reputation: 40220
You could pick an angle at random:
var angle := rand_range(0, TAU)
Similarly, a distance:
var distance := rand_range(min_distance, max_distance)
And then figure out where that is from self.position
. There is a polar2cartesian
we can use for that:
asteroid_instance.position = self.position + polar2cartesian(distance, angle)
By the way, remember to call randomize
at the start of the game, to seed the random number generator. If you need more control over that (e.g. being able to input a seed) use the RandomNumberGenerator
class.
Upvotes: 1