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

An enemy that follows me and attacks me when I am at a certain distance gmas 1.4

Imiglikos

Member
Hi everyone, I have an enemy who has several states, the first state when he flies left right, the second state when he sees me at a certain distance, he starts chasing me.
The third state, when they hero disappear from its range, the enemy returns to its starting position.
I have one enemy problem when chasing a player it happens that the enemy sprite it willbe freez place.
and similarly, when he kills the enemy, respawn should take place at the starting point and the enemy resurrects in the place where he died and his sprite freezes in place

this is what the enemy code looks like

obj_flying_enemy

create

GML:
hp=10;
dead=false;
timeDamage =0;
hspeed=2;
vspd = 0;
hspd = 0;
start_x = x;
start_y = y;
min_x=x
max_x=x

enum state {
    patrol,
    back
    wait
}
stat=state.wait

distance=640

step


GML:
image_speed=0.3

if hspeed>0

image_xscale=1
else
image_xscale=-1

if(instance_exists(obj_player) && abs(obj_player.x-x)<256 && point_distance(obj_player.x,obj_player.y,xstart,ystart)<distance && !collision_line(x,y,obj_player.x,obj_player.y-30,par_solid,1,1)) {
    stat=state.patrol
} else if(stat!=state.wait && point_distance(x,y,xstart,ystart)>16) {
    stat=state.back
} else {
    stat=state.wait
}

if(instance_exists(obj_player) and stat=state.patrol) {
    hspeed=sign(obj_player.x-x)*abs(hspeed)
    speed=clamp(speed+(5/room_speed),2,5)
    vspeed=(obj_player.y-y-32)/60
} else if(stat=state.back) {
    mp_potential_step(xstart,ystart,3,0)
    speed=clamp(speed-(2/room_speed),2,5)
} else {
    vspeed*=0.1
    speed=clamp(speed-(2/room_speed),2,5) 
    min_x=min(x,min_x)
    max_x=max(x,max_x)

if hspeed == 0 {
image_xscale *= -1;
}

//When an enemy is attacked, he blinks
if timeDamage > 0 {
visible = !visible;
  timeDamage -= 1;
} else {
visible = true;
}

if hp <= 0 {

instance_destroy();
dead=true;

with instance_create(x,y,obj_entity_die)  {
  sprite_index = spr_flying_enemy_dies;
  image_speed=2;
  physics = false;
  image_xscale = other.image_xscale;
  image_yscale = other.image_yscale;
  hspd = other.hspd;
  vspd = other.vspd;

  audio_play_sound(snd_enemy_dite, 1, false);
}
}


// respawn enemy
if (dead == true)
{


  var inst = instance_create(x, y, obj_respawn);
  inst.object_to_respawn = obj_flying_enemy;

}


  
}


collision with par_solid

GML:
hspeed*=-1;
collision with obj_brick //object reflecting the enemy

GML:
if(stat=state.wait) {
    hspeed*=-1
}


I also tried to add another fourth state when the enemy chases me (the enemy sprite changes) and the enemy shoots, but I did something wrong, because he was shooting while patrolling and he should shoot when he chases me.
of course, it should shoot left and right depending on which side the hero is on.
 
Last edited:

Imiglikos

Member
Hello everyone, at the outset I would like to wish everyone a Happy New Year :)

I managed to set the enemy to respawn where he started his patrol, I added location coordinates and it's ok.


Two things left

1.
I also tried to add another fourth state when the enemy chases me (enemy sprite changes) and the enemy shoots, but I did something wrong because he was shooting while patrolling and should have shot when chasing me.
obviously it should shoot left and right at the hero depending on which side the hero is on.

2.
When the enemy chases the hero and the hero stands still, the enemy goes crazy spinning left and right.
 

TheouAegis

