Search Unity

Can't Get Animation to Play Only One Time

Discussion in 'Animation' started by jcoolguy, Jan 14, 2019.

  1. jcoolguy

    jcoolguy

    Joined:
    Jan 14, 2019
    Posts:
    3
    (Huuuuge C# newbie, here)

    When my character is close enough to an object (dice) and then clicks the object, an animation plays of the character grabbing the object.

    The animation plays, but it loops and never ends. I just want it to play once and then stop.

    This is the code I've added to the object that gets picked up:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DiceScript : MonoBehaviour {
    6.  
    7.  
    8.     public bool GrabBool;
    9.     public Animator PlayerAnimator;
    10.  
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.      
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update ()
    19.  
    20.     {
    21.  
    22.         if (Input.GetKey(KeyCode.Mouse0))
    23.  
    24.         {
    25.      
    26.         Collider[] hits = Physics.OverlapSphere (transform.position, 1);
    27.         foreach (Collider hit in hits)
    28.  
    29.             {
    30.                 if (hit.tag == "Player") {
    31.                  
    32.                  
    33.                     Destroy (gameObject);
    34.                     PlayerAnimator.SetBool ("GrabBool", true);
    35.  
    36.                 }
    37.             }
    38.  
    39.         }          
    40.     }
    41.  
    42. }





    Everything works, except for the animation problem.
    I'm going bonkers over here.

    Thanks.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,435
    if you double click those animations in animator stage, there is loop option (its on by default i think)
    upload_2019-1-14_13-47-38.png
     
  3. jcoolguy

    jcoolguy

    Joined:
    Jan 14, 2019
    Posts:
    3
    Thanks for the reply.

    As shown in image #3, LOOP TIME is unchecked.
     
  4. jcoolguy

    jcoolguy

    Joined:
    Jan 14, 2019
    Posts:
    3
    I figured it out. I shouldn't have been using a Bool. I needed to be using a Trigger.

    But even if I was to use a bool, you need to deactivate the bool after activating it, in order to stop the animation.
    Getting it to deactivate was a real pain. I never accomplished it.

    Furthermore, "I was saying Bool-Erns"
     
    flynnkoller likes this.
  5. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    The reason that it restarts is because you transition from the Any State. That transition will be retriggered each time your animation has finished.