19 OctProjectile Speed on Mouse Position
Something I’ve been working on lately in one of my games is working in dynamic projectile speeds so players can strategically toss grenades to where they need to go rather than having to find some way to make the default speed work for them. So in my case, a player wants to throw a grenade to where their cursor is positioned. What I did was set up the angle calculations which is the easy part, though then there’s speed afterwards.
var mspd; //Creates the temporary value, mspd
mspd =(point_distance(x,y,mouse_x,mouse_y)/16); //Sets mspd to our needed speed
The above GML finds the distance between the source of the projectile and your mouse cursor, then divides by 16 to adjust the returned value to a usable speed (otherwise it’d go quite fast) This is how I used the code in my game:
var plyspd, gspd;
plyspd = o_player.hsp //Horizontal Speed of My Character
if plyspd < 0 { plyspd=plyspd*-1; } //If player is moving backwards, switch negative speed to positive
gspd = plyspd+(point_distance(x,y,mouse_x,mouse_y)/16) //Speed will be the calculated speed PLUS the speed the player is moving at
if gspd > (18.92)+plyspd { gspd = (18.92)+plyspd; } //Keeps the projectile from going rediculously fast by setting a limit plus the player's current speed







