Topic was automatically imported from the old Question2Answer platform.
Asked By
Rayu
(One Screen Game)
I want that if the Game is Started that Player and Enemy spawn in different positions on the screen every time and Move Randomly every time (they will bounce of the screen and not frantic)
Every time you think of something “right when it started” you can use the ready function
func _ready():
##code here will run once on load
To pseudo randomize something, you will need to call for the randomize function once on the beginning of the level scene or project, you can also use the ready function for that. This will make sure that any random number generator function you use will use a different random seed every time this is called.
randomize()
Now to generate randomness… there are many many ways… but I’ll give it a shot. Since what you want is positions (vector2 x y), you will need 2 values and whose values will have to be inside the viewport/camera region
randf() ## will give you a float value from 0 to 0.999999999.... (as far as I know it is never 1 unless its rounded)
randi() % 20 ## will give you an integer between 1 and 19 if I'm not mistaken
and you can round values with “round()”
so assuming your game level is 100x100, on the ready function I’d call