Search Unity

Need help with Mechanim triggers

Discussion in 'Animation' started by NullSignal, Aug 20, 2015.

  1. NullSignal

    NullSignal

    Joined:
    Sep 18, 2013
    Posts:
    45
    I'm trying to set up sword fighting animations using triggers and, while I'm not getting any errors... nothing happens, either. I'm using layers in Mechanim because the player character may have a gun or fists instead of blades and I'm setting the layer weight to 1.0 and I'm using Additive, since I'm also using a layer mask and I'm only animating the upper body with this layer. I *think* that's the correct way of doing things, please correct me if I'm wrong. Either way, I've tried both Additive and Override and neither work.

    Here's the code I'm using to activate the triggers. The debug lines do execute.

    Code (CSharp):
    1. if (Input.GetButtonUp("Fire1"))
    2.             {
    3.                 Debug.Log("SwingLeft");
    4.                 anim.SetTrigger("SwingLeft");
    5.             }
    6.             if (Input.GetButtonUp("Fire2"))
    7.             {
    8.                 Debug.Log("SwingRight");
    9.                 anim.SetTrigger("SwingRight");
    10.             }
    11.             if (Input.GetButton("Fire3"))
    12.             {
    13.                 Debug.Log("Block");
    14.                 anim.SetTrigger("SwordBlock");
    15.             }
    16.             if (Input.GetAxis("Mouse ScrollWheel") < 0)
    17.             {
    18.                 Debug.Log("Slash");
    19.                 anim.SetTrigger("Slash");
    20.             }
    21.             if (Input.GetAxis("Mouse ScrollWheel") > 0)
    22.             {
    23.                 Debug.Log("Thrust");
    24.                 anim.SetTrigger("Thrust");
    25.             }
    This is how I'm setting the layer weight

    Code (CSharp):
    1. anim.SetLayerWeight(1, 1.0f);
    Here is how the layer is set up.

    sword_mechanim_layer.png

    Each transition is set up with the triggers... which are set up here

    sword_mechanim_params.png

    Could someone tell me what I'm doing wrong here? This is the first time I'm working with Mechanim layers and, according to the tutorial videos and documentation I've seen, this should be working. I'm sure I'm missing something somewhere.
     
  2. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Sorry I didn't reply to your other thread carnivoris.
    So the animations are just playing through without being interrupted?

    Are your animations set with has exit time? I have gone through this process only once before, and I no-code, but check to make sure the animations are interrupt-able by either having has exit time is checked or not. It's a little backwards in my opinion, I believe you want to un-check has exit time for this to work properly, but it might be need to be checked for it to work properly.

    I really need to work through this process again.
     
  3. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
  4. NullSignal

    NullSignal

    Joined:
    Sep 18, 2013
    Posts:
    45
    No, the animations just aren't playing, at least, they weren't when I made this post. I've since noticed some errors in my rigs on those animations and corrected those. However, now some of them do play when triggered... but only once. Some of them still don't play at all. I've got a fairly confusing mess on my hands here and I feel like it should be fairly simple.

    I actually don't understand what the person in that link is trying to do, really. The videos and documentation I've been looking at does pretty much exactly what I'm trying to do by just using animator.SetLayerWeight and using an avatar mask. I have made some progress today with it. As I said, I've gotten it to the point where one animation will play. I'm going to go back over all my animations to make sure they're set up exactly the same, as far as the rig goes. Maybe that will eventually lead me to a solution.

    I do now have another question, though. Do you have to "reset" a trigger? The documentation on Mecanim triggers implies that once you use a trigger, it does it's action then resets on its own. However, in this particular instance, it seems to trigger once and then not want to trigger again. I need to find out why.
     
  5. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Trigger reset them self when they are consummed by a transition.

    The behaviour that you see is often caused by the game loop ifset. You set the trigger in your Update function, the engine evaluate the mecanim controller which consumme your trigger, then you get another MonoBehaviour.Update and the button is still press so you set your trigger back to true.

    Code (CSharp):
    1.  
    2. if (Input.GetButton("Fire3")) anim.SetTrigger("SwordBlock");
    3. }
    Input.GetButton return true as long as the button is pressed, Autofire and trigger doesn't work well together you should maybe use Input.GetButtonDown in this case