• 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!

enemy AI shooting problem

Y

Yusufpopen

Guest
Hi im a new to programming and im trying to make a pirate game. But im having problem with the enemy ship AI. I wanna make it so that when the ship gets near the player it will smoothly rotate to one of it sides to shoot. Im have been trying some stuff but i can only make it so that it works when it's chasing the player but as soon as it gets close ,it will start spinning uncontrollable.

im on GM 1.4
Here is my code

Code:
-Create event-
pointdir = point_direction(x, y, obj_player.x ,obj_playerj.y)

rspeed = 4

range = 64

-Step event-
if (distance_to_object(obj_player) <= minimumDistance)
{         
  
    image_angle += sin(degtorad(pointdir - 90)) * rspeed
    direction  = image_angle
    
} else {
        
    mp_potential_step(obj_player.x,obj_player.y,2,true)
    image_angle  = direction
}
 
I have a script that might be helpful... it's a bit old, and there's probably a better way to do this, but if you know the angle you want to rotate to, and how fast you want to rotate: this will do it for ya.

Code:
///angle_rotate_towards(start direction, target direction, velocity);
//Returns a new angle rotated towards the target direction with the given velocity.
//If the given start is already within the velocity of the target, the angle will be snapped exactly and returned.

var _start = argument[0];
var _target = argument[1];
var _velocity = argument[2];
var _direction = noone;

_direction = ((((_start - _target) mod 360) + 540) mod 360) - 180;

if(abs(_direction)<_velocity)
    return(_target);
else
{
    return(_start-_velocity*sign(_direction));
}
 

samspade

Member
Hi im a new to programming and im trying to make a pirate game. But im having problem with the enemy ship AI. I wanna make it so that when the ship gets near the player it will smoothly rotate to one of it sides to shoot. Im have been trying some stuff but i can only make it so that it works when it's chasing the player but as soon as it gets close ,it will start spinning uncontrollable.

im on GM 1.4
Here is my code

Code:
-Create event-
pointdir = point_direction(x, y, obj_player.x ,obj_playerj.y)

rspeed = 4

range = 64

-Step event-
if (distance_to_object(obj_player) <= minimumDistance)
{        
 
    image_angle += sin(degtorad(pointdir - 90)) * rspeed
    direction  = image_angle
   
} else {
       
    mp_potential_step(obj_player.x,obj_player.y,2,true)
    image_angle  = direction
}
This may also work depending upon what you want. I use it in a number of places. Its taken from the example code for angle_difference, just replace the point direction with the correct parameters.

Code:
var pd = point_direction(x, y, mouse_x, mouse_y);
var dd = angle_difference(image_angle, pd);
image_angle -= min(abs(dd), 10) * sign(dd);
I believe the reason your current code doesn't work is that "sin(degtorad(pointdir - 90)) * rspeed" will always return a number which is not 0 meaning that if the enemy is close enough it's image angle will always be changing. You could test this in the debugger or with show_debug_message(string(sin(degtorad(pointdir - 90)) * rspeed) placed right before that line.
 
Top