1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Random movement

Discussion in 'Programming' started by SinkSankSunk, Nov 6, 2019.

  1. SinkSankSunk

    SinkSankSunk Member

    Joined:
    Nov 6, 2019
    Posts:
    3
    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
     

    Attached Files:

  2. Hulle

    Hulle Member

    Joined:
    Oct 29, 2019
    Posts:
    8
    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.
     
    SinkSankSunk likes this.
  3. SinkSankSunk

    SinkSankSunk Member

    Joined:
    Nov 6, 2019
    Posts:
    3
    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.
     
  4. robproctor83

    robproctor83 Member

    Joined:
    Sep 30, 2019
    Posts:
    65
    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.
     
  5. SinkSankSunk

    SinkSankSunk Member

    Joined:
    Nov 6, 2019
    Posts:
    3
    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
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice