K
Kay
Guest
Not quite sure how to title my issue.
So, I made a segmented snake-type enemy and I want the body segments to follow the path of the snake head exactly. Right now it looks like this though.
The first few segments follow the motion of the head a little bit, but the ones after that just follow smoothly. This would be fine in some cases, but it's not the effect I want. I want a part to follow the part before it at a constant distance on a constant path.
This is the code I use inside the step event of the snake head object:
Now the step event of o_snakebody:
Lerp-ing towards a moving object on every frame obviously changes the distance constantly, which is my issue.
I previously fiddled around with it a bit and got it to look like this.
The path looks to be correct, but the segments don't actually move.
Here's what I changed:
I also tried this code for the movement of the segments:
Which resulted in this. This seems to be very close, but the movement is very jittery.
The solution seems to be embarassingly simple, but I just don't know how to do it.
So, I made a segmented snake-type enemy and I want the body segments to follow the path of the snake head exactly. Right now it looks like this though.
The first few segments follow the motion of the head a little bit, but the ones after that just follow smoothly. This would be fine in some cases, but it's not the effect I want. I want a part to follow the part before it at a constant distance on a constant path.
This is the code I use inside the step event of the snake head object:
Code:
//following the mouse + snake motion
x = lerp(x, mouse_x, 0.02) + lengthdir_x(sin(t/8)*2, direction + 90); //t is a variable that increments every step
y = lerp(y, mouse_y, 0.02) + lengthdir_y(sin(t/8)*2, direction + 90);
//the following part spawns one segment and passes some variables,
//the most important one being the id of the object that spawned it
if ((tail_parts > 0) && (checkonce = false))
{
tail_parts -= 1;
checkonce = true;
if tail_parts = 0
{
with instance_create_layer(x, y, "Instances", o_snakebody)
{
tail_parts = other.tail_parts;
followthisone = other.takethis_id;
takethis_id = id;
checkonce = false;
sprite_index = spr_snakeend;
}
}
else
{
with instance_create_layer(x, y, "Instances", o_snakebody)
{
tail_parts = other.tail_parts;
followthisone = other.takethis_id;
takethis_id = id;
checkonce = false;
sprite_index = spr_snakebody;
}
}
}
Now the step event of o_snakebody:
Code:
//this is obviously the issue
x = lerp (x, followthisone.x, 0.1);
y = lerp (y, followthisone.y, 0.1);
//afterwards it executes the second half of the code of o_snakehead
I previously fiddled around with it a bit and got it to look like this.
The path looks to be correct, but the segments don't actually move.
Here's what I changed:
Code:
//added this to both objects
//also passed those variables
if t mod 10 = 0
{
delayed_x = x;
delayed_y = y;
}
//changed the movement of the segments:
x = followthisone.delayed_x;
y = followthisone.delayed_y;
Code:
move_towards_point(followthisone.delayed_x, followthisone.delayed_y, 3);
//instead of
//x = followthisone.delayed_x;
//y = followthisone.delayed_y;
The solution seems to be embarassingly simple, but I just don't know how to do it.