# how to make an object move in random directions.

#### Artwark

##### Member
I'm tasked to make a button_object that when it is clicked, an object should appear at a random direction both horizontally, vertically and diagonally with the same speed value.

move_random(1,1);
if random(10) >= 9
{
hspeed = 5;
}
else if random(10) <= 9
{
vspeed = 5;
}

If I apply this, the object comes randomly both horizontal & vertical but it doesn't come diagonally.
how do I fix this?

I

#### InkSho

##### Guest
for diagonals youd need vspeed and hspeed at the same time

#### Nux

##### Member
it wont go diagonal since (by the looks of it) only hspeed or vspeed will be set, not both.
try create two random values for each speed value for each axis similar to this:
Code:
``````hspeed = irandom_range(-1,1) * 5;
vspeed = irandom_range(-1,1) * 5;
// for if there is a chance both vspeed and hspeed become 0
if ( vspeed == 0 && hspeed == 0 ) hspeed = 5; // default to going right``````
there will be a chance the object will move up only, down only, left only, right only but also diagonly if both have a value.

(edit changed vspeed+hspeed == 0 to vspeed == 0 && hspeed == 0 )

#### Artwark

##### Member
So how should I do this?

Like give a variable and use hspeed and vspeed like this?

i = hspeed + vspeed?

I did try that and it still won't move diagonally.....

#### Nux

##### Member
alternatively you can set speed to 5 and change the rotation of the object by a randomised value with
Code:
``````speed = 5;
image_angle = irandom(7) * 45; // randomised rotation to go up/down/left/right/45 degrees diagonally``````

Last edited:

#### Artwark

##### Member
it wont go diagonal since (by the looks of it) only hspeed or vspeed will be set, not both.
try create two random values for each speed value for each axis similar to this:
Code:
``````hspeed = irandom_range(-1,1) * 5;
vspeed = irandom_range(-1,1) * 5;
// for if there is a chance both vspeed and hspeed become 0
if ( vspeed == 0 && hspeed == 0 ) hspeed = 5; // default to going right``````
there will be a chance the object will move up only, down only, left only, right only but also diagonly if both have a value.

(edit changed vspeed+hspeed == 0 to vspeed == 0 && hspeed == 0 )
Thank you for the logic.

But there's something that I don't understand in the logic. how is it that you give the statement vspeed + hspeed == 0 if you haven't given it a variable to give the value 0?

Also, I looked at the irandom_range and I'm not able to understand it properly, could you explain what it does and how it works in this case?

#### Nux

##### Member
But there's something that I don't understand in the logic. how is it that you give the statement vspeed + hspeed == 0 if you haven't given it a variable to give the value 0?
Oh, basically the logic is checking if vspeed equals 0 and hspeed equals 0 if vspeed = 1 then it will not activate the code
it's much like picking up a fruit and saying "is this an apple" and if it's not an apple you put it back, but if it is an apple you buy it.

Also, I looked at the irandom_range and I'm not able to understand it properly, could you explain what it does and how it works in this case?
irandom_range is generating a random number between -1 and 1; the computer will set the value to -1, 0 or 1

what the code is doing is picking random directions:
these directions are: up, down, left, right, diagonally up ( left and right ), diagonally down ( left and right )

#### Artwark

##### Member
Oh, basically the logic is checking if vspeed equals 0 and hspeed equals 0 if vspeed = 1 then it will not activate the code
it's much like picking up a fruit and saying "is this an apple" and if it's not an apple you put it back, but if it is an apple you buy it.

irandom_range is generating a random number between -1 and 1; the computer will set the value to -1, 0 or 1

what the code is doing is picking random directions:
these directions are: up, down, left, right, diagonally up ( left and right ), diagonally down ( left and right )
oh ok...so you don't need to give a variable like horizontal = 0 and vertical = 0 and then make it so that it goes like this

move_random(1,1);
if random(10) >= 9
{
hspeed = 5;
vertical ++
}
else if random(10) <= 9
{
vspeed = 5;
horizontal ++
}
if horizontal == 1 && vertical == 1
{
direction = 45;
speed = 5;
horizontal = 0;
vertical = 0;
}

I was going to do this or at least I tried something like this. It still doesn't move diagonally though and I don't understand why since direction will rotate the object and based on the angle that it rotates, the speed should go on that distance....or is that something that the image_angle is suppose to do instead...? idk.

But thanks anyways. I hope I'm not too annoying over here seeing as how frequently I've asked help here.

#### Nux

##### Member
dude don't worry! ^w^
anyway i believe you're over complicating things:
move_random(1,1);
if random(10) >= 9
{
hspeed = 5;
vertical ++
}
else if random(10) <= 9
{
vspeed = 5;
horizontal ++
}
if horizontal == 1 && vertical == 1
{
direction = 45;
speed = 5;
horizontal = 0;
vertical = 0;
}
there is way too much going on here and finding it difficult to understand what you're trying to acomplish
there's no reason to set horizontal?/vertical? to 0 unless you want to make the object stop moving.

try the way i posted here