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

how to create an enemy that will me attack when I get close to him

Imiglikos

Member
good morning everyone


I am struggling with such an issue

How to create an enemy that will me attack when I get close to him?

in more detail:


I have a drawn enemy, it is a tiger. That tiger is placed in a cage
when I get close to him, he should change the rest state of the sprite to the attack sprite and attack me and throw me from the cage so that I cannot climb on it
And now the most important thing is how to make this tiger receive a bone of meat from me, which I find on the stage and then , it will stop attacking me ...and take the bone from me and change state sprite to a bone-eating sprite

thank you for every suggestion and help.
 
Last edited:
GML:
if(point_distance(player.x,player.y,tiger.x.tiger.y) < 128)
{
    //ATTACK
}
if(point_distance(player.x,player.y,tiger.x.tiger.y) < 32&&hasBone = true)
{
    //Give tiger bone
}
 

FoxyOfJungle

Kazan Games
I think this is better, because I will only call point_distance() once, you will get more performance (although it doesn't make much difference, but if I have about 100 objects it can do a little)

GML:
var _dist = point_distance(player.x, player.y, tiger.x. tiger.y);

if (_dist < 128)
{
    //ATTACK
}
else
if (_dist < 32 && hasBone)
{
    //GIVE TIGER BONE
}
You can use enumerators to make the tiger's state machine, changing the sprites according to a different action.
 
Last edited:

Imiglikos

Member
Thank you very much for your help
Will the tiger now throw me out of the cage when I approach him?

I illustrated it in the picture below what effect I would like to achieve ..





Yes, I can have many objects on the stage, so a more efficient solution will be better to implement

ok, I still have a question ..

1.The hasBone variable should also be implemented in the object the bone that I find on the stage?


2.And how to make a icon bone appear in the upper right corner when I take it and disappear from the screen when I give it to the tiger?

obj_bones

create

GML:
hasBone = false

collision with the hero

GML:
hasBone = true;
Instance_destroy()
draw_sprite(spr_bones_icon, 0, x, y);

obj_tiger

create


step


GML:
var _dist = point_distance(player.x, player.y, tiger.x. tiger.y);

if (_dist < 128)
{
    sprite_index=spr_tiger_attack
    hp=-5//damage the enemy deals to the hero when he attacks him
}

if (_dist < 32 && hasBone=true)
{
    sprite_index=spr_tiger_eat_bones
}
 
Last edited:

Imiglikos

Member
What could be wrong that the tiger doesn't not attack me when I get close to it? looks like

Point_distance doesn't work.

Maybe i need used to use a different function? only then what place_meeting? instance_nearest?

Should attack me and throw me off the ladder as he tries to climb up the ... obj_ladder


As I described in the picture below



I'm using gms version 1.4.9999




currently this is my code in obj_tiger


obj_tiger

create


GML:
hasBone = false
image_speed =1;
step


GML:
var _dist = point_distance(player.x, player.y, tiger.x, tiger.y);

if (_dist < 128)
{
    sprite_index=spr_tiger_attack
    hp=-5//damage the enemy deals to the hero when he attacks him
}

if (_dist < 32 && hasBone=true)
{
    sprite_index=spr_tiger_eat_bones
}

obj_bones

create



GML:
hasBone = false
collision with the hero


GML:
hasBone = true;
Instance_destroy()
draw_sprite(spr_bones_icon, 0, x, y);
thank you for any help
 
Last edited:

pixeltroid

Member
I'm not much of coder but shouldn't there be some collision between the tiger and the player in order to cause damage to player? place_meeting?

Code:
var _dist = point_distance(player.x, player.y, tiger.x. tiger.y);

if (_dist < 128)
{
sprite_index=spr_tiger_attack
hp=-5//damage the enemy deals to the hero when he attacks him
}
Shouldn't it be "hp -=5", instead of "hp=-5"?

If I were to do it, I'd cause the tiger to move a bit when I get too close to it. And then if it collides with player, I'd reduce players hp in the object "obj_player" NOT "obj_tiger".
 

Imiglikos

Member
I'm not much of coder but shouldn't there be some collision between the tiger and the player in order to cause damage to player? place_meeting?

Code:
var _dist = point_distance(player.x, player.y, tiger.x. tiger.y);

if (_dist < 128)
{
sprite_index=spr_tiger_attack
hp=-5//damage the enemy deals to the hero when he attacks him
}
Shouldn't it be "hp -=5", instead of "hp=-5"?

If I were to do it, I'd cause the tiger to move a bit when I get too close to it. And then if it collides with player, I'd reduce players hp in the object "obj_player" NOT "obj_tiger".

yes, it should be in a collision with the player, I have already corrected it..I have a parent for a group of enemies that have a certain amount of energy to take ...
and thanks for the correction ... that's right, this is how the variable should look like hp -=5.

collision with obj_hero


GML:
if damageClock < 0
{
hp-=5
damageClock=room_speed*2
}

what do you mean here exactly? tries to solve it, but I don't know why the tiger doesn't attack and knock the player from the cage ...


GML:
If I were to do it, I'd cause the tiger to move a bit when I get too close to it. And then if it collides with player, I'd reduce players hp in the object "obj_player" NOT "obj_tiger".
 

pixeltroid

Member
yes, it should be in a collision with the player, I have already corrected it..I have a parent for a group of enemies that have a certain amount of energy to take ...
and thanks for the correction ... that's right, this is how the variable should look like hp -=5.

collision with obj_hero


GML:
if damageClock < 0
{
hp-=5
damageClock=room_speed*2
}

what do you mean here exactly? tries to solve it, but I don't know why the tiger doesn't attack and knock the player from the cage ...


GML:
If I were to do it, I'd cause the tiger to move a bit when I get too close to it. And then if it collides with player, I'd reduce players hp in the object "obj_player" NOT "obj_tiger".

I'm saying, if the player gets close to the tiger object, the tiger object would move up and collide with the player and then reduce his hp.

I don't know why your code isn't working.

I use this to code for some of my enemies that attack when I'm close:

Code:
if (point_distance(x, y, obj_hero.x, obj_hero.y) <128) //checks if distance of hero is <128
{
move_towards_point(obj_hero.x, obj_hero.y, 2); //moves towards player at the speed of "2"
}
else
{
speed = 0;
}
So if I am 128 pixels from the enemy, the enemy is activated and moves towards me. You might need to change the code so the tiger goes back if I am more than 128 pixels away from it.
 
Last edited:

Imiglikos

Member
I'm saying, if the player gets close to the tiger object, the tiger object would move up and collide with the player and then reduce his hp.

I don't know why your code isn't working.

I use this to code for some of my enemies that attack when I'm close:

Code:
if (point_distance(x, y, obj_hero.x, obj_hero.y) <128) //checks if distance of hero is <128
{
move_towards_point(obj_hero.x, obj_hero.y, 2); //moves towards player at the speed of "2"
}
else
{
speed = 0;
}
So if I am 128 pixels from the enemy, the enemy is activated and moves towards me. You might need to change the code so the tiger goes back if I am more than 128 pixels away from it.



tiger does nothing special when he is resting he has to sit in the cage .. and wait for the hero to approach him .. then he is to be attacked hero

I also wonder why this code does not work ... the distance at which the hero's tiger is to attack is rather good ...

now it looks like the rabbit is attacking all the time ... as if its animation was looping ... it should rest first ... and only when I get close enough to the cage and start climbing the ladder obj_ladder should attack me and throw me down from the cage ... the same as if I was on the roof of the cage ... I described it in the picture in previous messages


 
Last edited:

pixeltroid

Member
your enemy attack code just changes the sprite

if (_dist < 128)
{
sprite_index=spr_tiger_attack
hp=-5//damage the enemy deals to the hero when he attacks him
}


does the sprite change to spr_tiger_attack when the player gets close to it?
 

Imiglikos

Member
the sprite in the object obj_tygrys is set to idle ... Even though it is set to idle, when I start the game, the tiger attacks all the time without stopping ... even though I am a hero far from him

he should attack when I am next to the cage and preferably by the ladder next to the cage and I will climb it a bit ... then the tiger should attack me and push me off the cage and the ladder

I described it in the picture below ..


 

pixeltroid

Member
Can you post ALL the code for your tiger object? You're saying it should do this and that, but I'm not seeing any code for those actions.

If your tiger is attacking even if youre far from him, then it means the attack code is working and you just need to make it so that he attacks only when youre near to him.
 

Imiglikos

Member
Can you post ALL the code for your tiger object? You're saying it should do this and that, but I'm not seeing any code for those actions.

If your tiger is attacking even if youre far from him, then it means the attack code is working and you just need to make it so that he attacks only when youre near to him.


this is my entire code in previous news ..
it turns out that i don't have the complete code for the obj_tiger step event

Obj_tiger
Step


GML:
var _dist = point_distance(player.x, player.y, tiger.x, tiger.y);

if (_dist < 128)
{
    sprite_index=spr_tiger_attack
 
}

if (_dist < 32 && hasBone=true)
{
    sprite_index=spr_tiger_eat_bones
}

so here's the problem ..
I would have to use the distance_to_point function
 

FrostyCat

Redemption Seeker
so here's the problem ..
I would have to use the distance_to_point function
No, you don't. You have to learn the difference between objects and instances.
Corollary: NEVER set or use an instance's own variables with object.variable. An instance's own variables can be referenced as-is without dot prefixes. DO NOT use self. If multiple instances of the object exists, you might end up setting the value for all instances or for some other instance (depending on the export).
pixeltroid has already shown you what to do with point_distance in post #10. The code in the tiger object SHOULD NOT reference its own coordinates as tiger.x and tiger.y, only as x and y. And if you want the tiger to move, you need to explicitly add the movement code. The software will NOT auto-complete this for you.
GML:
var _dist = point_distance(x, y, player.x, player.y);
if (_dist < 128)
{
    sprite_index = spr_tiger_attack;
    mp_potential_step(player.x, player.y, 4, true);
}
else if (_dist < 32 && hasBone)
{
    sprite_index  =spr_tiger_eat_bones;
}
Remember: Computers do what you say, not what you want, especially when you say the wrong things.
Stop expecting the software to fill in or correct what you neglect to say.
 

Imiglikos

Member
No, you don't. You have to learn the difference between objects and instances.

pixeltroid has already shown you what to do with point_distance in post #10. The code in the tiger object SHOULD NOT reference its own coordinates as tiger.x and tiger.y, only as x and y. And if you want the tiger to move, you need to explicitly add the movement code. The software will NOT auto-complete this for you.
GML:
var _dist = point_distance(x, y, player.x, player.y);
if (_dist < 128)
{
    sprite_index = spr_tiger_attack;
    mp_potential_step(player.x, player.y, 4, true);
}
else if (_dist < 32 && hasBone)
{
    sprite_index  =spr_tiger_eat_bones;
}
Remember: Computers do what you say, not what you want, especially when you say the wrong things.
Stop expecting the software to fill in or correct what you neglect to say.




ok, I added a sprite change when Hero away from the tiger so that it wouldn't attack me non-stop ..
Thanks a lot for the interesting article ... I still have a question

I don't know how to set the tiger to start attacking the player when he climbs the ladder at the cage ...
not when he gets close to him ...

and the second thing ... I know that my colleague described above as if he did it ... it's about the move_towards_point function

to make the tiger throw me off the ladder every time I come ... Unfortunately this solution does not work ... I don't want to move the player a few pixels


it should work that way. The hero is close to the tiger ... but the tiger will attack the hero when we start climbing the ladder (obj_ladder) and throw us off the ladder ...
also if i am above the tiger on the cage roof (obj_cage) the tiger should attack me and drop me from the cage
in these two cases ..


as described in the attached drawing below ..

obj_tiger

step

GML:
var _dist = point_distance(x, y, player.x, player.y);
if (_dist < 128)
{
    
sprite_index = spr_tiger_attack;
mp_potential_step(player.x, player.y, 4, true);
}


else if (_dist > 128)
{
    sprite_index  =spr_tiger_idle;
}

else if (_dist < 32 && hasBone=true)
{
    
sprite_index  =spr_tiger_eat_bones;

}
 

pixeltroid

Member
I don't know how to set the tiger to start attacking the player when he climbs the ladder at the cage ...
not when he gets close to him ...
What is the distance between the tiger and the ladder?
Let's say it's 256 pixels. So just have the tiger's step event code say "if hero is 256 pixels away, start attacking"


The hero is close to the tiger ... but the tiger will attack the hero when we start climbing the ladder (obj_ladder) and throw us off the ladder ...
also if i am above the tiger on the cage roof (obj_cage) the tiger should attack me and drop me from the cage
in these two cases ..
If you want the player to fall off from the ladder and cage, you need to add the "falling" code to your hero object. Add it in the collision event between the hero and the tiger.

Can you post your collision code between the hero and the tiger?
 

Imiglikos

Member
What is the distance between the tiger and the ladder?
Let's say it's 256 pixels. So just have the tiger's step event code say "if hero is 256 pixels away, start attacking"




If you want the player to fall off from the ladder and cage, you need to add the "falling" code to your hero object. Add it in the collision event between the hero and the tiger.

Can you post your collision code between the hero and the tiger?


GML:
What is the distance between the tiger and the ladder?
Let's say it's 256 pixels. So just have the tiger's step event code say "if hero is 256 pixels away, start attacking"

but that's not the point..He is not supposed to attack when I'm on the ground..I wrote it wrong..Tiger is supposed to attack the player when the player is close to the tiger when he starts climbing the ladder (obj_ladder) .. and when he is on the roof of the cage that is (obj_cage)
as in the picture below, I described it. The mere setting of the distance will not do me a lot ... because in this way it will prevent me from passing the hero behind the cage with the tiger ...





GML:
If you want the player to fall off from the ladder and cage, you need to add the "falling" code to your hero object. Add it in the collision event between the hero and the tiger.

Can you post your collision code between the hero and the tiger?

I do not have this code in the hero tiger collision yet.
 

pixeltroid

Member
but that's not the point..He is not supposed to attack when I'm on the ground..I wrote it wrong..Tiger is supposed to attack the player when the player is close to the tiger when he starts climbing the ladder (obj_ladder) .. and when he is on the roof of the cage that is (obj_cage)
as in the picture below, I described it. The mere setting of the distance will not do me a lot ... because in this way it will prevent me from passing the hero behind the cage with the tiger ...
ok. So how many pixels is "when the player is close to the tiger"? Because you'll have to specify that number in the code.

Also, you say the tiger needs to attack when hero is
1) On the ladder
2) On the roof
So you will have to specify both conditions.

