object avoidance
I have code that builds a path and returns the path as a list of nodes. My code to follow the path has an issue that when starting out the objects will sometimes go onto the same node and enter each other. I just need help preventing that. IEnumerator followPath4() { Node currentNode = Map.map.GetNodeFromLocation(transform.position); currentNode.isOccupied = true; currentNode.occCode = code; List<Node> p = new List<Node>(); int index = 0; path = new List<Node>(); path.Add(currentNode); path.Add(currentNode); bool broke = false; while (true) { if(path != null) { if(p.Count > 0 && path.Count > 0 && p[p.Count-1] == path[path.Count - 1]) { path = null; continue; } index = 0; p = path; path = null; Node next = p[index]; while(next.isOccupied && next.occCode != code) { if(path != null) { broke = true; break; } yield return null; } currentNode.isOccupied = false; currentNode.occCode = -1; currentNode = next; next.isOccupied = true; } if(transform.position != currentNode.pos && !broke) { transform.position = Vector3.MoveTowards(transform.position, currentNode.pos, moveSpeed * Time.deltaTime); } else if(index < p.Count - 1) { index++; Node next = p[index]; while(next.isOccupied) { if(path != null) { break; } yield return null; } currentNode.isOccupied = false; currentNode.occCode = -1; currentNode = next; currentNode.isOccupied = true; currentNode.occCode = code; } broke = false; yield return null; } }
Your answer
Follow this Question
Related Questions
How to draw a path from players's position to the target position where i touch? 0 Answers
How to move an Object in a path? 0 Answers
Doing an Update Function only once 1 Answer
How to make object follow line vectors 1 Answer
2D NPC Movement 0 Answers