I was trying to buid something that predicts where it should shoot to hit the enemy, i have tried using a simple script that looked like this
(i'm not sure if this will work)
but it was innacure especially at long ranges and low bullet speeds
so i decided to make it myself and after some thinking i have got this beautiful paint cauculations
and after some more time and cauculations i present you my script
GML:
var i = instance_nearest;
var di = point_direction(x,y,i.x,i.y);
var dist = point_distance(x,y,i.x,i.y);
time = dist/bulletspeed;
alpha = i.direction - di;
beta = darcsin(dsin(alpha));
final = di + beta;
but it was innacure especially at long ranges and low bullet speeds
so i decided to make it myself and after some thinking i have got this beautiful paint cauculations
and after some more time and cauculations i present you my script
GML:
function scr_accurateshot(target,bulletspeed){
var di = point_direction(x,y,target.x,target.y);
var dist = point_distance(x,y,target.x,target.y);
var alpha = target.direction - di;
var a = sqr(target.speed) - sqr(bulletspeed);
var b = -2*dist*target.speed*dcos(alpha);
var c = sqr(dist);
var falsedelta = sqr(b) - (4*a*c);
if sign(falsedelta) != -1{
var time = (-b-sqrt(falsedelta))/(2*a);}
var delta = target.speed*time
var beta = bulletspeed*time
var delto = darcsin(dsin(alpha)*delta/beta);
var final = di + delto;
return final;
}