- Home /
How to detect multiple objects for avoidance
Hi Everyone, I have three 3D objects that move with A*pathing. I am testing some local avoidance code that is basically supposed to repel units from each other. The code detects surrounding units from a list and if they are too close it pushes them away. It works for two of the objects but not the other. Could anyone tell me why this doesnt work? I have the code im using below and a screenshot of my display here: http://imgur.com/2rkCAjg (code works for the bottom 2 units in the picture)
public void LocalAvoidance(){
//get drawn ray between all units
for (int i=0; i<UnitsList.Count-1; i++) { //this iterates through my list of unit objects
for(int y=0; y<UnitsList.Count; y++){ //this iterates again through the list to make sure we capture all objects
Debug.DrawRay (UnitsList[i].transform.position, UnitsList[y].transform.position - UnitsList[i].transform.position);
//this draws a ray from unit to unit
float distance = Vector3.Distance (UnitsList[i].transform.position, UnitsList[y].transform.position);
//this calculates the distance from one unit to another
if (distance <= 3) { //if the distance is less than 3 push the unit away
Vector3 direction = Vector3.zero; //creates zero vector
direction=UnitsList[i].transform.position - UnitsList[y].transform.position; //gets the direction vector
transform.Translate(-direction*6*Time.deltaTime); //pushing in the opposite direction of the movement.
}
}
}
}
Answer by camtronius · May 06, 2014 at 06:04 PM
Im thinking that the solution may be making the first for loop without the -1 after UnitList. I think i was skipping an index because of this.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to add gameobject inside of an array? 0 Answers
Choose 10 items on each iteration 1 Answer
Need to find a point in a line using a percentage and a Vector3 list. 1 Answer
Boids/Flocking Tutorial 0 Answers