Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Looping animation?

Discussion in 'Animation' started by aCake0202, Apr 15, 2021.

  1. aCake0202

    aCake0202

    Joined:
    Mar 11, 2021
    Posts:
    26
    I finally got the animation to play when the "enemy" gets close to the player, but the animation loops rapidly. How can I make it wait for the animation to finish before looping again?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6.  
    7. public class ZombieAI : MonoBehaviour
    8.  
    9. {
    10.     [SerializeField] public GameObject Player;
    11.     [SerializeField] public GameObject Zombro;
    12.     private NavMeshAgent Zombie;
    13.  
    14.     [SerializeField] private Animator myAnimationController;
    15.  
    16.  
    17.     void Start()
    18.     {
    19.        
    20.        
    21.         Zombie = GetComponent<NavMeshAgent>();
    22.        
    23.     }
    24.    
    25.     // Update is called once per frame
    26.      void Update()
    27.     {
    28.     float distance = Vector3.Distance (Player.transform.position, Zombro.transform.position);
    29.         Zombie.SetDestination(Player.transform.position);
    30.         if(distance < 2)
    31.         {
    32.            myAnimationController.SetBool("attack1", true);
    33.          
    34.          
    35.         }
    36.        
    37.  
    38.    
    39.     }
    40.    
    41. }
    42.  
    43.  
     
  2. jack_65

    jack_65

    Joined:
    Apr 8, 2021
    Posts:
    8
    I am working on it too. Did you find any solutions?
     
  3. launzone

    launzone

    Joined:
    Dec 2, 2015
    Posts:
    56
    My guess is, in the Animator you have a transition from "Any" to your animation and in the settings for that animation you have "can transition to self" checked to true, like it is by default. You should try checking this to false.
     
  4. aCake0202

    aCake0202

    Joined:
    Mar 11, 2021
    Posts:
    26
    In my case, I had to get myanimationcontroller to just play the animation rather than setting it to true.