• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Ai targeting script troubles

Hello I am making a top down spaceship shooter and I am currently using this script when the bullet is activated
//when the player gets close it shoots the bullet (enemy step event)
if (distance_to_object(obj_player)) <= 1000 && (cooldown < 1) {


instance_create_layer(x,y,"bullets",obj_bullet1)
cooldown = 10;

//Lazer sound & pitch
audio_play_sound(snd_lazer3,10,false)
audio_sound_pitch(snd_lazer3,random_range(0.8,1.2));

}
cooldown = cooldown - 1.0



//script
{
var origin,target,pspeed,dir,alpha,phi,beta;
origin = argument0;
target = argument1;
pspeed = argument2;
dir = point_direction(origin.x,origin.y,target.x,target.y);
alpha = target.speed / pspeed;
phi = degtorad(target.direction - dir);
beta = alpha * sin(phi);
if (abs(beta) >= 1) {
return (-1);
}
dir += radtodeg(arcsin(beta));
return dir;
}

//and I have this is the bullets creat event
direction = script_execute(scr_targeting,obj_enemy,obj_player,17)



my problem is that when there are multiple enemies, only one is shooting the player accurately, the others look like they are shooting based on where the accurate enemy is positioned .
asking for a friend
 
Last edited:

Alix

Member
It's because the "origin" (argument0) is always obj_enemy, not each instances' own ID. They are all the same object, therefore they will use the id of the first instance of the object I believe.

Maybe try it with "id" (basically the ID of the instance calling the script).
 

Alix

Member
The id is a local variable of each instance in the game.
Let's say obj_ennemy would be the human race, the id of a person would be his name. Get it?

(Just replace the obj_ennemy in the script call)
direction = script_execute(scr_targeting,id,obj_player,17)
 
Top