Using Sin with move_towards_point.

I'm currently learning to use Sin from an old post from Gre_Lor12: https://forum.yoyogames.com/index.php?threads/vertical-sine-wave.8185/.
This is the code:

GML:
var Amp = 20; //How "Stretched" the wave is

var Freq = 10  //How much to increase the waves amplitude by (in px)

x = xstart + Amp*sin(y/Freq);
I applied it to an enemy script, which is placed in its step event. I've attempted to combine it with:
GML:
move_towards_point(obj_Player.x, obj_Player.y, enemy_speed);
However, it correctly moves in the sin motion, but does not approach the player.
Any help would be greatly appreciated.

Thanks!
 

TheouAegis

Member
Well, the script that you posted first manually affects the x coordinate. So, it's going to move toward the player, and then it's going to go right back to where it was before.

You could try
x += Amp*sin(y/Freq);
 

kburkhart84

Firehammer Games
I assume you want it to move basically in a straight line, but then wave along that straight line. So, you will need TWO separate positions. One will be the actual position on that straight line, and the other will be the "real" position, which is simply an offset from that line. If you combine them like this, even following the above suggestion, the move_towards_point() is going to move towards the point from the x/y where the object is along the wave, not along the "center" line if you get what I mean.
Code:
 *   *   *   *
*-*-*-*-*-*-*-*-
   *   *   *
I would assume you want the object's movement to generally be based on that center line, and then just wave along it. But move_towards_point() will slightly deform that because it moves from the literal current x/y towards the target, so it will calculate based on wherever the position is after applying the wave, instead of being straight. This may be enough for the effect you want, but if not, you will need to go away from move_towards_point and calculate the movement along that line using the direction to the point and the speed.

Another alternative(that may be better, and would allow for some animation), would be to actually draw sprite animation for it. The sprite would be about 3x as tall, but you animate the bullet going up and down the wave manually. This would allow you to no longer calculate sine waves at all, and you could have some nice effects along with the bullet, like a laser line down the middle with the bullet thingy moving up and down, and even a trail with that bullet too. You would just want to tweak the collision box/sphere for it, but it could result in much nicer visuals, and even technically be faster(not that performance is an issue, it usually isn't unless you have way too many bullets along with other stuff).
 
Well, the script that you posted first manually affects the x coordinate. So, it's going to move toward the player, and then it's going to go right back to where it was before.

You could try
x += Amp*sin(y/Freq);
That did it! I knew it was going to be something simple, but I still couldn't figure it out. Thanks much!
 
Top