- Home /
Question by
panim0_unity · Jan 06, 2020 at 07:32 AM ·
c#coroutinenavmeshagentbool
Coroutine doesnt turn bool?
I believe agents should not go to the destination that was at because it makes timeToAttack false in Countdown. this is the code. After counter2=0 they spawn at the positions but than they follow the destination again.
void(){
if (timeToAttack==true)
{
if (enemy1select)
{
agent1.SetDestination(enemyPlayer1.transform.position);
FaceTarget1();
/* if (combat1.isDeath==true)
{
Debug.Log("Calıstı 1");
enemyPlayer1 = null;
Debug.Log("Calıstı 2");
enemyPlayer1 = GameObject.FindGameObjectWithTag("Player");
Debug.Log("Calıstı 3");
agent1.SetDestination(enemyPlayer1.transform.position);
Debug.Log("Calıstı 4");
FaceTarget1();
}*/
}
if (enemy2select)
{
agent2.SetDestination(enemyPlayer2.transform.position);
FaceTarget2();
}
if (enemy3select)
{
agent3.SetDestination(enemyPlayer3.transform.position);
FaceTarget3();
}
}
if (enemyPlayer1 != null && agent1G != null)
{
float distance1 = Vector3.Distance(enemyPlayer1.transform.position, agent1G.transform.position);
if (distance1 <= 7)
{
animator1.SetBool("Attack", true);
animator1.speed = 1.5f;
}
}
if (enemyPlayer2 != null && agent2G != null)
{
float distance2 = Vector3.Distance(enemyPlayer2.transform.position, agent2G.transform.position);
if (distance2 <= 7)
{
animator2.SetBool("Attack", true);
animator2.speed = 1.5f;
}
}
if (enemyPlayer3 != null && agent3G != null)
{
float distance3 = Vector3.Distance(enemyPlayer3.transform.position, agent3G.transform.position);
if (distance3 <= 7)
{
animator3.SetBool("Attack", true);
animator3.speed = 1.5f;
}
}
}
IEnumerator Countdown(int seconds)
{
canClick = true;
waveTimer = GameObject.Find("Canvas/Timer").GetComponent<Text>();
int counter = seconds;
int counter2 = seconds;
while (counter > 0)
{
yield return new WaitForSeconds(1);
counter--;
waveTimer.text = "" + counter;
}
if (counter == 0)
{
timeToAttack = true;
canClick = false;
counter = seconds;
}
while (counter2 > 0)
{
yield return new WaitForSeconds(1);
counter2--;
waveTimer.text = "" + counter2;
if (counter2 == 0)
{
timeToAttack = false;
canClick = true;
Debug.Log("Fight Over!");
if (agent1G != null)
{
agent1G.transform.position = pos1;
agent1G.transform.rotation = rot1;
}
if (agent2G != null)
{
agent2G.transform.position = pos2;
agent2G.transform.rotation = rot2;
}
if (agent3G != null)
{
agent3G.transform.position = pos3;
agent3G.transform.rotation = rot3;
}
agent1 = null; agent1G = null;
agent2 = null; agent2G = null;
agent3 = null; agent3G = null;
enemyPlayer1 = null;
enemyPlayer2 = null;
enemyPlayer3 = null;
counter = seconds;
}
}
}
Comment
The first thing I would do would be to divide this script into 3, or use a loop and an array containing all the data for each iteration. Your current duplication of the code for each enemy/animator/agent is just a recipe for bugs.
Your answer
![](https://koobas.hobune.stream/wayback/20220612220954im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
IEumerator not recognising when a bool has switched 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Click to Move - get NavMesh Area Name 2 Answers