One easy way of doing it is to create 2 trigger objects. Place one on the ladder and one on the roof. So when player touches them, it will cause the tiger to attack.

In hero step event....

//make tiger attack when hero is on ladder
if place_meeting(x,y,obj_trigger_ladder) {
with obj_tiger {
//Tiger attack code
}
}

//make tiger attack when hero is on roof
if place_meeting(x,y,obj_trigger_roof) {
with obj_tiger {
//Tiger attack code
}
}
 

pixeltroid

Member
Imiglikos

I'm not an expert at programming. But I think to solve your problem you need to break it down into small pieces.

1. WHEN does the tiger attack?
When hero is on ladder / the roof he will touch a trigger object.
(in obj_hero, add collision with trigger that will cause a reaction in obj_tiger -- it's state changes to 'attack').

2. HOW the tiger attacks?
When hero touches trigger object the tiger moves near the hero
(in obj_tiger, add a code to make tiger move, or create an attack object that will hurt the hero)

3. WHAT happens when tiger collides with hero?

The hero's hp will be reduced and he will fall off.
(in obj_hero, reduce hp by 5 and add a movement code that will make him fall off).

The problem I'm seeing right now is that your code only changes the sprite of the tiger.
 

pixeltroid

Member
Imiglikos

I used the method I described above to 'solve' your tiger/cage problem. I used my existing project and a dummy tiger sprite.



As you see, the hero touches the pink box (obj_trigger) and it causes the tiger to attack by creating a large blue box (obj_tigerattack) which appears and disappears very quickly. When the hero touches the obj_tigerattack he loses 8 hp and falls off the cage. It creates the ILLUSION that the tiger is attacking the hero. Note that the obj_trigger and obj_tigerattack will be invisible.

I don't have a ladder in my project so I haven't shown it. But it's the same principle -- place obj_trigger on the ladder, and when the hero touches it, the tiger will attack him by creating obj_tigerattack which will knock him off the ladder.

I can give you the relevant codes to implement the above actions, but because I have used my own method and codes, you may need to change some code in your project. :)
 