Member
You are breaking up your states incorrectly.
if(instance_exists(obj_player) && abs(obj_player.x-x)<256 && point_distance(obj_player.x,obj_player.y,xstart,ystart)<distance && !collision_line(x,y,obj_player.x,obj_player.y-30,par_solid,1,1)) { stat=state.patrol
That should be in your waiting state, not running all the time in the Step event.

} else if(stat!=state.wait && point_distance(x,y,xstart,ystart)>16) { stat=state.back
This should be in your patrol state only. In addition, the way your code is set up, this would have run almost immediately every step. No player? Go back. Player horizontally more than 256 pixels away? Go back. Player more than 640 pixels away from start point along any axis? Go back. Ever a wall between Player and enemy? Go back.


if hspeed == 0 { image_xscale *= -1; }
This is just wrong. If hspeed is 0, you don't want to change image_xscale at all. If you want the enemy to look left and right at regular intervals, use a repeating timer to toggle image_xscale.
with instance_create(x,y,obj_entity_die) { sprite_index = spr_flying_enemy_dies; image_speed=2; physics = false; image_xscale = other.image_xscale; image_yscale = other.image_yscale; hspd = other.hspd; vspd = other.vspd; audio_play_sound(snd_enemy_dite, 1, false); } } // respawn enemy if (dead == true) { var inst = instance_create(x, y, obj_respawn); inst.object_to_respawn = obj_flying_enemy; }
Just put this in the Destroy Event. The dead variable is pointless.

You need to set speed to 0 when shooting if you don't want the enemy to move while shooting.
 

Imiglikos

Member
You are breaking up your states incorrectly.

That should be in your waiting state, not running all the time in the Step event.


This should be in your patrol state only. In addition, the way your code is set up, this would have run almost immediately every step. No player? Go back. Player horizontally more than 256 pixels away? Go back. Player more than 640 pixels away from start point along any axis? Go back. Ever a wall between Player and enemy? Go back.



This is just wrong. If hspeed is 0, you don't want to change image_xscale at all. If you want the enemy to look left and right at regular intervals, use a repeating timer to toggle image_xscale.

Just put this in the Destroy Event. The dead variable is pointless.

You need to set speed to 0 when shooting if you don't want the enemy to move while shooting.

At the moment I'm learning state machines and I'm trying to do something, as you can see there are bugs.
What should it look like properly?



1.
While chasing the player, it happens that the enemy sprite goes crazy and does not know whether to turn left or right, it happens when his sprite overlaps the hero's sprite

2.
When the enemy notices me and when he is chasing me, he should change the sprite and start shooting at me depending on which side the hero is from, left or right, and when I disappear from his sight, the enemy changes the sprite again, stops shooting and returns to his place


I fixed the third point. There was a problem that after respawning the enemy wouldn't go back to his patrol spot, I added his sprite's coordinates and it worked.
 

FrostyCat

Redemption Seeker
1: That is target jitter in a nutshell. See what I did in this post to deal with it in the context of move_towards_point.
2: That is basically the attack state shown in the video tutorial you posted, and sign(obj_player.x-x) would have told you which way the player is coming from. You are better off using what you clearly know already than waiting for rescue.
 

Imiglikos

Member
You are breaking up your states incorrectly.

That should be in your waiting state, not running all the time in the Step event.


This should be in your patrol state only. In addition, the way your code is set up, this would have run almost immediately every step. No player? Go back. Player horizontally more than 256 pixels away? Go back. Player more than 640 pixels away from start point along any axis? Go back. Ever a wall between Player and enemy? Go back.



This is just wrong. If hspeed is 0, you don't want to change image_xscale at all. If you want the enemy to look left and right at regular intervals, use a repeating timer to toggle image_xscale.

Just put this in the Destroy Event. The dead variable is pointless.

You need to set speed to 0 when shooting if you don't want the enemy to move while shooting.


GML:
    if hspeed == 0 { image_xscale *= -1; }

This is just wrong. If hspeed is 0, you don't want to change image_xscale at all. If you want the enemy to look left and right at regular intervals, use a repeating timer to toggle image_xscale.
1.
ie hspeed cannot be zero? how it should look correctly so that the enemy sprite, when overlapping the hero's sprite, does not go crazy spinning left and right?


GML:
    with instance_create(x,y,obj_entity_die) { sprite_index = spr_flying_enemy_dies; image_speed=2; physics = false; image_xscale = other.image_xscale; image_yscale = other.image_yscale; hspd = other.hspd; vspd = other.vspd; audio_play_sound(snd_enemy_dite, 1, false); } } // respawn enemy if (dead == true) { var inst = instance_create(x, y, obj_respawn); inst.object_to_respawn = obj_flying_enemy; }

Just put this in the Destroy Event. The dead variable is pointless.
2.
ok I managed to do it and now the enemy sprite respawns at the patrol site

3.
When the enemy notices me and when he is chasing me, he should change the sprite and start shooting at me depending on which side the hero is from, left or right, and when I disappear from his sight, the enemy changes the sprite again, stops shooting and returns to his place
 

TheouAegis

