Why is randomze() not working

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

I am trying to make my ball in pong go in a random direction when I start the game, but for some reazon everytime I try it goes in only one direction.

My code looks like this:

var random_dir = randi() % 100 + 5
var up_down = randi() % 10


func _ready():
    randomize()
    if up_down >= 5:
        random_dir *= -1
    else:
        random_dir`

For some reason the ball is always going down. I’m sure I am missing something important but I can’t figure out what.

:bust_in_silhouette: Reply From: kidscancode

You’re setting the random_dir and up_down variables before calling randomize() in ready(). Try this way:

var random_dir
var up_down

func _ready():
    randomize()
    random_dir = randi() % 100 + 5
    up_down = randi() % 10
    if up_down >= 5:        
        random_dir *= -1

You could probably be a lot more concise, but it’s not clear what the rest of your code needs these variables for.

Thanks that worked. I realized that I was very vague with my question - those variables were simply to figure out the direction the ball moved for the vector2 move_and_slide y-coordinates. When I said going down I meant at the same slope (though you probably already realized that).

TheNewGrant | 2019-10-15 22:39