Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question animation won't stop

Discussion in 'Scripting' started by kingdom216, May 19, 2023.

  1. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    45
    My pick up animation won't stop unless I let go of the walk button. Stationary pick up is fine, but when movement is applied to it it gets on a loop. (There are no loop boxes ticked true. I tried adding:

    Code (csharp):
    1.  
    2. else if(isMovementPressed && !isPickingUp)
    3. {
    4.        animator.SetBool(isPickingUpHash, false);
    5. }
    6.  
    at the end of the following script but it doesn't stop the picking up motion while movement is pressed. What am I missing here?

    Code (csharp):
    1.  
    2.      
    3.  void handleAnimation()
    4.     {
    5.         bool isWalking = animator.GetBool(isWalkingHash);
    6.         bool isRunning = animator.GetBool(isRunningHash);
    7.         bool isPickingUp = animator.GetBool(isPickingUpHash);
    8.  
    9.         if (isMovementPressed && !isWalking)
    10.         {
    11.             animator.SetBool(isWalkingHash, true);
    12.         }
    13.  
    14.         else if (!isMovementPressed && isWalking)
    15.         {
    16.             animator.SetBool(isWalkingHash, false);
    17.         }
    18.  
    19.         if ((isMovementPressed && isRunPressed) && !isRunning)
    20.         {
    21.             animator.SetBool(isRunningHash, true);
    22.         }
    23.  
    24.         else if ((!isMovementPressed || !isRunPressed) && isRunning)
    25.         {
    26.             animator.SetBool(isRunningHash, false);
    27.         }
    28.  
    29.         if(isPickUpPressed)
    30.         {
    31.             animator.SetBool(isPickingUpHash, true);
    32.         }
    33.  
    34.         else if(!isPickUpPressed)
    35.         {
    36.             animator.SetBool(isPickingUpHash, false);
    37.         }
    38.  
    39.         if(isMovementPressed && isPickingUp)
    40.         {
    41.             animator.SetBool(isPickingUpHash, true);
    42.         }
    43.  
    44.  
    45.     }
    46.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    Did you make a proper Has Exit Time transition out of the pickup state?

    You can open the Animator window and manipulate it at runtime to debug what is going on.

    Beyond that, if the Animator is fully set up, then it is...

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  3. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    45
    To answer your questions, yes the transitions in the animator are marked properly for exit time and isWalking false and isPickup true, and isWalking true & isPickUp false during the return transition. The debug.log shows in the console that everything is working, the only problem is that while I am pressing the movement button, the pickup animation doesn't stop until I stop pressing the walk button. I thought I was clear in my description of the problem, that the code all works, it just won't stop animating the pick up motion while the movement button is pressed.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    Okay, so you are saying that the code is properly asserting the property booleans... that's GREAT!

    Now take the code OFF the animator and press play and twiddle the properties yourself in the editor and change the state machine around until it works.

    If that does not work, again, it's not in the code because you have told me the booleans are correct, so just set up the animation controller afresh, eg, make a new one and get it correct.

    We know animation controllers work and you're telling me the code sets the properties, so this isn't really a scripting question.
     
  5. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    45
    I will try what you said, but there's got to be a way to use the script to inform the animator that when I am pressing movement and pickup an object then release the picking up button to stop the picking up animation while the movement button is still being pressed. I feel like this is a scripting issue because there should be a line of code that conveys this to the animator, "Continue walking, but stop scooping down to pick up an item when the scooping button is released".
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,647
    I feel like a picking-up animation should be on it's own layer in the AnimatorController, so it just runs on-top of any other animations.
     
  7. Deleted User

    Deleted User

    Guest

    @kingdom216 You seem to have had problems with animation and input for 2 years.

    If you’re still struggling with this near 2024, I recommend you set that aside and work on other aspects of your game. Otherwise you will never get your game to a playable state.