Last edited:

Imiglikos

Member
Imiglikos

I used the method I described above to 'solve' your tiger/cage problem. I used my existing project and a dummy tiger sprite.



As you see, the hero touches the pink box (obj_trigger) and it causes the tiger to attack by creating a large blue box (obj_tigerattack) which appears and disappears very quickly. When the hero touches the obj_tigerattack he loses 8 hp and falls off the cage. It creates the ILLUSION that the tiger is attacking the hero. Note that the obj_trigger and obj_tigerattack will be invisible.

I don't have a ladder in my project so I haven't shown it. But it's the same principle -- place obj_trigger on the ladder, and when the hero touches it, the tiger will attack him by creating obj_tigerattack which will knock him off the ladder.

I can give you the relevant codes to implement the above actions, but because I have used my own method and codes, you may need to change some code in your project. :)


Thank you very much
pixeltroid

for your help .. please if you can send it to me ...

the ladder will be solved by using your solution as you mentioned .. I will only set the y coordinate to give the illusion that when the player "starts to climb" the tiger will start attacking him.:)
 

pixeltroid

Member
hey Imiglikos here is the code I used:


obj_tiger

create:

Code:
isattacking = false
step:

Code:
if isattacking = true {

if !instance_exists (obj_tigerattack) {
    instance_create(x,y-32,obj_tigerattack)
}
}
--------------------------------------------------------------------------------

