• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

GMS 2.3+ Jet Fighter Clone. Help with enemy movement.

Hey guys, I started learning Game Maker Studio about a week ago. I have completed a few tutorials and made Pong on my own with a computer AI to play against. So not much experience. I have been using the manual to try and work things out or to try and take the completed tutorials further. This however really has me stumped.

I thought I would try and recreate Jet Fighter by Atari. The player and enemy fly around the room as jets trying to shoot each other down. When they turn in space they turn gradually. I can get the player movement no problem but I just can't work it out for the enemy AI. I have the enemy fly towards the player and shoot when in range but I can't pull off the turning aspect. This is my code for the player.

Create event

speed = 6;
direction = direction;

Step event // Player movement

if keyboard_check(vk_left)
{direction += 2;
image_angle = direction;}

if keyboard_check(vk_right)
{direction -= 2;
image_angle = direction;}

Outside room event

move_wrap(true, true, 0);

How can I take my player movement and apply it to the enemy jet. At the moment the enemy jet will fly directly towards the player using 'move_towards_point', and will only fire when in range by using 'distance_to_point'. If the player turns the enemy turns sharply. I have tried using 'direction' in various ways but just can't get the enemy to turn gradually like the player. The enemy jet can wrap around the screen but chooses not to. When the player leaves the screen and appears on the other side the enemy jet just turns back on itself. My code for the enemy became so messed up I had to start again. Would be great to get an idea of what to try or a recommended page from the manual or if you guys know of a tutorial that may help.

Thanks :)
 
Last edited:

Nidoking

Member
You want to change the direction only by a maximum amount per step. Move toward the point_direction but don't jump directly to it unless you're already close enough. This is turning 101.
 

Fielmann

Member
This ought to work (enemy's step event):

GML:
var dir_to_player = point_direction(x, y, o_player.x, o_player.y);
var diff = dir_to_player - direction;

if ((diff > 0 && diff < 180) || diff < -180) {
    direction += 2;
} else {
    direction -= 2;
}
o_player is the name of the player object. This assumes that object only has one instance.
 
Thanks for the reply Nidoking. I tried but just couldn't figure it out. Maybe I should slow down and do a few more tutorials. Gain a little more knowledge.

Thanks Fielmann! This works perfect. I've learned a lot by referring to the manual so I can break down your code to find out how it all works.
 

Nidoking

Member
Presumably, you want to use angle_difference rather than subtracting angles, and you want to actually check what the difference is and only turn 2 degrees if you need to turn at least 2 degrees. Otherwise, this will make for a very wobbly path if the player's ship is always in the same direction from the enemy.
 

Roa

Member
It's funny, cause while wrapping the screen is what you want for bullets, you need to be smarter about the planes.

you actually want to check the distance of the plane, but not only that, but 4 cardinal directions equal to the size of the screen AND its 4 corners. Sort those 9 positions using the min function to find the shortest path.
When a plane crosses over the screen, just manually set him to the other side.
 
Thanks guys, I have been messing around with the min function among other things and managed to get the computer ship to wrap around when near the edge of the screen. So now the player can't cheat by escaping off screen and flanking the computer. Only thing is the computer is relentless, haha. I discovered switch statements last night. Going to try and use them to change up the computers tactics depending on the situation.
 
Top