GameMaker Issues with paths

T

Thunder025

Guest
Very new to GMS as a whole, just completed the DnD breakout tutorial. Decided I'd stick with that game a little longer to practice some stuff, and worked on adding new powerups.

The one I'm currently working on is supposed to make the bat move slightly upwards in a smooth motion, then return back downwards (a little boost that, when hitting the ball that way, will speed up the ball). My current issue is I can't figure out how to make the bat move in that way - I tried using a while loop to make it repeatedly jump to a different y location until y is less than a certain number, but every time the loop is entered the game freezes and I have to use task manager to kill it.

I also tried making it follow a path, going in a straight line up, then a second path command to make it go back down. This sort of works - for some reason, the bat teleports to the top end of the path, but will move smoothly back down like it's supposed to.

I also have an issue where the button can be spammed and the bat will keep going up as long as it is spammed, despite me setting a check to see if it's already busy following a path so it can't be reactivated.

Any ideas on what I'm doing wrong?
 

CloseRange

Member
for the while loop you probobly structured it wrong. A tip, if the game freezes like this again next to the play button where you run the game is a square button that stops the game so no need for task manager (unless the whole editor freezes too)
the loop should have looked something like this:
Code:
while(y > 100)
     y -= 3;
you probobly had less than rather than greater than. or added a value instead of subtracted.
Anyway here is how it's best done. In the create event:
Code:
base_y = y; // the normal y position
offset_y = -32; // how high to move up
then in the step event you can do this:
Code:
var target_y = base_y;
if(keyboard_check(vk_space)) target_y += offset_y;
y = lerp(y, target_y, .1);
target_y is where we want to go and lerp will get us there.
Lerp (short for linear interpolation) takes 3 values. The first value is the start position (where we are) the second value is the destination (the target) and the third value is how far to the destination we want to move (value from 0 to 1, 0 being no movement 1 being instant movement)
add 3 variable blocks.
one named 'base_y' and set it to 'y' this is the normal position
one named 'offset_y' and set it to '-32' this is how high up to move
one named 'target_y' and set it to 'base_y' this is where we want to move to

in the step event:
add a variable block where you set y to the value 'lerp(y, target_y, .1)'
add another variable block and set 'target_y' equal to 'base_y'
Lerp (short for linear interpolation) takes 3 values. The first value is the start position (where we are) the second value is the destination (the target) and the third value is how far to the destination we want to move (value from 0 to 1, 0 being no movement 1 being instant movement)
and the target_y is just being reset back to its origin (for if we arnt pressing to move up)

in the keyboard check event (for whatever key you want)
add a variable block and set 'target_y' equal to 'base_y + offset_y'
this will just set the target to where we want to move
I'd recommend the code version simply because I think it's better to learn code early rather than starting with code blocks.
 
Last edited:
T

Thunder025

Guest
for the while loop you probobly structured it wrong. A tip, if the game freezes like this again next to the play button where you run the game is a square button that stops the game so no need for task manager (unless the whole editor freezes too)
the loop should have looked something like this:
Code:
while(y > 100)
     y -= 3;
you probobly had less than rather than greater than. or added a value instead of subtracted.
Anyway here is how it's best done. In the create event:
Code:
base_y = y; // the normal y position
offset_y = -32; // how high to move up
then in the step event you can do this:
Code:
var target_y = base_y;
if(keyboard_check(vk_space)) target_y += offset_y;
y = lerp(y, target_y, .1);
target_y is where we want to go and lerp will get us there.
Lerp (short for linear interpolation) takes 3 values. The first value is the start position (where we are) the second value is the destination (the target) and the third value is how far to the destination we want to move (value from 0 to 1, 0 being no movement 1 being instant movement)
add 3 variable blocks.
one named 'base_y' and set it to 'y' this is the normal position
one named 'offset_y' and set it to '-32' this is how high up to move
one named 'target_y' and set it to 'base_y' this is where we want to move to

in the step event:
add a variable block where you set y to the value 'lerp(y, target_y, .1)'
add another variable block and set 'target_y' equal to 'base_y'
Lerp (short for linear interpolation) takes 3 values. The first value is the start position (where we are) the second value is the destination (the target) and the third value is how far to the destination we want to move (value from 0 to 1, 0 being no movement 1 being instant movement)
and the target_y is just being reset back to its origin (for if we arnt pressing to move up)

in the keyboard check event (for whatever key you want)
add a variable block and set 'target_y' equal to 'base_y + offset_y'
this will just set the target to where we want to move
I'd recommend the code version simply because I think it's better to learn code early rather than starting with code blocks.
Thanks so much for the help! That seems to have done the trick for the most part - I have one other issue though currently, while the bat moves smoothly back to the starting y position when pressing the button to activate this movement, it still "snaps" to that target y position rather than moving smoothly to that location as well. Is there a way to do that with this method?
 
Top