I have an array which is filled with all the objects with the tag Enemy. When one enemy comes close to an other enemy, it has to stay away from that enemy or go around it.
This is what I have now:
foreach(Transform enemy in enemies){
if(enemy == this.transform) continue;
enemyPos = enemy;
float enemyDistance = Vector3.Distance(enemy.transform.position, transform.position);
if(enemyDistance < 8){
transform.RotateAround (enemyPos.position, Vector3.up, 360 * Time.deltaTime);
}
}
If enemies come close enough to each other, they will circle around each other. Also tried to use 2 cube triggers for when an enemy object touches one of the cubes, the cubed enemy wil rotate away from it. Also tried different angles. But no luck :(
I am still trying to find a solution. But if you开发者_开发百科 have a better idea about this, your help is much appreceated :)
Thanks in advance!!!
You have several moving objects, and some of them may stand in the way of other objects. I would propose that you defer moving of all those objects that have a moving obstacle in front of them. When all free-moving objects are moved then try to move all other, by repeating the procedure, because in the process some objects might become free. You essentially loop through list of not-yet-moved objects until there is no change in the list.
If you don't find any new free-moving objects then deal with the rest as you like. I would suggest that you try find alternative route only for one of them, and then repeat the procedure above. It might be that when one object is unstuck then all others are unstuck as well. You may repeat this second procedure for the rest of stuck objects.
The following article could help you.
In short, your agent are "repulsing" each other when they are under the given distance. This is the "separation" concept in the flock. What is really interesting is that you can have a rotation that depends on the distance (ie another agent very near mean that you have to turn hard) and that you can interact with more than one agent.
in the same site, this can help you search the player. Combien the two method, and you have agents that try to catch the player while avoiding other agents.
regards Guillaume
精彩评论