Question by
sniperar · Sep 21, 2020 at 10:02 PM ·
unity 5pathfinding
Node Based AI,Node Based AI System?
I am trying to make a Node-based AI network but when I run my code the AI or Capsule (CRed) keeps getting stuck on node 1 and 2 only going back and forth on them never moving to the connected node 3, the same happens with node 6, it always goes to 4 and never 8 and 7, the same is also with node 4 where it won't move to node 5 only 3.
public GameObject Node1;
public GameObject Node2;
public GameObject Node3;
public GameObject Node4;
public GameObject Node5;
public GameObject Node6;
public GameObject Node7;
public GameObject Node8;
public GameObject CRed;
public GameObject CGreen;
public GameObject CBlue;
public float Node = 1;
public float TimeRemaining = 3;
public float NodeRand;
private void Update()
{
Vector3 Node1Pos = Node1.transform.position;
Vector3 Node2Pos = Node2.transform.position;
Vector3 Node3Pos = Node3.transform.position;
Vector3 Node4Pos = Node4.transform.position;
Vector3 Node5Pos = Node5.transform.position;
Vector3 Node6Pos = Node6.transform.position;
Vector3 Node7Pos = Node7.transform.position;
Vector3 Node8Pos = Node8.transform.position;
TimeRemaining -= Time.deltaTime;
if (TimeRemaining <= 0)
{
if (Node == 1)
{
NodeRand = Random.Range(1, 2);
if (NodeRand == 1)
{
Node = 2;
print("Node2");
}
else if (NodeRand == 2)
{
Node = 3;
print("Node3");
}
}
else if (Node == 2)
{
NodeRand = Random.Range(1, 2);
if (NodeRand == 1)
{
Node = 1;
print("Node1");
}
else if (NodeRand == 1)
{
Node = 3;
print("Node3");
}
}
else if (Node == 3)
{
NodeRand = Random.Range(1, 3);
if (NodeRand == 1)
{
Node = 1;
print("Node1");
}
else if (NodeRand == 1)
{
Node = 2;
print("Node2");
}
else if (NodeRand == 1)
{
Node = 4;
print("Node4");
}
}
else if (Node == 4)
{
NodeRand = Random.Range(1, 3);
if (NodeRand == 1)
{
Node = 3;
print("Node3");
}
else if (NodeRand == 2)
{
Node = 5;
print("Node5");
}
else if (NodeRand == 3)
{
Node = 6;
print("Node6");
}
}
else if (Node == 5)
{
Node = 4;
print("Node4");
}
else if (Node == 6)
{
NodeRand = Random.Range(1, 3);
if (NodeRand == 1)
{
Node = 4;
print("Node4");
}
else if (NodeRand == 1)
{
Node = 7;
print("Node7");
}
else if (NodeRand == 1)
{
Node = 8;
print("Node8");
}
}
else if (Node == 7)
{
Node = 6;
print("Node6");
}
else if (Node == 8)
{
Node = 6;
print("Node6");
}
TimeRemaining = 3;
}
if (Node == 1)
{
CRed.transform.position = Node1Pos;
}
if (Node == 2)
{
CRed.transform.position = Node2Pos;
}
if (Node == 3)
{
CRed.transform.position = Node3Pos;
}
if (Node == 4)
{
CRed.transform.position = Node4Pos;
}
if (Node == 5)
{
CRed.transform.position = Node5Pos;
}
if (Node == 6)
{
CRed.transform.position = Node6Pos;
}
if (Node == 7)
{
CRed.transform.position = Node7Pos;
}
if (Node == 8)
{
CRed.transform.position = Node8Pos;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How do I configure A*? 1 Answer
Astar movement implementation not working properly 0 Answers
Problem with layer mask and nodes 1 Answer
Typo in file exists 0 Answers
Pathfinding Set Up In Unity2D 0 Answers