• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Snake Body-Segment Follow

C

captiongoosebut

Guest
First off, this isn't like ordinary snake game. I need a good way to have the segments follow in the path the player went, and in the correct order.

Right now my body segments follow the head part, and move from further away depending on the order they were created.

Code:
obj = instance_create(obj_char.x,obj_char.y,obj_segment);
obj.segments = instance_number(obj_segment);
I use that variable to multiply the distance_to_object that it waits to check.

Code:
if (instance_exists(obj_char))
{
    if (obj_char.moving == true)
    {
        if (distance_to_object(obj_char) > (addon+5)*segments)
        {
            move_towards_point(obj_char.x,obj_char.y,4);
        }
        else
        {
            speed = 0;
        }
    }
    else
    {
        move_towards_point(obj_char.x,obj_char.y,4);
    }
}
The addon variable was a solution I made to the following segments after the first, not being far enough away.

The problem I mostly face right now is that all the balls just follow blindy, but I want the to actually go in the path the player went, that way they dont just go through walls and 💩💩💩💩...

Edit: This is the kind of snake game I'm trying to emulate:
https://mattykins.com/game/danger-noodle
 

NightFrost

Member
Yeah, that kind of effect is done by remembering N previous positions of the head in a data structure. Depending how close to each other you want the pieces, you read either every value, or every other, or every third... and assign to body pieces in order. Whenever the head moves, you move all the values down one step and pop the previous head position on top of the list. The length of the list is number of body pieces times the spacing you want, and you shorten or lengthen it as you lose or gain body pieces. When new piece is gained, it has to use the last valid position on the list until the list has grown long enough.
 
Top