Search Unity

Animator is not playing a Playable UnityEngine.Animator:SetTrigger(Int32)

Discussion in 'Animation' started by BBoffa, Jun 21, 2016.

  1. BBoffa

    BBoffa

    Joined:
    Dec 23, 2015
    Posts:
    12
    Hi, all
    I've recently been trying to make a game and make my person stab the enemy by scripting it.
    Here's my code:
    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;

    public class AnimationScript : MonoBehaviour {
    public Animator anim;
    int Stab = Animator.StringToHash("KnifeMove");

    // Use this for initialization
    void Start () {
    anim = GetComponent<Animator> ();
    }

    // Update is called once per frame
    void Update () {
    if (Input.GetKeyDown (KeyCode.Z))
    {
    anim.SetTrigger(Stab);
    }
    }
    }
     
  2. TrickyHandz

    TrickyHandz

    Joined:
    Jul 23, 2010
    Posts:
    196
    Please format your posts with code tags to make it more readable in the future. I have test the code that you provided and have encountered no problems with the code itself. There may be an issue in the AnimatorController though. Check to make sure of the following:
    1. There is a trigger parameter called "KnifeMove" in the AnimatorController (capitalization counts).
    2. There is a transition from any state to the state with the stab animation that requires the condition "KnifeMove" to execute.
    3. The transition does not have "Has Exit Time" checked to ensure that the current animation can be interrupted.
    Also, what version of Unity are you using? That information might help narrow any behavior issues.

    [EDIT: Post edited based on findings of testing]
     
    Last edited: Jun 22, 2016
  3. BBoffa

    BBoffa

    Joined:
    Dec 23, 2015
    Posts:
    12
    How would I make a trigger parameter?
     
  4. BBoffa

    BBoffa

    Joined:
    Dec 23, 2015
    Posts:
    12
    Also I'm using 5.3.4
     
  5. TrickyHandz

    TrickyHandz

    Joined:
    Jul 23, 2010
    Posts:
    196
  6. BBoffa

    BBoffa

    Joined:
    Dec 23, 2015
    Posts:
    12
  7. TrickyHandz

    TrickyHandz

    Joined:
    Jul 23, 2010
    Posts:
    196
    That would mean that the AnimatorController does not have the parameter you are looking for. Make sure it is added to the Parameters in the Animator and remember that capitalization counts.
     
  8. BBoffa

    BBoffa

    Joined:
    Dec 23, 2015
    Posts:
    12
    I've added a trigger called KnifeMoves but I've added in the transaction from the knifeIdle to the KnifeMove. Is that right??
    Also, I loaded up my game and pressed the button that I assigned it to in the script. No warning or errors but my knife didn't move;
     
  9. TrickyHandz

    TrickyHandz

    Joined:
    Jul 23, 2010
    Posts:
    196
    Does the transition also have a condition requiring the "knifemove" trigger?
    Is the script attached to the object with the animator?
    You may want to add a Debug.log() line where you button click happen to make sure it is getting called properly. Beyond that, without sample code or a project file it is going to be hard to track down.
     
  10. BBoffa

    BBoffa

    Joined:
    Dec 23, 2015
    Posts:
    12
    Here is my project:
    https://www.dropbox.com/s/gwg4l3tm0mdl703/KingdomTest1.exe?dl=0
     
  11. TrickyHandz

    TrickyHandz

    Joined:
    Jul 23, 2010
    Posts:
    196
    A compiled version of the project won't work well for trouble shooting. You would have to provide the Unity project files or post screenshots of your animator controller, with the transition selected so the inspector is clearly visible.
     
  12. BBoffa

    BBoffa

    Joined:
    Dec 23, 2015
    Posts:
    12
    Ok. Here are the images
    https://www.dropbox.com/s/jw3v9r869njb064/Screenshot (2).png?dl=0
    https://www.dropbox.com/s/7kl0kksx3p5yevy/Screenshot (3).png?dl=0
    https://www.dropbox.com/s/4nycxo70sncrxuu/Screenshot (5).png?dl=0
     
  13. TrickyHandz

    TrickyHandz

    Joined:
    Jul 23, 2010
    Posts:
    196
    So, right now it looks like you have a big transition period where your KnifeIdle state and the KnifeMove state where the animations will blend together. Also, there is no transition back from the KnifeMove state back to the KnifeIdle state which means your character will be stuck in the KnifeMove once it enters it. Here are some things to try:
    1. On the transition from KnifeIdle to KnifeMove, uncheck "Has Exit Time"
    2. Create a transition from KnifeMove to KnifeIdle. You should be able to keep the default settings for the transition to at least get a working result.

    Also, try putting your Animator window somewhere else in your editor layout so you can see it working while you are in play mode. This will quickly tell you if you are actually entering and leaving the state properly.
     
  14. BBoffa

    BBoffa

    Joined:
    Dec 23, 2015
    Posts:
    12
    Ok. do i put the trigger in the transition from knifemove to knifeidle
     
  15. BBoffa

    BBoffa

    Joined:
    Dec 23, 2015
    Posts:
    12
    Ran the game again, dont work.
     
  16. TrickyHandz

    TrickyHandz

    Joined:
    Jul 23, 2010
    Posts:
    196
    You do not need to put a trigger on the transition back to Idle...as I stated, just add the transition and leave the default values.

    It is really hard to try to troubleshoot this issue when you don't say what you have done so far. Simply saying that it doesn't work is not helpful. Here are some steps to try to track this down (please answer all questions along the way):
    1. Create a new scene.
    2. Add your character to the scene.
    3. Add and Animator component to the character.
    4. Create a new Animator controller and add the KnifeIdle animation to it to create a default state.
    5. Assign the new Animator Controller to the character Animator Component.
    6. Run the game. Does the KnifeIdle animation play properly? If yes, continue. If no, the problem exists in either your animation or the rig.
    7. Add a trigger parameter to the Animator Controller called "KnifeMoves"
    8. Add the KnifeMove animation to the AnimatorController and create a transition from KnifeIdle state to KnifeMove state.
    9. Uncheck "Has Exit Time" in the transition's properties.
    10. In the transition's condition list, add "KnifeMove" as a condition.
    11. Run the game, select your character in the hierarchy, go to the Animator Window and click on the "KnifeMove" trigger in the parameter list. Does this properly trigger the transition and play the KnifeMove animation? If yes, continue. If no, the problem exists in the setup of the animator controller and you should redo all steps starting at 7.
    12. Add your custom AnimationScript to the character.
    13. Run the game and press Z. Does the character animate properly. If yes, continue. If no, the problem is in the custom script.
    14. In the Animator controller, add a transition from KnifeMove to KnifeIdle. DO NOT change any settings in the transtion's properties.
    15. Run the game. Press Z. Does the character play the KnifeMove animation and then return to KnifeIdle? If yes, everything is working properly. If no, something is wrong in the animator setup.

    Really hope that helps you out.
     
  17. BBoffa

    BBoffa

    Joined:
    Dec 23, 2015
    Posts:
    12
    I dont know how to trigger the trigger in a script can you help me?
     
  18. BBoffa

    BBoffa

    Joined:
    Dec 23, 2015
    Posts:
    12
    Hi. I don't know how to activate a trigger in a script can you help
     
  19. TrickyHandz

    TrickyHandz

    Joined:
    Jul 23, 2010
    Posts:
    196
    The script you provided in the first post does it.
     
  20. BBoffa

    BBoffa

    Joined:
    Dec 23, 2015
    Posts:
    12
  21. TrickyHandz

    TrickyHandz

    Joined:
    Jul 23, 2010
    Posts:
    196