obj_tigerattack

create:

Code:
alarm [0] = 3
alarm 0:
Code:
instance_destroy();
collision with hero:

Code:
with obj_hero {
health -= 8
}
--------------------------------------------------------------------------------

obj_hero

step event:

Code:
if place_meeting(x,y,obj_triggertiger)
{
with obj_tiger {
isattacking = true
}
}

else with obj_tiger {
isattacking = false
}
-------------------------------------------------------------------------------

obj_trigger

This object has no code. But if hero touches, it the tiger will react.
 

Imiglikos

Member
hey Imiglikos here is the code I used:


obj_tiger

create:

Code:
isattacking = false
step:

Code:
if isattacking = true {

if !instance_exists (obj_tigerattack) {
    instance_create(x,y-32,obj_tigerattack)
}
}
--------------------------------------------------------------------------------

obj_tigerattack

create:

Code:
alarm [0] = 3
alarm 0:
Code:
instance_destroy();
collision with hero:

Code:
with obj_hero {
health -= 8
}
--------------------------------------------------------------------------------

obj_hero

step event:

Code:
if place_meeting(x,y,obj_triggertiger)
{
with obj_tiger {
isattacking = true
}
}

else with obj_tiger {
isattacking = false
}

Thank you Pixeltroid for help and all users
 

