• 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 Shooting Help

B

BigLaw Games

Guest
Hey, I'm working on a platforming shooter. Currently I'm trying to get enemies to shoot the player automatically. I found some really good code on this form that should work. However, it only shoots one bullet after the enemy dies after the death animation plays.

I want it to work where he shots the player when he is in range automatically, not after when the enemy dies. The step event code is below. Any help is greatly appreciated.


hsp = dir * movespeed;
vsp += grav;

// Enemy Shooting
max_range = 300
range = distance_to_object (obj_player)
if (range <= max_range)
{

bullet1 = instance_create(x,y,obj_enemy_bullet)
bullet1.direction = point_direction(x,y,obj_player.x,obj_player.y)
bullet1.image_angle = bullet1.direction
bullet1.speed = 5

}


// Hit Points & Dealth
if (hp == 0)
{
instance_change(obj_enemy_dealth,false);

}

Thanks
Derek
 
N

NoireRogue

Guest
I was confused as to why this wasn't working for you so I tested it out on my end and it works just fine (though it shoots an endless stream of bullets when I'm in range). Maybe there's something wrong with the rest of the code?
 
A

Aura

Guest
What is hp set to? I see no code that handles it. If it is set to 0, then only a single bullet would be created and the enemy would die. Try setting it to a larger value or if there's already some code, please post it.
 
T

Ting_Thing

Guest
I was confused as to why this wasn't working for you so I tested it out on my end and it works just fine (though it shoots an endless stream of bullets when I'm in range). Maybe there's something wrong with the rest of the code?
I agree, it looks fine to me. Perhaps the bullet is programmed to remove when it touches any instance? In that case it would be removed right away because it is created over the enemy.

Something else I noticed. You have:

max_range = 300
range = distance_to_object (obj_player)
if (range <= max_range)


Why not just do this?:

if (distance_to_object (obj_player) <= 300)

It would do the exact thing with less wordiness in your code.
 
Top