- Home /
Loop problem
Hello, I would like my character to move from box to box following a array list which stores vectors 3. At the beginning, the character did not move enough because I would like the movement to be fluid. So I wanted to add a loop that works until the player has finished moving. The problem is that the character does not move and the loop seems infinite and therefore I have to restart Unity ...
public void StartTurn()
{
actionPoint = -1;
for(int i = 0; i < mouvementList.Length; i++) // mouvementList.Length = 6.
{
do
{
Vector3.MoveTowards(transform.position, transform.position + mouvementList[i], moveSpeed); // Move
}
while (transform.position != transform.position + mouvementList[i]) ;
}
startTurn.gameObject.SetActive(false);
playerTurn = true;
}
I did not understand I added a bool variable but it still does not work.
public void StartTurn()
{
bool move = true;
actionPoint = -1;
for(int i = 0; i < mouvementList.Length; i++) // mouvementList.Length = 6.
{
do
{
if (transform.position != transform.position + mouvementList[i])
{
Vector3.$$anonymous$$oveTowards(transform.position, transform.position + mouvementList[i], moveSpeed); // $$anonymous$$ove
}else
{
move = false;
}
}
while (move) ;
}
startTurn.gameObject.SetActive(false);
playerTurn = true;
}
}
Answer by Hostam · May 08, 2020 at 10:26 AM
I managed to do what I wanted and I didn't use while thanks anyway. I can put my code if someone wants to know how to make the character follow an array list of vector 3 in order and smoothly.
Answer by Labertasche3_0 · May 08, 2020 at 09:02 AM
You always need a possible exit for a while-loop, otherwise it will run forever. But you could use a for loop.
Your answer
Follow this Question
Related Questions
GUI inside a While Loop 1 Answer
Quaternion.Slerp issue 1 Answer
Iterator variable not increasing. 1 Answer
While/if loop problem 1 Answer
How to get something to be inactive while there is a certain value 2 Answers