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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Mecanim - automatic SetBool after animation ends

Discussion in 'Scripting' started by Deecann, Mar 23, 2013.

  1. Deecann

    Deecann

    Joined:
    Mar 17, 2010
    Posts:
    93
    Simple problem, I've got action which sets some bool to true (anim.SetBool).
    Now, animation which will be played has exit condition on Exit Time = 0.85 and I wan't to play this once.

    The problem is, when it goes to this animation, plays once, I have no idea how automaticly set bool flag to false.
    Animation is going over and over again.

    So the question is, how SetBool can be switched when animation ends ( has exit time ).
     
  2. Kinos141

    Kinos141

    Joined:
    Jun 22, 2011
    Posts:
    969
    On the file is it set to loop? Turn that off and it should play only once.
     
  3. Deecann

    Deecann

    Joined:
    Mar 17, 2010
    Posts:
    93
    No, it has no loop. It's looping because transition has boolean set to true. So it's going with transition to animation, exits from animation when condition is Exit time = 0.85 and go again.
     
  4. Archimagus

    Archimagus

    Joined:
    Sep 29, 2012
    Posts:
    21
    In script, where you set your bool to true, then immediately set it back to false. The "true" setting just triggers it to start, but then setting it back to false will not cause it to stop if the exit condition is set to Exit time.
     
    crav12345 and kishsolanki like this.
  5. HaNioL

    HaNioL

    Joined:
    Jun 20, 2013
    Posts:
    2
    Same problem here!
     
  6. SamTheT

    SamTheT

    Joined:
    Feb 21, 2015
    Posts:
    4
    it actually does. as soon as I set boolean to false animation stops playing
     
  7. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    You could use a yeild WaitForSeconds or set it to false after one frame....
     
  8. reppiz01

    reppiz01

    Joined:
    Oct 20, 2012
    Posts:
    10
    Lecarno, The501legion, l00p33 and 9 others like this.
  9. Divit

    Divit

    Joined:
    Jul 14, 2017
    Posts:
    4
    @repipz01 Thanks man
     
  10. AnnaGeras

    AnnaGeras

    Joined:
    Nov 27, 2018
    Posts:
    1
    Castanharo likes this.
  11. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    400
    Resurfacing this for an issue I'm having.

    What if I need to set a script bool (not an animation bool) back to false once a trigger animation is finished? I need it to be true during the animation, then false after. But if I place the false in the code, it's immediate and is false DURING the animation, which does not work.
     
  12. TheGameNewBie

    TheGameNewBie

    Joined:
    Jul 27, 2017
    Posts:
    92
  13. russisunni

    russisunni

    Joined:
    Oct 26, 2019
    Posts:
    8
    @repipz01 Thanks man
     
  14. MlleBun

    MlleBun

    Joined:
    Sep 19, 2017
    Posts:
    163
    Hi @Deecann, what you can also do is to set an event function on your animation which will call your anim.setBool code.


    Ex : On this exit time (0.85) or at the end of the animation as shown below, place an Event function Stop.
    upload_2020-11-5_8-55-51.png

    Then in script
    Code (CSharp):
    1. private void Stop()
    2. {
    3.    anim.SetBool(true);
    4. }
    When dealing with animation, I prefer to work with events, which is cleaner and offers more control on the animations. I hope it helps.
     
    Cookieg82 likes this.