Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Setting an Animation Boolean back to false after it has played.

Discussion in 'Scripting' started by Ceriumin, Dec 12, 2021.

  1. Ceriumin

    Ceriumin

    Joined:
    Dec 17, 2019
    Posts:
    9
    Hello, I recently am making a Pixel Art style top-down shooter as a college project, and I am quite new to C#.

    My issue is, I tried to create a shooting animation, and I did it successfully but I want it to play again after a set amount of time. Therefore, I tried experimenting and set the Animation to true when the gun is fired (that has a set cooldown). When I try doing it normally, when pressing Fire, the Animation plays but I can keep playing it repeatedly and I want it to be in sync with the firing. My latest implementation is the script below:

    Code (CSharp):
    1.             if (isAiming && timer > shootDelay)
    2.             {
    3.                 topAnimator.SetBool("Aim", true);
    4.                 GameObject bullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity); //Bullet spawning script 77-82
    5.                 bullet.GetComponent<Rigidbody2D>().velocity = shootingDirection * bulletVelocity;
    6.                 bullet.transform.Rotate(0, 0, Mathf.Atan2(shootingDirection.y, shootingDirection.x) * Mathf.Rad2Deg);
    7.                 Destroy(bullet, bulletDespawnTime);
    8.                 timer = 0;
    9.                 AmmoCount(-1);
    10.             }
    I set the topAnimator boolean to true when I fire, but I have no way of turning it off and it keeps looping while it is true. Is there another way of doing it, a setting in the blend tree perhaps, or maybe an If statement with a timer, set with shootDelay. Thank you
     
  2. Ceriumin

    Ceriumin

    Joined:
    Dec 17, 2019
    Posts:
    9
    Nevermind, I fixed it, I missed the obvious and set
    Code (CSharp):
    1.             if (isAiming == false)
    2.             {
    3.                 topAnimator.SetBool("Aim", false);
    4.             }
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    There is also a property type called Trigger (not to be confused with the collider type trigger), which is just a bool that doesn't stay true.

    A trigger trips only once and can drive state changes in the animator. Not 100% sure it works in your blendtree application, but triggers are pretty useful for one-shot type things in animators.