I have an object (enemy), and I want it to shoot at the player when he's in a defined zone around the enemy.
I can easyly do it if there's only one enemy, but if I copy the object to have multiples enemys on my frame, nothing works. I could do it using another object representing the zone for each object but this means that it's necessary to code for every enemy on the frame. And I'm sure there's an easyer way to do it.
So if you know how to do that, please, let me know.
Hope you'll understand and sorry for my languages mistakes, I'm french.
n/a
Assault Andy Administrator
I make other people create vaporware
Registered 29/07/2002
Points 5686
19th May, 2008 at 04:36:08 -
One way you could do it is to always set an alterable value of an enemy to the distance between it and the player. This could be done with maths or with an extension. This distance would be different for every enemy as it is an internal value.
All you would need to do next is check whether the alterable value of an enemy was less than a certain value (eg: 200 pixels) and then make it attack the player.
Just to let you know that it work fine, so thank you again. I've added some restrictions so it can't shoot if he's dying or if it isn't in front of the player. Now all works fine.
Does your method works if I test X AND Y ? What I mean is, I want the enemy to shoot at the player if :
- Is in the direction of player
- Player is in a range of + or -250 pixels (depending on the position of player relative to the enemy) from the enemy (X axis)
- Player is in a range of -10 to +10 pixels from the enemy (Y axis)
What I want to simulate is some sort of range of view but as the enemy is constantly moving...
Don't bother too much, now it works with the method given by Assault Andy.
sadly shabs only works for the x distance. but can be modified slightly to work in an x AND y situation.
dont use
- Player is in a range of + or -250 pixels (depending on the position of player relative to the enemy) from the enemy (X axis)
- Player is in a range of -10 to +10 pixels from the enemy (Y axis)
because that makes a square which doesnt accurately depict distance. the enemy could be +10 and 250 away which is farther than 0 and 250. its just not good practice.
use a distance formula to put the distance of the enemy from the player into an alterable.
distance^2 = x^2 + y^2
distance = sqrt(x^2 + y^2)
x would be the x distance between enemy and player, and y would be y distance between enemy and player. that formula gives you the distance in between based on their x's and y's.
simply compare the distance to a fixed value to check if its close enough.
What I've done is really near of what you are saying, if I understand you well.
What I'm doing is :
- Compare x position of player and enemy and store it in an alterable value.
- Do the same with Y position and store it in another alterable value.
Then, all I have to do is :
If X position of player greater than a defined value relative to enemy
And X position of player lower than a defined value relative to enemy
And Direction of enemy is in front of player
Then shoot in Direction of player
Obviously, I have added the same for Y in the same event so enemy can only shoot player if their Y position are almost the same
This event has two adventages. First, as I'm having multiple same enemies in a scene, every enemy will react differently depending on the two alterable values stored. And the other advantage is that it can easeyly be modified for other enemies if I want them to react differently.
i understand what you are doing but its not mathematically correct. comparing the x independantly from the y makes the attack zone a square. you have to use x and y together in that formula to make the zone circular
Originally Posted by cecil sadly shabs only works for the x distance. but can be modified slightly to work in an x AND y situation.
dont use
- Player is in a range of + or -250 pixels (depending on the position of player relative to the enemy) from the enemy (X axis)
- Player is in a range of -10 to +10 pixels from the enemy (Y axis)
because that makes a square which doesnt accurately depict distance. the enemy could be +10 and 250 away which is farther than 0 and 250. its just not good practice.
use a distance formula to put the distance of the enemy from the player into an alterable.
distance^2 = x^2 + y^2
distance = sqrt(x^2 + y^2)
x would be the x distance between enemy and player, and y would be y distance between enemy and player. that formula gives you the distance in between based on their x's and y's.
simply compare the distance to a fixed value to check if its close enough.
Uh no, it works for Y as well. I'll upload the example when I get home. You just do the same exact thing for Y and add it in the same event. Yeah, it's only for a box though.
thats what i was meaning. you either do it for x or for y. not both in the same. thats how i used to do enemy zones and distance. its just not good, its horribly innacurate, unless youre really wanting a box zone area.
Well technically a box include both X and Y, but I get what you mean. Yeah, it whatever I used this for (pre Aru Core I think) it didn't matter if it was a box.
Ok, with the picture I understand what you mean now.
And you're right, the zone created by your method is more logical than mine. Except that I'm working on a platform game, and the square is good in that case (the enemy I'm working on precisely). But I'm curious to see a short exemple of your method cause I'm planning to have flying enemies, and in this case, your method will be better.
This subject would be a good subject for a tutorial I think...
EDIT : I asked an exemple because I don't know what mean ^ and sqrt in math... Sorry I'm 35 years old, school is far away from me !