pixeltroid

Member
Thank you Pixeltroid for help and all users
no problem man. Just note one thing -- the code I have given wont make the hero fall off. Because I am using a completely different code for player movement in my project. But the code I have given will make the tiger attack if you touch the trigger.
 

Imiglikos

Member
no problem man. Just note one thing -- the code I have given wont make the hero fall off. Because I am using a completely different code for player movement in my project. But the code I have given will make the tiger attack if you touch the trigger.

it's obvious ... but I'll set it up in my code ...
 

Imiglikos

Member
Imiglikos

I used the method I described above to 'solve' your tiger/cage problem. I used my existing project and a dummy tiger sprite.



As you see, the hero touches the pink box (obj_trigger) and it causes the tiger to attack by creating a large blue box (obj_tigerattack) which appears and disappears very quickly. When the hero touches the obj_tigerattack he loses 8 hp and falls off the cage. It creates the ILLUSION that the tiger is attacking the hero. Note that the obj_trigger and obj_tigerattack will be invisible.

I don't have a ladder in my project so I haven't shown it. But it's the same principle -- place obj_trigger on the ladder, and when the hero touches it, the tiger will attack him by creating obj_tigerattack which will knock him off the ladder.

I can give you the relevant codes to implement the above actions, but because I have used my own method and codes, you may need to change some code in your project. :)


