Home > Mobile >  How to focus Camera on enemy grid for a specific seconds?
How to focus Camera on enemy grid for a specific seconds?

Time:02-25

I am making a turn based game. I have two different tile grids. When it's player's turn, the Camera is focused on the enemy's grid to select a tile to attack. When it's computer's turn the Camera will focus on player's grid to select a random tile to attack. Because the player has to give an Input (from mouse) to attack a tile, the Camera will be focused on the enemy's grid. However, when the computer attacks, the Camera moves very fast on the player's grid and back to the enemy's (in less than a second). What i want is to wait a few seconds so that the player can see where the attack happened. The Camera position changes inside the function Update(). Here is part of my code for that:

    private void Update()
    {
        if (playerTurn)
        {
            Camera.main.transform.position = new Vector3(40, 12.5f, -6);
            playerAttack(); //Select tile to attack
        }
        else
        {
            Camera.main.transform.position = new Vector3(-1.7f, 12.5f, -6);
            computerAttack(); //Select one random tile
        }
    }

CodePudding user response:

You could try sliding a `Thread.Sleep()' in there. For example this should pause for two seconds.

Camera.main.transform.position = new Vector3(40, 12.5f, -6);
Thread.Sleep(2000);
playerAttack(); //Select tile to attack

CodePudding user response:

You can use IEnumerator

private void Update()
{
   if (playerTurn)
   {
       Camera.main.transform.position = new Vector3(40, 12.5f, -6);
       StartCoroutine(WaitAndGO(2))
   }
   else
   {
      Camera.main.transform.position = new Vector3(-1.7f, 12.5f, -6);
      computerAttack(); //Select one random tile
   }
}
private IEnumerator WaitAndGO(float waitTime)
{
     yield return new WaitForSeconds(waitTime);
     playerAttack(); 
}
  • Related