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.

asked Oct 15, 2019 in Engine by TheNewGrant (49 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.

answered Oct 15, 2019 by kidscancode (17,714 points)
selected Oct 22, 2019 by TheNewGrant

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.