Forum rules - please read before posting.

Setting up Character Animation 2D Track

edited June 2020 in Technical Q&A

First of all, thank you for including the Character Animation 2D Track! It sounds useful and I'd like to utilize it for a cutscene.

I've read through the Manual's description of it, and understand the concept of it -- that the correct direction's animation should play based on the direction the character is moving.

Been attempting to use it for awhile and haven't been able to fully understand it yet, so I was wondering if an explanation on how to use it can be given. I have read the manual on the 2D Track, and watched the video on the AC Timeline tracks.
Things done so far:
1) Unchecked Turn Root Object in 3D on the Player character.
2) In the Timeline, I've created an Character Animation 2D Track, and assigned/binded the character to it.
3) On another track in the Timeline, added a "Walk_L" animation clip that was created for the character (as originally I thought this was the way to "move" the character, though still unsure).

Here's a screenshot of the Timeline:
https://imgur.com/a/65qsKej

In another thread on the 2D Track it reads: "need to combine it with a regular Animation track (on the character's root) to move them around the scene.
For any part of the Timeline that the "Character Animation 2D" track is active, they should then automatically play their idle/walk animations based on their movement state each frame."

My understanding is the movement state for each frame is set in the Inspector for the "Character Animation 2D Shot" that can be added to the 2D Track (e.g. Animation when moving: Walk) -- is that correct? Also, what is meant by "combine it with a regular Animation track"; and how to do that?

The Manual reads:

While a clip from this track is active, it will cause the character's walk or run animation to play as they move around the scene. By default, they will turn to face the direction of the motion.

And the 1.70 update thread reads:

works by animating a character according to their movement - so if a character moves right (due to a separate Animation track), they will automatically play they're "walk right" animation.

Based on these descriptions, I originally thought this meant adding a directional animation clip (e.g. "Walk_L") in another track in the Timeline.
Also, I tried adding a new custom animation moving the character from the right to the left (Position.x) -- in another track in the Timeline. Though that didn't work either.

Can more details about how the character moves (due to a separate Animation track) be provided?

One other question:
The manual reads that the 2D Track doesn't support previewing. Does that mean the preview isn't shown when you scrub through the Timeline; Or when you preview the game in "Play" mode?

Using Unity 2019.4.0f1 and AC 1.71.4. And Sprites Unity, with the Unity 2D Animation Package for characters.

Thank you for any assistance that can be provided!

