Forum rules - please read before posting.

Head animation for the player

nhtnht
edited February 2022 in Technical Q&A

Hello, I'm trying to build the prefab of my player with some peculiarities in a 2D game.
-The player is a knight and he is able to remove his helmet sometimes. So I need to work with two different heads.
-Both head sprites are animated while the Player walks. When he is not wearing the helmet the hair moves, and when he wears the helmet he has an arrow in the top that waves.
I need an approach to work on this as reading the 2D head animation manual I don't see how to work on this.
-Should I create two different objects with the same name (two Head_D with different tag: Helmet and no helmet) and make one of them invisible?
-About the head animation, should I add an animator to every object inside Head folder inside Body?

«1

Comments

  • Is the character using a rig, or frame-swapping for animation?

    You don't necessarily need to rely on AC's "Head animation" options for this, as you can embed the animations into your standard animations.

    For example, "WithHelmet_Idle_D" could be an animation clip for the whole body with the helment, with "WithoutHelment_Idle_D" as its equivalent without.

    What you could then do is rely on the Character: Animate Action's Set Standard option to switch their standard animation naming convention, from e.g. "WithHelmet_" to "WithoutHelmet_". This'll then cause the alternative set of animations to be played.

    Having two separate objects and making one of them invisible is also an option. For this, you'd animate both together, and then control their visibility in a sub-layer in your Animator, with a pair of clips that each cause one to show and the other to be invisible. Controlling this playback would then be a case of using another Character: Animate Action to affect a Bool parameter in the Animator that causes a transition between these two states in the sub-layer.

  • The character is using a frame-swapping animation.
    Anyway, in order to change the Standard actions, I guess I need to select the Sprites Unity Animation engine, right? I was taking as an example the prefab available for the head animator, which is Sprite Unity Complex, and selecting this I don't see the Action list. I'm saying that because I want the head being controlled separately from the body.
    Also, I have to say that in this action list (Standard 2D Animations) there's only:
    Idle, Walk, Run and Talk. Where I am able to define new animations here?

  • Idle, Walk, Run and Talk. Where I am able to define new animations here?

    I'm suggesting to use an alternative set of these same animations, not add new ones.

    For example, if you place in two animation states: WithHelmet_Idle_D and WithoutHelmet_Idle_D, into your Controller, you can set WithHelmet_Idle_D to be the default by entering in "WithHelmet_Idle" as the "Idle anim name" in your Player's Inspector. When the character removes the helmet, you'd then use the Action to change the "Idle anim name" field to "WithoutHelmet_Idle".

    If you want the head and body to be animated separately, however, it may be best to rely on Sprites Unity Complex after all.

    In this mode, animation playback is driven by Animator parameters - not by calling state names directly via naming convention.

    My suggestion above regarding use of a sub-layer to control things would be the approach here. This could either be a case of using a sub-layer dedicated only to controlling the visibility of two separate head sprites, or use a single object, and control which set of head animation is played. Both of these would involve use of a Bool parameter (e.g. "IsWearingHelmet") that could then be controlled via AC.

    In this animation mode, however, it's more a case of setting up the Animator to work in your intended way - AC won't be so involved here, as it will only set the values of the parameters that you allow it to in the character's Inspector.

    If you have screenshots showing your current attempt with this mode, you're welcome to share them.

  • Thank you, very much. I'm trying to understand all what you are saying.
    I don't know how to create a new animation set, I don't know if you are talking about a new state in the animator controller. But as I need to work with Sprites Unity Complex I understand that this is not relevant (if I am understanding).

    I need to understand if this animation controller is the one that has the prefab, or if I need to use different animation controllers for body and head.
    I don't know how I should place the objects in to the hierarchy either.
    https://ibb.co/N1xKPCS

  • There's no need for multiple Animator Controllers - you can just use the one, so long as it's high up enough in the Hierarchy (root or immediate child) for it to be able to affect all objects you wish it to.

    The purpose of using sub-layers is to be able to animate multiple things at once without the need for multiple Controllers. For example, your base layer could handle your standard animations for the various directions, while a sub-layer could handle object visibility.

    You also don't need to have separate GameObjects for each head direction - unless I'm missing some specific requirements of the animation. If you're just relying on regular sprite-based animation, you could even get by with just one GameObject for all head animation - but having separate Head and Helmet objects is probably easier to set up.

    With this approach, you'd animate both Head and Helmet objects together - so e.g. HeadIdle_D would update the Head object with its down-idle sprite, and the Helmet object with its own down-idle sprite, together.

    You'd then make use of a sub-layer to control which object is shown. This'd be a case of supplying it with two Animation states - one that disables the Head object / enables the Helmet object, and another vice-versa. A bool parameter that causes a transition back and forth could then be used to control their visibility.

    Much of this workflow isn't unique or specific to AC, however, as it's mainly just dealing with Unity's Animator system. If you're unfamiliar with sub-layers, I'd recommend visiting Unity's documentation pages on this beforehand.

  • nhtnht
    edited March 2022

    Hello, I've been working some weeks with that and I'm not getting results.
    This is how I organized the prefab object. One body and tgwo objects over it with the animations and everything created correctly through the BlendTrees, just like the prefab example you did in the demo.
    But now I don't know how to disable any of this objects. I don't know if I need to Add a Behaviour, and if this is true and I need to script anything this is another issue, because I don't know how to access the variable I created to control this.

    https://imgur.com/a/4H8FEmH

  • nhtnht
    edited March 2022

    This is what I am trying to use as code, I don't know if this is going to work as the object seems not to me showing.
    BTW, isWearingHelmet is a boolean.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using AC;

    GVar WearingHelmet = GlobalVariables.GetVariable("isWearingHelmet");
    
    if (WearingHelmet.GetValue() == true) {
        print("Snor is wearing helmet");
    } else {
        print("Snor is not wearing helmet");
    }
    

    Of corse this is not working. I don't know how to retrieve the name of the variable instead working with the id of it. There are no examples in the guide.

  • Ok, I have seen that this needs to be inside of a method to work.

  • So the code so far is this.

    public class Helmet : StateMachineBehaviour
    {
    GVar CheckHelmet = GlobalVariables.GetVariable("isWearingHelmet");
    public GameObject cabesa;
    public GameObject casco;

    void Start() {
    
        if (CheckHelmet.BooleanValue == true) {
            Debug.Log("Snor is wearing helmet");
        } else {
            Debug.Log("Snor is not wearing helmet");
        }
    }
    

    }
    But still I don't know what to do. Im trying to use the object to use method enable.false, but it doesn't work.

  • You can't define a GVar variable outside of a method, as it won't exist outside of runtime.

    Also, the Start method is unique to MonoBehaviour - it's not defined for StateMachineBehaviour.

    Try this alternative, attached to the character GameObject as a regular component:

    using UnityEngine;
    using AC;
    
    public class Helmet : MonoBehaviour
    {
        public GameObject cabesa;
        public GameObject casco;
    
        void Update()
        {
            GVar CheckHelmet = GlobalVariables.GetVariable("isWearingHelmet");
    
            if (CheckHelmet.BooleanValue == true)
            {
                Debug.Log("Snor is wearing helmet");
                casco.SetActive (true);
                cabesa.SetActive (false);
            }
            else 
            {
                Debug.Log("Snor is not wearing helmet");
                cabesa.SetActive (true);
                casco.SetActive (false);
            }
        }
    
    }
    
  • nhtnht
    edited March 2022

    Hello, thank you very much for this code.

    Now I would like to know who you create these animations in the prefab which have all the layers and it has just one of them enabled. Do you have any kind of tutotial explaining step by step? I'm trying to follow the prefab and the documentation, but it doesn't go deep in that.

  • What animation engine is your character now using?

    The "Sprites Unity Complex" example prefab in the 2D head animation package uses the same technique of having separate head objects for each direction, and then hiding all but one.

    The only difference with the example is that it disables the Sprite Renderer, whearas it appears your animation removes the alpha channel of the sprite's color. Both techniques are valid, though.

    The animation you've shared looks fine - you'll then just need to place it in a BlendTree along with the others inside a sub-layer of your Animator Controller, and have that BlendTree be controlled by the "HeadAngle" paramater in your Player component.

    Compare your Animator Controller asset with the example's BrainHead_SpritesUnityComplex asset. Share uncroppsed screenshots of all involved if you need another pair of eyes on it, but the package's Readme file gives a rundown of how the technique works.

  • Hello, I am using the "Sprites Unity Complex" one.
    The animation I shared is the one that comes with your prefab. I'm not able of building that from the scratch for my own prefab. The animations I create can have just one Sprite Renderer and I'm not able to add more than one.

  • edited March 2022

    Still, if you can share uncropped images of all related objects I can better understand the situation.

    Your Hierarchy shot above shows that you have a separate Head object for each direction - same for the Helmet.

    If each object has a Sprite Renderer, then you can animate all of them together in a single animation - but you need to make sure that the Animator you're using to control them is above them in the Hierarchy - either on the character's root, or the sprite child. This isn't a characteristic of AC, though - but Unity's Animation system.

    With the character in the scene, select the object with the Animator on it and open up Unity's Animation window. Lock it via the top-right corner, and you should then be able to animate the properties of any object that's below the Animator in the Hierarchy.

    In your case, you'll want your animations to animate both the head and the helmet objects - for example, the Head_D animation can enable both the Head_D and Helmet_D sprites, while disabling the others. In a separate layer, you'd then be able to control the Head/Helmet toggling - but that's a later step.

  • nhtnht
    edited March 2022

    Hello.
    Here I give you a link with many screenshots.
    https://imgur.com/a/2FtCHWd
    My idea was to build the character like the prefab of "Sprites Unity Complex".
    The character sometimes removes the helmet so the body is separated from the head. I'm trying to trace your prefab, but I'm not able to create an animation with different layers so now my character is displayed with all the different faces together. I'll tried to read all the documentation and watch videos but I don't really how to do it.

  • edited March 2022

    It looks like your Animator sub-layers have no Weight value, which means they won't have an affect on the animation. You can increase a layer's weight via the cog menu in the list on the left of the Animator window.

    Where is the Animator component in the Hierarchy? Again, it's possible to animate all children of an object that has the Animator attached. Both this and the above are to do with Unity's standard Animation workflow, however.

    When it comes to using Sprites Unity Complex, animation is driven via parameter values - so it's possible to test the animation without involving AC by temporarily clearing the Player component's "Animator parameter" fields and playing with the parameter values manually at runtime. I'd suggest first focusing on making the Animator work as intended without involving AC, and then have AC take control afterwards.

  • nhtnht
    edited March 2022

    I do really try to follow what you say but I don't know what do you mean. How can I add weight to the layers? What is the cog menu? If you mean the line under the sublayers in Animator I'm not able to modify anything.
    The Animator component is in the Body, just like your prefab. And I am doing copycat of the Complex one. Its name is BrainHead_SpritesUnityComplex. But the problem here is that I'm not able to create an animation with many sprite layers. That is what I was asking. I readed all the documentation available, even I read the animations, animator and everything in Unity manual and I'm not able to get it.
    I made the animation working previously with the standard one, but I want the head work separated from the body.

  • Controlling layer weights is a Unity animation concept - you can learn about this from Unity's official docs here: https://docs.unity3d.com/Manual/AnimationLayers.html

    I'm not able to create an animation with many sprite layers.

    To be clear: you're talking about animating multiple Sprite Renderers in one animation - as with the example animations?

    This too is all down to Unity's animation workflow - a tutorial that demonstrates the concept can be found here:

  • Thank you, I will review all these information.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.