Search Unity

Play animation doubt

Discussion in 'Animation' started by -Stil, Mar 23, 2017.

  1. -Stil

    -Stil

    Joined:
    Mar 23, 2017
    Posts:
    1
    Hello guys,
    Is is possible to play an animation consecutively without waiting the currently animation end so another can start?
    Example, fire automatic weapon then play the shooting animation over and over without waiting its length.

    public void Fire(){
    if (bulletsInMagazine > 0 && Time.time > nextFire) {
    nextFire = Time.time + fireRate;

    gunAnimations.Play ("Fire");
    gunAnimations ["Fire"].speed = animationSpeed;

    Object bulletClone = Instantiate (bulletGO, bulletPlace.transform.position, bulletPlace.transform.rotation);
    Object bulletSoundClone = Instantiate (bulletSoundGO, bulletPlace.transform.position, bulletPlace.transform.rotation);
    Destroy (bulletClone, 0.6f);
    Destroy (bulletSoundClone, bulletSoundGO.GetComponent<AudioSource>().clip.length);

    subtractBullets (bulletsToShoot);
    }
    }
     
  2. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Consecutively - means one after the other without interruption so your question is an enigma.

    Look into the 'has exit time' option in the documentation. Since you are using code instead of mecanim I don't know the "code" equivalent to check/uncheck has exit time.
     
  3. TheBooBear

    TheBooBear

    Joined:
    Dec 15, 2011
    Posts:
    95
  4. TrickyHandz

    TrickyHandz

    Joined:
    Jul 23, 2010
    Posts:
    196
    As has been mentioned, you can interrupt animations. Your script appears to be using the legacy animation system rather than Mecanim. You can change this line:
    Code (csharp):
    1. if (bulletsInMagazine > 0 && Time.time > nextFire)
    to this:
    Code (csharp):
    1. if (bulletsInMagazine > 0)
    and remove this line:
    Code (csharp):
    1. nextFire = Time.time + fireRate;
    Those changes should remove all delay and the animation would start over every frame. However having a fire rate less than the length of the animation should interrupt it, if my memory of the legacy system serves me well.

    [EDIT]
    I seem to remeber now that the legacy system required you to explicitly call Animation.Stop () before Animation.Play() if you are trying to interrupt. It has been years though so I could be wrong.


    Cheers,
    TrickyHandz
     
    Last edited: Mar 29, 2017