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.
Assault Andy Administrator
I make other people create vaporware
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.
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.
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
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.
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 !