Search Unity

A script to uncheck "Exit Time" on an animation while button is held down.

Discussion in 'Scripting' started by Sparrow421, Mar 8, 2018.

  1. Sparrow421

    Sparrow421

    Joined:
    Mar 8, 2018
    Posts:
    1
    Hey all, i am completely new to programming in general.

    Right now i am messing around with making a 2d character controller to try to learn scripting in unity.

    I am looking for a way to freeze an animation in it's last frame while the right mouse button is held.

    I noticed that if i uncheck "Has Exit Time" on the animations transition back to idle unity does what i want it to do.

    but i want to be able to script that to a button to continue doing it until the button is released.

    TIA
     
  2. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    Hi Sparrow!

    Whenever you are stuck you should try to look in the manual. Every programmer constantly has it open, it really has a solution to most of your problems.

    In this case, we can find that there is a property called `hasExitTime` that you can modify!

    https://docs.unity3d.com/ScriptReference/Animations.AnimatorStateTransition-hasExitTime.html

    You'll have to dig in the AnimatorController to find a AnimatorState, and then check its Transitions: https://docs.unity3d.com/ScriptReference/Animations.AnimatorState-transitions.html

    Though this is all a bit ugly... Dont you think its possible to solve this in the animator itself? For example passing the `mouseDown` state as a trigger, and using it to go and stay in the right state?
     
  3. Necrohunter

    Necrohunter

    Joined:
    May 18, 2017
    Posts:
    9
    to be honest everytime i see this manual pages, i dont have a clue what to do with this.
    as example:

    AnimatorStateTransition.hasExitTime
    public bool hasExitTime;

    Description
    When active the transition will have an exit time condition.

    where exactly do i need to write animatorstatetransition.hasexittime?
    im trying alot of this manuals, but most of them are useless for me, i need the pages where is an example written, other way i can sit hours without a solution. thats why questions like this exist.. we dont have time to search for ages for a solution which is probably just a simple line of code.

    but thank you anyway, i will keep searching for some simple solution

    edit:
    i found a simple solution.
    in my case my player keeps reloading even if the player dies, because the reload animation has an exit time.
    i just added another transition (the transition then have 3 arrows instead 1) and told this other transition to activate in case the player is dead and also uncheck in the new transition the exit time.
    in the picture you can see how it looks, three arrows means multiple transitions are used here and in the inspector you can select the different transitions and everyone has its own checkbox for exit times.
    cheers
     

    Attached Files:

    Last edited: Apr 11, 2020