Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Animation Layers?

Discussion in 'Animation' started by PRninja8488, Jan 18, 2015.

  1. PRninja8488

    PRninja8488

    Joined:
    Mar 22, 2014
    Posts:
    5
    I may be missing something obvious, but can someone please explain to me just how animation layers work? The official tutorial and manual seemed to focus more on what they do rather than on how to use them. I've combed the answers and the forums and no one seems to be asking about this, so that tells me that I'm just missing it. So I'm going to ask:

    How do I initiate an animation on a second layer?

    I have a simple 2D game where the player stands still and can either jump or shoot. I want the shooting to be on a second layer, but the shooting animation is not being called.

    UPDATE: I've discovered that the animations will play automatically. Which is nice, except that I only want to it to play when called. Scripting perhaps?
     
    Last edited: Jan 18, 2015
  2. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    Scripting or state controls on the animation layer.
     
  3. PRninja8488

    PRninja8488

    Joined:
    Mar 22, 2014
    Posts:
    5
    What do I need to do in order to script it? I've tried using Animation.Play, but I get a null reference exception (note: I'm not above admitting that I may just be using it wrong). I tried using SetTargetWeight, but it loops the clip in the background, so when I shoot, it starts mid-clip.

    This is the code that I have for this. Above the "yield return new" command is where I want to start the animation.

    void Fire ()
    {
    if (Time.time > nextFire) {
    nextFire = Time.time + fireRate;

    StartCoroutine (Shot ());
    }
    }
    IEnumerator Shot()
    {

    yield return new WaitForSeconds (animationDelay);
    Instantiate (shot, shotSpawn.position, shotSpawn.rotation);
    }
     
  4. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
  5. PRninja8488

    PRninja8488

    Joined:
    Mar 22, 2014
    Posts:
    5
    I appreciate it, but I don't think that's going to do it. I'm trying to figure out a way to toggle the shoot animation on and off. I figure animation.enabled would work, but Unity freaks out when I try. So I don't think I'm doing it right.
     
  6. PRninja8488

    PRninja8488

    Joined:
    Mar 22, 2014
    Posts:
    5
    I actually feel sort of silly for this, but the answer lie in creating an empty animation state to be the default animation on the upper layer.
     
  7. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    First you need to set the weight of the layer, either in the editor or by script at runtime when needed. By default the weight of a new layer is set to 0.

    http://docs.unity3d.com/ScriptReference/Animator.SetLayerWeight.html

    Exactly, create a first empty state in your upper layer and set it as your default state and then create your jump state.

    Best regards,