Member
1.
ie hspeed cannot be zero? how it should look correctly so that the enemy sprite, when overlapping the hero's sprite, does not go crazy spinning left and right?
No one said hspeed can't be 0. What I said is you shouldn't multiply image_xscale by -1 every time hspeed is 0, because that just makes the sprite spin constantly. Why are you even trying to set image_xscale when it's not moving? Your first line was correct: set image_xscale to the direction of movement. You can set image_xscale at the exact moment you set hspeed to 0 when attacking, sure, but that is a different code than what you have.

If the player is right over the enemy and NOT moving, there is absolutely no problem with facing the player, since the enemy will face the direction the player came from. However, if the layer is moving back and forth over the enemy, then yes, there will be ugly spinning, which is the fault of the player, not the enemy. If that is a concern for you, which it probably is, then you can increase the distance the player needs to be before turning to face the player. However, be aware that this effectively allows the player to pass behind the enemy briefly in most cases.
Code:
hspeed = sign((obj_player.x - x) div 8);
This would only move to the player if his origin is at least 8 pixels away from the enemy's origin. Another thing that was probably happening is your enemy moved faster than the distance to the player. The above suggestion would certainly help with that, since you could tell the enemy to only move if the player is at least 1 full step away.
 

Imiglikos

Member
So this is how it should look like below?




GML:
image_speed=0.3

if hspeed>0

image_xscale=1
else
image_xscale=-1

if(instance_exists(obj_player) && abs(obj_player.x-x)<256 && point_distance(obj_player.x,obj_player.y,xstart,ystart)<distance && !collision_line(x,y,obj_player.x,obj_player.y-30,par_solid,1,1)) {
    stat=state.patrol
} else if(stat!=state.wait && point_distance(x,y,xstart,ystart)>16) {
    stat=state.back
} else {
    stat=state.wait
}

