- Home /
 
Performing if loop multiple times to perform attack animation
I am using the following code, which makes the game object look at me, walk towards me and then I change the boolean on the animation to perform an attack animation:
  transform.LookAt(ThePlayer.transform);
             if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
             {
                 float dist = Vector3.Distance(ThePlayer.transform.position, transform.position);
 
                 if (dist > 2.5f)
                 {
                     EnemySpeed = 0.01f;
                     //animation.SetBool("Running", true);
                     transform.position = Vector3.MoveTowards(transform.position, new Vector3(ThePlayer.transform.position.x, transform.position.y, ThePlayer.transform.position.z), EnemySpeed);
                     TargetDistance = Shot.distance;
                     animator.SetBool("Moving", true);
                     animator.SetBool("Attack1Trigger", false);
 
                     animator.speed = 0.5f;
                     if (TargetDistance < AllowedRange)
                     {
 
                         if (AttackTrigger == 0)
                         {
                             EnemySpeed = 0.05f;
                             transform.position = Vector3.MoveTowards(transform.position, new Vector3(ThePlayer.transform.position.x, transform.position.y, ThePlayer.transform.position.z), EnemySpeed);
 
 
                         }
                         else
                         {
                             EnemySpeed = 0.01f;
                             
                         }
                     }
 
 
                 }
                 else
                 {
                     animator.SetBool("Moving", false);
                     animator.SetBool("Attack1Trigger", true);
                     hasAttacked = true;
                 }
             }
 
               And then I am calling this to reduce the health of the player:
 if(EnemyMove.hasAttacked == true)
             {
                 currentHealth = currentHealth - 10;
                 healthBar.UpdateBar(currentHealth, maxHealth );
             }
 
               But it seems it is only doing this the once, and staying in the ELSE statement. I have tried to return but that does not work. Is there a way of saying to do this on a loop?
               Comment
              
 
               
              Answer by dcannata · Mar 15, 2019 at 11:25 PM
This sounds like a good use for 'while' to be utilized.
 int i = 0;
 while (i < 5) {
     EXECUTE THIS CODE;
     i++;
 }
 
               The above will execute the code within until the conditional is false, so in this example it will execute 5 times (0, 1, 2, 3, 4), or you can use this:
 while (true) {
     EXECUTE THIS CODE;
     if (condition that you want to stop performing the function is true) {
         break; 
     }
 }
 
 
               In either solution you need to ensure that you can end the while loop.
Also, use this:
 currentHealth -= 10;
 instead of 
 currentHealth = currentHealth - 10;
 
              Your answer