I’m trying to make the camera rotate for each players turn in a chess game, but when I made this code, the camera would stop moving. Anyone know the solution.
public void RotateCamera()
{
int x = 0;
if (isWhiteTurn == true)
{
transform.Rotate(0, 0, 0);
}
else
{
while(x < 180)
{
transform.Rotate(0, x, 0);
x++;
if(x >= 180)
{
break;
}
}
}
}
The problem is that since you executed this is Update and you set the value of x to 0 in the function every frame you set x to 0. The solution is to declare x in a Start or Awake function.
The problem is that you are using a while loop rather than a coroutine. A while loop will run every iteration of itself in a single frame. In other words, if you have a while loop that runs 180 times, all 180 loops will be calculated and done in a single frame. a coroutine on the other hand can be made to run a single iteration once every frame, or what ever time interval you want. Try something like this (i don’t have unity near me, so it may not be perfect)