Making Enemy Shoot at Player

P

Parzival

Guest
Hi. I'm using Drag and Drop and am new to coding so I'm having some trouble with this.

I've created an enemy called obj_enemy_dropper that that bounces off walls. I've tried looking through tons of threads and watch countless videos but I can't seem to find out how to make the bullet shoot at my player.

There seem to be two methods in doing this:
1. Using test chance and create moving instance. However, these functions were for the old gamemaker and don't work in GMS 2.
2. Creating the bullet, setting its direction as towards the player, and making it go at the player at some rate using alarms. I am using this method, below is my code.

CREATE:
alarm[1]=room_speed*5

Alarm 1:
var bullet = instance_create(x, y, obj_enemy_bullet);
{
direction = point_direction(x, y, obj_ship.x, obj_ship.y);
speed = 6;
}
alarm[0] = room_speed * 5;

When I try to run this, I get this compile error:
Object: obj_enemy_dropper Event: Alarm 1 at line 5: unknown function or script instance_create.

p.s. In my obj_enemy_bullet, there are variables that give my enemy_bullet a speed of 6, and that destroy the bullet if they leave the room.
 

Amon

Member
Are you using GMS2?

If you are then you need to change instance_create to instance_create_layer(x,y,"NAMEOFLAYER", obj_enemy_bullet);
 
P

Parzival

Guest
Are you using GMS2?

If you are then you need to change instance_create to instance_create_layer(x,y,"NAMEOFLAYER", obj_enemy_bullet);
Yes, I am using GMS2. Would the name of the layer just be "Instances" ?
 
P

Parzival

Guest
Thanks for helping by the way, really appreciate it.

So my game is running fine, just no enemy_bullet is being spawned. Any thought on why this might be happening?
 

Amon

Member
Are you updating the bullets location in the step event? the x/y in instance create is where the bullets will be created. Are you using another object to manage your bullets? If so have you moved that object in to the room?

Seeing some code will help. :)

edit

What object are the x/y values in point_direction using? just putting x/y will not work.
 
P

Parzival

Guest
Hi,
No object controls enemy_bullet

My obj_enemy_bullet code looks like this:
CREATE:
Screen Shot 2020-05-24 at 2.03.22 PM.png
OUTSIDE ROOM:
Screen Shot 2020-05-24 at 2.03.57 PM.png

For context, here is what my game looks like. The ship on the left that is bigger is my obj_enemy_dropper and the triangle is my player, obj_ship
Screen Shot 2020-05-24 at 2.09.31 PM.png

obj_enemy_dropper is grey because I make it flash using image alpha when hit by a bullet.


I just tested my game again, and it seems that the alarm that goes off every room_speed*5 is acting on obj_enemy_dropper, seeing that after 5 seconds, the ship rapids increases it's speed after 5 seconds. This is odd because my code is intended to have a bullet be shot from the obj_enemy_dropper.
 

Attachments

FrostyCat

Redemption Seeker
You haven't set the speed of the bullet in your Alarm 1 code, you've only set the speed of the thing calling the code. The conspicuous lack of bullet in the code that followed should have been a major red flag.

Either do this:
GML:
with (instance_create_layer(x, y, layer, obj_enemy_bullet))
{
    direction = point_direction(x, y, obj_ship.x, obj_ship.y);
    speed = 6;
}
Or this:
GML:
var bullet = instance_create_layer(x, y, layer, obj_enemy_bullet);
bullet.direction = point_direction(x, y, obj_ship.x, obj_ship.y);
bullet.speed = 6;
Also, are you sure you mean to be setting alarm[0] afterwards?
 
Top