ok, with the help of a friend, I did this solution for this tiger problem ..


but there are some mankamanets ..

1.the tiger does not throw the hero from the cage .. I do not know what code to apply here .. that when the hero comes into contact with obj_trigger which is an invisible object, the tiger attacks and throws the player from the player's cage to the right by a few pixels ..
2.and now the last thing..which is really weird..if I give a tiger a bone..and I go to obj_tigeratack it takes all my energy .. even though I gave a bone to a tiger..Of course tiger changes its sprite into a bone-eating sprite and the tiger he does not attack me anymore, but I can see that this invisible large sprite in obj_tigeratack is constantly displayed

this is what my code looks like now.

obj_tiger

create



GML:
image_speed = 1;
isattacking = false
eatbone = false
Alarm[0]

GML:
isattacking = false
sprite_index = spr_tiger_idle;
image_speed = 1;

step

GML:
//tiger attacks the player when he touches the trigger
if isattacking = true{
sprite_index = spr_tiger_attack;
image_speed = 2;
if !instance_exists (obj_tigerattack) {
instance_create(x,y-32,obj_tigerattack)
}
}

//if TIGER gets the bone it will stop attacking and change it's sprite
if eatbone = true {
sprite_index = spr_tiger_eatbones
image_speed = 1;
isattacking = false
}
obj_tigerattack (I set the cage-sized invisible sprite so that it touches the hero during the attack ... so it is bigger than a sprite tiger)

create


GML:
alarm [0] = 3
damageTime = 0;
Alarm[0]

GML:
instance_destroy();

collision event obj_tigerattack with obj_hero

GML:
if obj_hero.damageTime == 0
{
obj_hero.hp-=1;
obj_hero.damageTime=room_speed*3
}

obj_hero

create



GML:
hasbone = false;

step

GML:
///tiger and hero
//if hero touches the trigger WITHOUT the bone he will be attacked
if place_meeting(x,y,obj_trigger) && hasbone = false
{
with obj_tiger {
isattacking = true
alarm[0]=60
}
}

else with obj_tiger {
isattacking = false
}

//take bone
if place_meeting(x,y,obj_bones)
{
hasbone = true
with obj_bones instance_destroy();
}

//prevent tiger from attacking if you have bone
if hasbone = true {
with obj_tiger {
isattacking = false
}
}

//Give bone to tiger
if place_meeting(x,y,obj_trigger) && hasbone = true {
with obj_tiger {

eatbone = true

}
instance_create(obj_tiger.x+32,obj_tiger.y+12, obj_bones)
hasbone = false
}


thank you for any help



obj_trigger and obj_bones are without code
 

pixeltroid

Member
collision event obj_tigerattack with obj_hero

GML:
if obj_hero.damageTime == 0
{
obj_hero.hp-=1;
obj_hero.damageTime=room_speed*3
}
Here you mention a variable in obj_hero called "damagetime". But the code for obj_hero, does not have a variable called "damagetime".

Maybe this is causing the problem?

Like I said earlier, you have something else in your code that is causing a problem.
 
Top