Making an enemy shoot in the direction it is facing

N

nedius

Guest
Hi there,

I can find plenty of tutorials for how to make an enemy shoot if it facing left or right, or to shoot towards the player.

However, I'm making an R-Type style space shooter, and want an enemy to shoot in the direction it is facing at the time.

I only have Lite, so am using a rotated sprite for a laser beam. I can get it to shoot in the direction of the player, but one sort of enemy I want will follow a curving path, and shoot in the direction the sprite is facing as it travels along that curved path. It will drop down from the top of the screen, then curve round to the left of screen. So, initially, it will shoot downwards, then shoot at an angle, then eventually to the left. Aaaaand I want there to be more than one on the screen... 4 at a time, in fact, at least. I think that means I'll need four different version of the same enemy?

However, I'm not really sure where to begin... Please help!!

Thanks!
 
R

rui.rosario

Guest
I only have Lite
You're using GM 8.1? If so, I don't particularly remember the limitations it has.

Nonetheless, you have the image_angle variable that rotates the sprite image to the angle you specify it. This means that if you know the direction from the enemy's position to the player's position (point_direction) you can use that value as the image angle of the sprite and it will rotate accordingly. Do remember that 0 degrees is to the right, so you'll need to have your sprite facing to the right in order to the rotation to work accordingly.

Now, if using image_angle is out of question you have a number of different options:
  • Create a sprite with 360 sub-images (one per angle) and assign the index (image_index) according to the angle it should be facing
  • If you want to save on the image space you can define an angle error, for example, you can round the subimage's angle to a multiple of 5 and then you only need 72 subimages. Less if you increase the angle error. You can then assign the approximate image angle to the direction. It'll be a little off, but if the error is little then it'll be negligible;
  • If you don't want the subimage angle to differ from the actual shooting direction you can always round the direction itself to the same image angle error. However if the player is far away this can mean the shot will miss by a lot.
Hope this helps.
 
N

nedius

Guest
I don't really understand...

I can use image angle to get the laser to face its direction of travel.

What I want is to set the direction of travel based on the direction of the object that created it. If Obj_enemy1 is facing direction 'y', I want the laser it shoots to travel in direction 'y'.

How do I get the laser to take as its direction of travel the image_angle of the object that created it?
 
R

rui.rosario

Guest
I don't really understand...

I can use image angle to get the laser to face its direction of travel.

What I want is to set the direction of travel based on the direction of the object that created it. If Obj_enemy1 is facing direction 'y', I want the laser it shoots to travel in direction 'y'.

How do I get the laser to take as its direction of travel the image_angle of the object that created it?
Sorry, completely overlooked the main question :p

If you can already assign the image_angle to the direction of travel you can use a similar logic to assign it depending on the object that created it.
Look at the following code

Code:
var laser = instance_create(x, y, obj_laser);
laser.direction = direction;
laser.image_angle = direction;
This piece of code will create a new laser (an instance of obj_laser) at the position of the current object (assume this is running on the enemy instance) and stores a reference to the newly created object in the temporary laser variable. It then assigns the direction of motion (direction variable) and the direction of the sprite (image_angle variable) to be the same direction of motion of the current object. If you just want it to be the direction the current object's sprite is facing, then do something like:

Code:
var laser = instance_create(x, y, obj_laser);
laser.direction = image_angle;
laser.image_angle = image_angle;
Where the rationale is the same, it just replaces direction by image_angle.
 
N

nedius

Guest
Sorry, completely overlooked the main question :p

If you can already assign the image_angle to the direction of travel you can use a similar logic to assign it depending on the object that created it.
Look at the following code

Code:
var laser = instance_create(x, y, obj_laser);
laser.direction = direction;
laser.image_angle = direction;
This piece of code will create a new laser (an instance of obj_laser) at the position of the current object (assume this is running on the enemy instance) and stores a reference to the newly created object in the temporary laser variable. It then assigns the direction of motion (direction variable) and the direction of the sprite (image_angle variable) to be the same direction of motion of the current object. If you just want it to be the direction the current object's sprite is facing, then do something like:

Code:
var laser = instance_create(x, y, obj_laser);
laser.direction = image_angle;
laser.image_angle = image_angle;
Where the rationale is the same, it just replaces direction by image_angle.

So the laser needs a create event set variable 'laser' to 0?
 
R

rui.rosario

Guest
No, the laser variable is just a temporary variable in the instance the object is created from. It is used to hold the reference to the new laser. You don't need any special Create Event because you would be assigning the variables (direction and image_angle) directly from the instance that created it.

Just a question, are you using GML or DND?
 
N

nedius

Guest
you vredit me with too much knowledge... I would guyess GML
 
R

rui.rosario

Guest
(GML means you are writing code, like the one I posted, DND means you're using the Drag And Drop interface, the blocks)

How do you make the enemy shoot the player currently?


EDIT: You can also set the speed in the same fashion
Code:
var laser = instance_create(x, y, obj_laser);
laser.direction = image_angle;
laser.image_angle = image_angle;
laser.speed = 4; // Or any other value
 
N

nedius

Guest
normally just create a object_laser at x:0 y:0 relative to Object_enemy, with a set movement instruction in the create event - either a fixed move for the horizontal bullets, or a move towards player on the targets ones.

These enemies will create shots that will shoot in different directions as the enemy turns along its path
 

dannerz

Member
Normally I use something like this:

if sprite_index = shoot_left_jet_spr { var I; I=instance_ceate(x,y,laser_obj); I.hspeed = 5; }

you can also subsitute a path for a time based system, like a series of alarm[x].
 
Top