Random movement

S

SinkSankSunk

Guest
Recently started trying my hand at this GML stuff.
So far I have my room, with my objects and death on player collision. But I have two birds that I am trying to get to move randomly around the game space, also changing directions when hitting an obj that isn't the player model.
In the hopes of the player model having to dodge not only my wall objects but also two moving objects.

Any help for be great.


SSS
 

Attachments

Hulle

Member
This is just a simple example for quick execution:

In bird

Code:
Create Event 

speed = 1 //Change to whatever speed you want
direction = irandom(360)  //Choose random direction

Step Event
var smart = collision_circle(x,y,5,OBJECT-HITTING,false,false); //Check for collision with OBJECT-HITTING

if smart
{
direction = irandom(360) //Choose another direction
}
Bear in mind, this is a very simple code, Bird might get stuck in the object it is hitting, and is in no means a permanent code. But it will get you going for now.
 
S

SinkSankSunk

Guest
Thanks, I'll give it a try.
I was trying timer/alarm just to even get the birds moving, but failing for some unknown to myself reason.
 
R

robproctor83

Guest
This can get really complicated depending on what exactly you are intending to do. For example, if you need the birds to also dodge around obstacles, and prevent getting stuck in walls. also, do you intend for the birds to periodically change direction, and not just wander in the same direction aimlessly? Some of these things are going to be more difficult than others, specifically if you need the birds to avoid random objects in your room.

I would give hulles code a try and see how far it gets you. If you want to periodically change the direction then you can just add an alarm and then in that alarm you can set the direction to a new random variable. Just make sure to reset the alarm so that it will periodically change the direction.

For avoiding objects in the room look into the motion path functions specific mp_potential_step it should get you in the right direction.

collisions are a little more complicated and there are quite a few ways you can go about it. Different ways provide different pros and cons but ultimately my suggestion would be to just use standard object collisions events if possible.
 
S

SinkSankSunk

Guest
Yea I have used the code above to get both birds moving, changing some parts that were not working for me when i was placed all other objects in the room apart from the player object.

Currently the birds are just bouncing around like balls rather than changing with out collision. I had tried setting the random movement using alarms previously but something wasn't working correctly, but without any errors getting displayed. So i just went for this method for the moment.

Thanks for the help.

SSS
 
Top