Comments

  • Don't assign an animation clip (e.g. Walk_L) in the Timeline - that's what this Timeline track will handle automatically.

    The animation track you need to create needs to control the character's position only. The "Character Animation 2D" track will play the directional animation according to the character's change in position between frames.

    When you animated the position.x property, was the "Walk_L" animation track also running?

    Be sure also to animate the position of the character's root - not their "sprite child".

    Though it should work with the 2D Animation package, best to try it without to be sure that it's not a contributing factor. Try animating the 2D Demo game's Player prefab, Brain2D, as this should work without any issues.

    The manual reads that the 2D Track doesn't support previewing. Does that mean the preview isn't shown when you scrub through the Timeline; Or when you preview the game in "Play" mode?

    Both - it will only work when being played naturally at runtime.

  • edited June 2020

    Thank you for the information!

    Be sure also to animate the position of the character's root - not their "sprite child".

    By this, do you mean place the Animator on the root? Or to animate the Player character while recording with the root selected?

    When I was originally setting up the character using the 2D Animation Package, it was mentioned in another thread to move the Animator to the root. Also, the Player won't move when clicking during gameplay if the Animator is on the root (NPCs can move when following, though).

    Hierarchy structure of this character looks like this: (older screenshot from an older Unity version, though the structure's still the same)
    https://imgur.com/a/T1cJ00H

    Thank you again for any assistance!

  • I mean to place the Animator on the root, yes.

    You may be able to get away with placing a second Animator on the root - as opposed to moving it completely - and only use it in Timeline. But the presence of 2D Animation Package shouldn't prevent the Animator from being able to be on the root.

    If the Player doesn't move when attached to the root, be sure that "Apply Root Motion" is unchecked in the Animator component.

  • edited July 2020

    The line in my previous reply above meant to say: "...using the 2D Animation Package, it was mentioned in another thread to move the Animator to the Sprite Child".
    In any case, I was able to get the Player character moving again with the Animator on the root, just by removing the animations from the Controller and re-dragging them back in.

    Also, the 2D Animation Track works somewhat -- though off and on.
    The character disappears / goes invisible when the Character Animation 2D Shot is added and its start point is at 0. Though it seems to work and plays the Walk Left animation when its start point is delayed, like this:
    https://imgur.com/a/TUp0oRC
    Unsure why that may be.

    Also, when adding another keyframe to move the character in the opposite direction (Right), the character disappears when reaching that keyframe:
    https://imgur.com/a/f7RTnnH

    Might using the Frame Flipping script with Sprites Unity Complex have anything to do with that?

    I believe I see the benefits of using the 2D Animation Track (wouldn't need to manually add the animation tracks to the Timeline, etc.), though I feel personally, a preview of some sort is needed to create an accurate cutscene animation. Unsure if it's currently possible for that with Unity, though just mentioning constructive feedback.

    Instead, I was hoping to manually animate the characters' positions in the Timeline, while dragging in the animations into another animation track and repeating them during the duration of the movement.

    In this thread, it's mentioned that it should work to manually animate a character by shifting their position and playing a walking animation.

    I believe that's similar to how this shows (at 6:06) manually animating the characters' positions, while dragging in the animations into another animation track and repeating them during the duration of the movement:

    I tired this and it does appear to animate properly when the Timeline is scrubbed back and forth (before entering "Play" mode). It also properly plays the "Walk_L" animation in "Play" mode, though, again the character disappears when reaching a "Walk_R" animation:
    https://imgur.com/a/ZNfpT1Q

    It seems like using Frame Flipping with Sprites Unity Complex may have something to do with it, as technically in the Controller the BlendTree only has the "Left" facing animations.
    Though unsure why it would animate corrently when scrubbing the Timeline, but then the character disappears / doesn't play the Right-facing animation when in Play mode.

    I'd really appreciate any further help that can be given!

  • The character disappears / goes invisible when the Character Animation 2D Shot is added and its start point is at 0.

    This is at runtime? Pause the game when this occurs and view its Inspector and the object in the Scene view - does it give any clues as to why it's not showing?

    Might using the Frame Flipping script with Sprites Unity Complex have anything to do with that?

    Easiest way to tell is to disable it and see!

    The script only kicks in in play-mode, and there may be a conflict if your animations / tracks affect the character's scale.

    Try this variant instead - it'll flip the SpriteRenderer in the X direction as opposed to messing with the scale:

    using UnityEngine;
    using System.Collections;
    using AC;
    
    public class SUC_FrameFlip : MonoBehaviour
    {
    
        [SerializeField] private AC.Char character;
        [SerializeField] private SpriteRenderer spriteRendererToFlip;
        [SerializeField] private AC_2DFrameFlipping frameFlipping;
    
        private void Update ()
        {
            if (frameFlipping == AC_2DFrameFlipping.None || character == null || transformToFlip == null)
            {
                return;
            }
    
            bool doFlip = false;
            float spriteAngle = character.GetSpriteAngle ();
    
            if (frameFlipping == AC_2DFrameFlipping.LeftMirrorsRight)
            {
                if (spriteAngle >= 0f && spriteAngle < 180f)
                {
                    doFlip = true;
                }
            }
            else if (frameFlipping == AC_2DFrameFlipping.RightMirrorsLeft)
            {
                if (spriteAngle > 180f && spriteAngle <= 360f)
                {
                    doFlip = true;
                }
            }
    
            spriteRendererToFlip.flipX = doFlip;
        }
    
    }
    
  • edited July 2020

    Thank you very much for that! It's appreciated!

    After more testing, it appears it wasn't the Frame Flipping, as I tried disabling it, and the character still vanishes when changing direction from left to right.

    Also, the Character Animation 2D Shot seems to now work when the start point is at 0.

    Also, if I'm using it correctly, I've seen that the 2D track does allow previewing in "Play" mode. Granted I did upgrade the Timeline in the Package Manager recently to the version just released in June. So given that, if a preview shows at least in Play mode, I'd definitely use the 2D Animation Track for many occasions.

    That is if I can solve the character vanishing part.

    I tracked down why the character disappears, though haven't solved completely what causes it. Essentially, when the Left Walk animation plays it's fine (Position.x to the left is recorded).
    Though when it switches to the Right Walk animation, it deactivates the right direction sprite (as well as the others):
    https://imgur.com/a/Z008wZa

    Hoping screenshots can provide enough information for now:

    Here's the Player's Inspector:
    https://imgur.com/a/GwRckQm
    Also the Hierarchy structure of this character looks like this:
    https://imgur.com/a/T1cJ00H
    (Note: a "Transform", "Follow Sorting Map, and "Sorting Group" component are on the Sprite Child).

    And the "Walk" State BlendTree in the Animator:
    https://imgur.com/a/Gd2k6uY
    (Note, the "Idle" state Blendtree has a similar structure)

    Also, if NPCs are also able to be animated as part of the 2D Animation Track, I also tried animating an NPC with it. Though when the 2D Animaion Track ran, all directions appeared to be disabled, even during the Left Walk animations:
    https://imgur.com/a/WBiNubl

    Will continue to keep working with the Animator and the 2D Animation Track.

    Thank you, any help that can be provide is really appreciated!

  • The 2D Animation track requires AC to be running for it to work - so it's not possible to have it preview anything in Edit mode.

    As for the main issue, it shouldn't be the case that AC itself is disabling any GameObjects directly - it's likely the Animator itself doing this.

    When switching to the Right Walk animation, what is the state of the Animator? Pause the game at this time, and try to find out which animation(s) it's playing that's causing this to occur. Likely these'll be based on your Animator Parameter values - so check to see if any of those look wrong.

  • edited July 2020

    Thank you for the advice!

    A good amount of time was spent looking into the Animator Controller.
    Here's a screenshot of what the Blend Tree looks like at the exact moment when the GameObjects are disabled:
    https://imgur.com/a/1bEc1pJ

    One thing to note is that they get disabled starting at when the Angle's value is at 180 (assuming this is where the character may be turning). The Angle at first is 90 (when Position.x animated to the Left), then 180 for a split second, then 270 when Position.x is animated to the Right (where the GameObjects continue to be disabled). Just FYI: the scale isn't being animated, just the direction / position.

    It seems that it's something between the Controller, as well as the Timeline itself (seeing as the character walks in Game Play mode fine). Perhaps not so much the 2D Animation Track, though was wondering if any advice can be given in any case. Been stalled here for awhile, and wanted to attempt to figure it out before moving ahead and creating any more characters.

    Here's a screenshot of the BlendTree:
    https://imgur.com/a/Gd2k6uY

    Are these the proper Threshold settings for a character that walks in four directions (Left, Right, Up, Down), has Angle Snapping set to Ninety Degrees, and is using the below modified code to account for "Up" and "Down" directions?
    Also, can AC's Direction Integers be used if using the Frame Flipping script, or only the Angle value (float) parameter? My thought is that it's the latter.

            using UnityEngine;
            using System.Collections;
            using AC;
    
            public class SUC_FrameFlip : MonoBehaviour
            {
    
                [SerializeField] private AC.Char character;
                [SerializeField] private Transform transformToFlip;
                [SerializeField] private AC_2DFrameFlipping frameFlipping;
    
                private void Update ()
                {
                    if (frameFlipping == AC_2DFrameFlipping.None || character == null || transformToFlip == null)
                    {
                        return;
                    }
    
                    bool doFlip = false;
                    float spriteAngle = character.GetSpriteAngle ();
    
                    if (frameFlipping == AC_2DFrameFlipping.LeftMirrorsRight)
                    {
                        if (spriteAngle >= 45f && spriteAngle < 135f)
                        {
                            doFlip = true;
                        }
                    }
                    else if (frameFlipping == AC_2DFrameFlipping.RightMirrorsLeft)
                    {
                        if (spriteAngle > 225f && spriteAngle <= 315f)
                        {
                            doFlip = true;
                        }
                    }
    
                    if ((doFlip && transformToFlip.localScale.x > 0f) || (!doFlip && transformToFlip.localScale.x < 0f))
                    {
                        transformToFlip.localScale = new Vector3 (-transformToFlip.localScale.x, transformToFlip.localScale.y, transformToFlip.localScale.z);
                    }
                }
    
            }
    

    One other thing to note: I also created other versions of the Controller, not using Blend Trees, but using direct transitions between the animations -- though with those other Controllers the character still vanishes due to the GameObjects being disabled when the direction changes in the direct opposite direction: 90 to 270 Angle value.

    Interestingly, when I changed the BlendTree Threshold values to these (just to test it), the character appears to animate Left, to Right using the Timeline / 2D Track without vanishing or all GameObjects being disabled:
    https://imgur.com/a/L1D7QLM
    Though, likely as expected, the character doesn't animate correctly in the normal gameplay mode with these values.

    Thank you very much for any further advice or guidance that can be provided!

  • Here's a variant of the frame-flipping script that reads the Direction integer instead of the Angle float:

    using UnityEngine;
    using System.Collections;
    using AC;
    
    public class SUC_FrameFlip : MonoBehaviour
    {
    
        [SerializeField] private AC.Char character;
        [SerializeField] private Transform transformToFlip;
        [SerializeField] private AC_2DFrameFlipping frameFlipping;
    
        private void LateUpdate ()
        {
            if (frameFlipping == AC_2DFrameFlipping.None || character == null || transformToFlip == null)
            {
                return;
            }
    
            bool doFlip = false;
            int direction = character.GetSpriteDirectionInt ();
    
            if (frameFlipping == AC_2DFrameFlipping.LeftMirrorsRight)
            {
                if (direction == 1)
                {
                    doFlip = true;
                }
            }
            else if (frameFlipping == AC_2DFrameFlipping.RightMirrorsLeft)
            {
                if (direction == 3)
                {
                    doFlip = true;
                }
            }
    
            if ((doFlip && transformToFlip.localScale.x > 0f) || (!doFlip && transformToFlip.localScale.x < 0f))
            {
                transformToFlip.localScale = new Vector3 (-transformToFlip.localScale.x, transformToFlip.localScale.y, transformToFlip.localScale.z);
            }
        }
    
    }
    

    Though, it should be disabled for the moment - while it's likely not the cause of the issue, it will be adding further complication to it.

    It sounds like this is to do with the Animator than AC - since the parameter values are as they should be and nothing else should be controlling the enabled state of the child GameObjects.

    Are you disabling these children as part of the walk / idle directional animations themselves? It may be worth moving these to separate animations that play in a sub-layer of the Animator, so that the walk/idle etc animations only affect the motion of the intended direction child - not which objects are enabled/disabled.

    What was the actual issue, in terms of what animations were playing, when you offset the BlendTree's threshold values by 45 degrees? If that fixed the issue, it may be worth sticking with that. The Blend Tree could be made to use a second float parameter instead of Angle, and then a simple script attached could set this "FakeAngle" parameter's value to e.g. Angle + 45 every frame, i.e.:

    void Update ()
    {
        animator.SetFloat ("FakeAngle", animator.GetFloat ("Angle") + 45f);
    }
    
  • edited July 2020

    First, thank you for all of your advice, and providing the Frame Flipping script for Direction Integers!
    May very likely just use Direction Integers if I don't end up continuing with Blend Trees, so it may come in very handy!

    First, by sub-layers, that meant the different layers in the Animator, correct? (And not sub-states):
    https://imgur.com/a/RyflkuG

    Did some testing with sub-layers, and have seen that Blend Trees (which I was considering using) aren't easily able to be "synced" from layer to layer.
    Sub-layers and syncing are mainly meant for Animator Controllers with direct transitions between individual animations (since the sub-layer's Inspector allows you to drag in an individual animation only, and not a full Blend Tree, obviously):
    https://imgur.com/a/8rwmWVR

    So, if things progress in the "direct transition" direction, then I'll likely opt for the Direction Integers, just to make things a bit more straightforward.

  • edited July 2020

    First, by sub-layers, that meant the different layers in the Animator, correct?

    That's right. Though I'm not sure what I'm looking at in the second screenshot - a sub-layer should look similar to the base layer, just allowing you to play two animations at once (one to set the enabled state of gameobjects, another to play the character animation).

  • During creating and testing the sub-layer, I discovered what the issue was, which ended being much simpler, and the sub-layer wasn't needed. Your advice did help me to solve it though, so thank you!

    As mentioned, the animations had GameObjects being disabled as part of the walk / idle directional animations. To solve the issue, each direction's GameObject also needed to be "enabled" in the first keyframe to "activate" it each time the direction changed in the Timeline.
    For example, this was needed to be added for the Left walking animation:
    https://imgur.com/a/Kh7uwRc

    Didn't realize that when creating animations in the the Timeline, it was necessary to do that -- since the walk / idle animations all played fine during normal Play mode (with have just the other 3 directions disabled, and no extra "enabling" of the current direction).

    Anyway, with that cleared up, I wanted to share something that may be useful to see.

    Both - it will only work when being played naturally at runtime.

    My understanding of this was no animation preview should play at all when using the 2D Animation Track on a Timeline, except for when the game is built (using "Build and Run", etc.).

    Take a look at this video though. It shows the character walking animated with the 2D Track in "Play" mode:

    Even though it doesn't show animations in the preview when scrubbing in the Timeline, the ability to see how / where the character moves to, just in the "Play" mode can be still very useful.

    Sometimes the Idle animation didn't always match up in the beginning pose of the animation, though I believe that's really to be expected in Unity -- when transitioning from the end of the Timeline to the gameplay. I plan to either extend / shorten the Timeline slightly in those cases (which I've done already to fix it); or continue tweaking the general length of the animations for what works best.

  • Apologies for any confusion - it should work in Play mode, just not Edit mode. There should be no difference in behaviour between Play mode, and in a Build.

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.