0 votes

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.

in Engine by (71 points)

1 Answer

0 votes
Best answer

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.

by (20,923 points)
selected by

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 moveandslide y-coordinates. When I said going down I meant at the same slope (though you probably already realized that).

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.