if(instance_exists(obj_player) and stat=state.patrol) {
    hspeed = sign((obj_player.x - x) div 8)
    speed=clamp(speed+(5/room_speed),2,5)
    vspeed=(obj_player.y-y-32)/60
} else if(stat=state.back) {
    mp_potential_step(xstart,ystart,3,0)
    speed=clamp(speed-(2/room_speed),2,5)
} else {
    vspeed*=0.1
    speed=clamp(speed-(2/room_speed),2,5)
    min_x=min(x,min_x)
    max_x=max(x,max_x)

if hspeed == 0 {
image_xscale = -1;
}

I have one more question
Since I'm using image_xscale at the beginning


GML:
if hspeed>0

image_xscale=1
else
image_xscale=-1

does it make sense to use image_xscale at this point ???

GML:
if hspeed == 0 {
image_xscale = -1;
}
 
Last edited:

TheouAegis

Member
if hspeed>0 image_xscale=1 else image_xscale=-1
Oh I overlooked this part. You could opt to only run that if hspeed is not 0.
Code:
if hspeed != 0 {
    image_xscale = sign(hspeed);
}
So while yes, your previous code does make sense in that situation, those lines of code should be immediately together ‐ no other code between them. What if hspeed is greater than 0, but then the next block of code between them sets it to 0? Then even though you should be facing right, it would flip left.
 

Imiglikos

Member
Oh I overlooked this part. You could opt to only run that if hspeed is not 0.
Code:
if hspeed != 0 {
    image_xscale = sign(hspeed);
}
So while yes, your previous code does make sense in that situation, those lines of code should be immediately together ‐ no other code between them. What if hspeed is greater than 0, but then the next block of code between them sets it to 0? Then even though you should be facing right, it would flip left.
Sometimes the enemy sprite and starts spinning, maybe not as often as it was before, but it still hangs

What about the latter point?

I did something like below but it works not as I want. Now the enemy shoots all the time. The enemy should shoot left and right when there is a hero in his line of sight, but he should first change the idle patrol sprite to the attack sprite then create a missile and shoot into a hero and when the hero disappears from sight, he will stop shooting, change the attack sprite to the patrol sprite and return to the patrol site.


step

GML:
image_speed=0.3

if hspeed>0

image_xscale=1
else
image_xscale=-1

if(instance_exists(obj_player) && abs(obj_player.x-x)<256 && point_distance(obj_player.x,obj_player.y,xstart,ystart)<distance && !collision_line(x,y,obj_player.x,obj_player.y-30,par_solid,1,1)) {
    stat=state.patrol
} else if(stat!=state.wait && point_distance(x,y,xstart,ystart)>16) {
    stat=state.back
} else {
    stat=state.wait
}

if(instance_exists(obj_player) and stat=state.patrol) {
    hspeed = sign((obj_player.x - x) div 8)
    speed=clamp(speed+(5/room_speed),2,5)
    vspeed=(obj_player.y-y-32)/60
} else if(stat=state.back) {
    mp_potential_step(xstart,ystart,3,0)
    speed=clamp(speed-(2/room_speed),2,5)
} else {
    vspeed*=0.1
    speed=clamp(speed-(2/room_speed),2,5)
    min_x=min(x,min_x)
    max_x=max(x,max_x)



//enemy shoot

  
    time_counter--
    if( time_counter<=0)
        {
      i_shoot=false
        var i;
        i = instance_create(x, y, obj_bullet);//create bullet
         audio_play_sound(snd_bullet 1, false);
         i.speed = 10; //speed bullet
         i.hspeed=image_xscale*10;
        i_shoot=false
         time_counter=30
        }
} else {
    //Not shooting
     time_counter--
    if( time_counter<=0)
        {
        hspeed=2;
        i_shoot=true
         time_counter=120
        }
}


if hspeed != 0 {
    image_xscale = sign(hspeed);
}
 
Last edited:

Nidoking

Member
Your shoot code runs every time the state is not patrol or back. (There's also a check for if an instance of obj_player exists, but that's redundant because it can't reach the patrol state if there's no obj_player instance.) If that's not the condition under which you want the instance to shoot, then figure out what is the condition and put it there. Probably, you confused yourself about brackets, because everything else in that conditional appears to be related to movement.
 

TheouAegis

Member
Also where do you change the sprite to the shooting sprite? Have you done that yet? Typically when I need to change Sprites between states, I do so at the same time I change the state.

Also, you don't need to use the speed variable in your code. You are setting hspeed and/or vspeed directly, so speed is just an additional burden.
For example

i.hspeed=image_xscale*10;
i_shoot=false
The first line just sets hspeed to 10, which you then override on the next line by setting hspeed to ±10. Furthermore, although I overlooked it because I didn't see any issue with it at first, but it just occurred to me:
hspeed = sign((obj_player.x - x) div 8)
speed=clamp(speed+(5/room_speed),2,5)
vspeed=(obj_player.y-y-32)/60
This could potentially cause issues, because speed is technically (hspeed²+vspeed²)^½, so the value of vspeed on the previous step could potentially prevent hspeed from ever reaching 5, or it could even force vspeed to be 2 (which doesn't matter because you set vspeed on the next line, which is why I overlooked it).
 

Imiglikos

Member
Also where do you change the sprite to the shooting sprite? Have you done that yet? Typically when I need to change Sprites between states, I do so at the same time I change the state.

Also, you don't need to use the speed variable in your code. You are setting hspeed and/or vspeed directly, so speed is just an additional burden.
For example


The first line just sets hspeed to 10, which you then override on the next line by setting hspeed to ±10. Furthermore, although I overlooked it because I didn't see any issue with it at first, but it just occurred to me:

This could potentially cause issues, because speed is technically (hspeed²+vspeed²)^½, so the value of vspeed on the previous step could potentially prevent hspeed from ever reaching 5, or it could even force vspeed to be 2 (which doesn't matter because you set vspeed on the next line, which is why I overlooked it).

Then how would it have to be done to fix the spinning of the enemy sprite?

in the picture example, I showed an example of a place where the hero will be and the enemy starts spinning left and right


enemy bird and hero.png
 
Last edited:

Imiglikos

Member
Sounds like this conditional is the culprit. Comment it out (use /* */ to comment out part of a line of code rather than a whole line) and see if the spin issue goes away for now.

I recommended this snippet but there is still a problem with the enemy sprite, he goes crazy not knowing if he should turn left or right


step

GML:
image_speed=0.3

if(instance_exists(obj_player) && abs(obj_player.x-x)<256 && point_distance(obj_player.x,obj_player.y,xstart,ystart)<distance /*&& !collision_line(x,y,obj_player.x,obj_player.y-30,par_solid,1,1)*/ ) {
    stat=state.patrol
} else if(stat!=state.wait && point_distance(x,y,xstart,ystart)>16) {
    stat=state.back
} else {
    stat=state.wait
}

if(instance_exists(obj_player) and stat=state.patrol) {
    hspeed = sign((obj_player.x - x) div 8)
    speed=clamp(speed+(5/room_speed),2,5)
    vspeed=(obj_player.y-y-32)/60
} else if(stat=state.back) {
    mp_potential_step(xstart,ystart,3,0)
    speed=clamp(speed-(2/room_speed),2,5)
} else {
    vspeed*=0.1
    speed=clamp(speed-(2/room_speed),2,5)
    min_x=min(x,min_x)
    max_x=max(x,max_x)



//enemy shoot


    time_counter--
    if( time_counter<=0)
        {
      i_shoot=false
        var i;
        i = instance_create(x, y, obj_bullet);//create bullet
         audio_play_sound(snd_bullet 1, false);
         i.speed = 10; //speed bullet
         i.hspeed=image_xscale*10;
        i_shoot=false
         time_counter=30
        }
} else {
    //Not shooting
     time_counter--
    if( time_counter<=0)
        {
        hspeed=2;
        i_shoot=true
         time_counter=120
        }
}

if hspeed>0
   image_xscale=1
else
   image_xscale=-1

if hspeed != 0 {
    image_xscale = sign(hspeed);
}
 

TheouAegis

Member
I would debug your variables then. Check the value of distance. Also track stat. I have an aching suspicion that stat is alternating rapidly between patrol and back.
 

Imiglikos

Member
I would debug your variables then. Check the value of distance. Also track stat. I have an aching suspicion that stat is alternating rapidly between patrol and back.


Or maybe it is the fault of an incorrectly selected value of the enemy patrolling range?
I will change the value from 640 to 512


create


GML:
distance=640


GML:
distance=512
 

Imiglikos

Member
I would like to have such an effect When the enemy notices me and when he is chasing me, he should change the sprite and start shooting at me depending on which side the hero is from, left or right, and when I disappear from his sight, the enemy changes the sprite again, stops shooting and returns to his place

My solution with shooting a hero is wrong because enemy bird shoots all the time and he should only shoot left and right when he is chasing the hero. He should also change his sprite to the attack sprite during the attack


obj_bird

step

GML:
image_speed=0.3

if(instance_exists(obj_player) && abs(obj_player.x-x)<256 && point_distance(obj_player.x,obj_player.y,xstart,ystart)<distance /*&& !collision_line(x,y,obj_player.x,obj_player.y-30,par_solid,1,1)*/ ) {
    stat=state.patrol
} else if(stat!=state.wait && point_distance(x,y,xstart,ystart)>16) {
    stat=state.back
} else {
    stat=state.wait
}

if(instance_exists(obj_player) and stat=state.patrol) {
    hspeed = sign((obj_player.x - x) div 8)
    speed=clamp(speed+(5/room_speed),2,5)
    vspeed=(obj_player.y-y-32)/60
} else if(stat=state.back) {
    mp_potential_step(xstart,ystart,3,0)
    speed=clamp(speed-(2/room_speed),2,5)
} else {
    vspeed*=0.1
    speed=clamp(speed-(2/room_speed),2,5)
    min_x=min(x,min_x)
    max_x=max(x,max_x)



//enemy shoot


    time_counter--
    if( time_counter<=0)
        {
      i_shoot=false
        var i;
        i = instance_create(x, y, obj_bullet);//create bullet
         audio_play_sound(snd_bullet 1, false);
         i.speed = 10; //speed bullet
         i.hspeed=image_xscale*10;
        i_shoot=false
         time_counter=30
        }
} else {
    //Not shooting
     time_counter--
    if( time_counter<=0)
        {
        hspeed=2;
        i_shoot=true
         time_counter=120
        }
}

if hspeed>0
   image_xscale=1
else
   image_xscale=-1

if hspeed != 0 {
    image_xscale = sign(hspeed);
}
 

Imiglikos

Member
Yes correct shooting should be in attack state i am aware of this but i am doing something wrong I out but stuck with this problem.
 

Imiglikos

Member
I already mentioned this at the beginning of my post, you repeated my words.
I know that there should be another state. I was already getting to it, but I am doing something wrong and that's why I ask how to correctly implement it in the code above.

The patrol status is responsible for the attack, but this attack is a chase after the hero and I wanted to achieve the same effect as I described, but I do not know how to implement it

Obj_bird

Step

GML:
image_speed=0.3

if(instance_exists(obj_player) && abs(obj_player.x-x)<256 && point_distance(obj_player.x,obj_player.y,xstart,ystart)<distance /*&& !collision_line(x,y,obj_player.x,obj_player.y-30,par_solid,1,1)*/ ) {
    stat=state.patrol
} else if(stat!=state.wait && point_distance(x,y,xstart,ystart)>16) {
    stat=state.back
} else {
    stat=state.wait
}

if(instance_exists(obj_player) and stat=state.patrol) {
    hspeed = sign((obj_player.x - x) div 8)
    speed=clamp(speed+(5/room_speed),2,5)
    vspeed=(obj_player.y-y-32)/60
} else if(stat=state.back) {
    mp_potential_step(xstart,ystart,3,0)
    speed=clamp(speed-(2/room_speed),2,5)
} else {
    vspeed*=0.1
    speed=clamp(speed-(2/room_speed),2,5)
    min_x=min(x,min_x)
    max_x=max(x,max_x)



//enemy shoot


    time_counter--
    if( time_counter<=0)
        {
      i_shoot=false
        var i;
        i = instance_create(x, y, obj_bullet);//create bullet
         audio_play_sound(snd_bullet 1, false);
         i.speed = 10; //speed bullet
         i.hspeed=image_xscale*10;
        i_shoot=false
         time_counter=30
        }
} else {
    //Not shooting
     time_counter--
    if( time_counter<=0)
        {
        hspeed=2;
        i_shoot=true
         time_counter=120
        }
}

if hspeed>0
   image_xscale=1
else
   image_xscale=-1

if hspeed != 0 {
    image_xscale = sign(hspeed);
}
 
Last edited:

Nidoking

Member
I know that there should be another state. I was already getting to it, but I am doing something wrong and that's why I ask how to correctly implement it in the code above.
Well, one thing wrong would be that you should have another state. You can't wait until the problem has been solved to solve the problem.

The patrol status is responsible for the attack
But you have explicitly put the attacking part in the wait state - or the patrol state, if there is no obj_player. While it seems a bit strange to me to have enemies attack if there is no player, that is what you have done. Check your curly braces more carefully. Your indentation should be a clue that something is not working the way you want it to.
 

Imiglikos

Member
I would debug your variables then. Check the value of distance. Also track stat. I have an aching suspicion that stat is alternating rapidly between patrol and back.

So I can use the patrol state as a shooting attack? and put a block of code with shooting there or maybe add another fourth state of attack?


obj_bird

step


GML:
image_speed=0.3

if(instance_exists(obj_player) && abs(obj_player.x-x)<256 && point_distance(obj_player.x,obj_player.y,xstart,ystart)<distance /*&& !collision_line(x,y,obj_player.x,obj_player.y-30,par_solid,1,1)*/ ) {
    stat=state.patrol
} else if(stat!=state.wait && point_distance(x,y,xstart,ystart)>16) {
    stat=state.back
} else {
    stat=state.wait
}

if(instance_exists(obj_player) and stat=state.patrol) {
    hspeed = sign((obj_player.x - x) div 8)
    speed=clamp(speed+(5/room_speed),2,5)
    vspeed=(obj_player.y-y-32)/60
} else if(stat=state.back) {
    mp_potential_step(xstart,ystart,3,0)
    speed=clamp(speed-(2/room_speed),2,5)
} else {
    vspeed*=0.1
    speed=clamp(speed-(2/room_speed),2,5)
    min_x=min(x,min_x)
    max_x=max(x,max_x)



//enemy shoot


    time_counter--
    if( time_counter<=0)
        {
      i_shoot=false
        var i;
        i = instance_create(x, y, obj_bullet);//create bullet
         audio_play_sound(snd_bullet 1, false);
         i.speed = 10; //speed bullet
         i.hspeed=image_xscale*10;
        i_shoot=false
         time_counter=30
        }
} else {
    //Not shooting
     time_counter--
    if( time_counter<=0)
        {
        hspeed=2;
        i_shoot=true
         time_counter=120
        }
}

if hspeed>0
   image_xscale=1
else
   image_xscale=-1

if hspeed != 0 {
    image_xscale = sign(hspeed);
}
 

Nidoking

Member
The block of code you have for shooting is in the wait state, not the patrol state. You keep pasting exactly the same code without any changes. It won't do anything different if you don't change it, and what it does is do the shooting action in the wait state only. Please understand this. You cannot progress in developing your game if you don't understand this. That is a fact.
 

TheouAegis

Member
Even if you could do the attack in another state, why would you want to? You are already using States oh, so just make an attack State and when you have the enemy decide to actually attack the player, switch to that state. If the enemy is attacking, he's obviously not still patrolling. If the enemy is attacking, he's obviously not waiting for the player to move into range.
 
Top