Okay! So this is the code I plan to use. I tested the target using my mouse coordinates initially, and it worked very well. It's a code I tried after looking up angle_difference() in the manual
GML:
var dir = point_direction(0,0,x_dir,y_dir); // Find direction of x_dir & y_dir
var ang = angle_difference(dir,point_direction(x,y,target.x,target.y)); // Find angle difference of current direction and direction to target
dir -= min(abs(ang), 10) * sign(ang); // Change direction
x_dir = lengthdir_x(1,dir); y_dir = lengthdir_y(1,dir); // Apply changes
In case you're wondering what x_dir and y_dir is, it's what I use to give the direction of the bullet.
The reason why I'm using this method instead of speed & direction which is built into the game object, I wanted to do it like this so I can code in my own ricochet code.
Code:
if (strike_type != "melee") && (strike_type != "piercing")
{
// COLLISION: Horizontal
if (place_meeting(x+x_dir * stat_speed,y,obj_parent_wall)) // Check if "x" value is about to collide
{
while (!place_meeting(x+sign(x_dir * stat_speed),y,obj_parent_wall)) // While there's space between "x" and hsp...
{x += sign(x_dir * stat_speed);} // add sign * hsp
if (strike_type == "ricochet")
{
x_dir *= -1;
if (limit_type == "distance")
{
stat_limit -= consumed_limit;
originX = x;
originY = y;
consumed_limit = (distance_to_point(originX,originY)); // Traveled distance increases as attack travels
}
}
else {instance_destroy(self,true);} // Strike destroys self and activates final_reaction
}
// COLLISION: Vertical
if (place_meeting(x,y+y_dir * stat_speed,obj_parent_wall)) // Check if "y" value is about to collide
{
while (!place_meeting(x,y+sign(y_dir * stat_speed),obj_parent_wall)) // While there's space between "y" and vsp...
{y += sign(y_dir * stat_speed);} // add sign * vsp
if (strike_type == "ricochet")
{
y_dir *= -1;
if (limit_type == "distance")
{
stat_limit -= consumed_limit;
originX = x;
originY = y;
consumed_limit = (distance_to_point(originX,originY)); // Traveled distance increases as attack travels
}
}
else {instance_destroy(self,true);} // Strike destroys self and activates final_reaction
}
}
image_angle = point_direction(0,0,x_dir,y_dir);
x += x_dir * stat_speed; // Apply movement on x value
y += y_dir * stat_speed; // Apply movement on y value
Eh heh, I don't want to talk about that ricochet code though, I wanted to show it in case you're wondering why I'm not using a traditional direction variable.
Regarding this important question
How is the target picked for the homing projectile? Is it the instance the mouse clicks on? The nearest instance to the targeting reticle (if there is a reticle)? The nearest instance to the instance launching the homing projectile?
I want it to look for the closest enemy possible.
Since the AI can shoot these homing bullets as well at other enemies (multiple hostile factions) I was wondering if it's possible to have the bullet check for the closest enemy that ALSO has a string that isn't the same as it's own.
- The bullet has a string called "team". They will inherit it from the enemy or player that fires it. The enemies can vary since there may be 2 hostile factions at once.
- Enemies have a string called "team" too.
Ideally, I want to see if it's possible to check available targets, pick one that isn't on the same team, and let the rest of the code work its magic. It only needs to run this code once, and after it is close enough to said available targets using distance_to_object();