- Home /
Enemy's gather around the player.. How?!
So I am working on some combat for my game... I have several enemy's wandering in an area. When the player attacks they will come towards the player. I use this code to get a position in front of the player:
var dir = player.position - transform.position;
var playerpoint : Vector3 = player.position - dir.normalized * 2;
Now this works nice. But if I have 3 enemy's attacking me (melee), and I walk around a bit. They eventually gather all up at the same spot, which is of course not what I want.
Can someone give me directions for this code to let it also check how many enemy's are next to the player (with TAG Enemy). And get a position next to the player but also next to the other enemy so they all gather around the player nicely (maybe in a circle).
Hope someone can help!
Answer by Royall · May 23, 2014 at 03:09 PM
This seems to work to get a angle:
var angle = 180;
var newPosition = transform.position + Quaternion.AngleAxis(angle, Vector3.up) * Vector3.forward * 3;
Will work with this to assign and check for a 'slot', thanks for the tip
Answer by MakeCodeNow · May 22, 2014 at 03:46 PM
The common solution to this problem is to have a predetermined number of "slots" around the player. Each slot is at a particular offset from the player, at different points around a circle. These slots translate with the player, but they do not rotate with them. When an enemy attacks the player, they see if a slot is available, reserve it, and then move towards that offset instead of towards the player directly. If no slots are available, then they move towards the player but stay further away, like the bad guys in a kung fu movie.
@$$anonymous$$akeCodeNow That's a nice idea! Do you know how I could deter$$anonymous$$e the slot positions... Lets say I want 8 slots around the player at distance of 2 units... How would I get the position of each slot given the position of the player?
Check out circular coordinates. Use them to get the local position then just add in the player position.