Forum rules - please read before posting.

SUC Frame-Flipping script and cutscene animations

edited October 2020 in Technical Q&A

Hello everyone!

First of all, some details:
Using Unity 2020.1.9f1, AC 1.72.2, and the Unity 2D Animation Package to animate my Player and NPCs.
And using Sprites Unity Complex, and the animation states use BlendTrees.
Also, using the Sprites Unity Complex Frame-flipping script from the AC Wiki.
Also, Frame Flipping is set to: Right Mirrors Left in the characters' Inspector.

The SUC Frame-flipping script was slightly modified to account for "Up" and "Down" animations, to this:

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);
        }
    }

}

With this script, the main Player and NPCs animations flip nicely during normal gameplay and walking around the scene.

When it comes to cutscenes there's some issues with it though. For cutscenes, I create custom animation clips which are played with Animator parameters and Character: Animate -> Change parameter.

When I need a right-facing animation, I've been creating clips with the characters' right-facing set of sprites / bones activated (e.g. "Nathan_R"), and the rest inactivated in the clip. Hierarchy structure of the Player for reference:
https://imgur.com/a/T1cJ00H

It's easiest to animate in the scene's context with the character facing right. However, when the game is run in "Play" mode, the animation is flipped to the character facing left.
And vice versa, custom clips animated facing left, flip and animate facing right when played.

Narrowed it down to the Frame-flipping script causing this -- which was tested by unchecking the "SUC_Frame Flip" script in a character's root Inspector.

While testing options to solve this, I was able to get the character to face right (and animate walking right) by turning the Spawn marker arrow to point the opposite (left) direction, like this:
https://imgur.com/a/tisPFER

This might work for cutscenes where they spawn off-screen and walk into it. Though when a custom animation is played on the spot in the same gameplay scene, they aren't teleported to a marker. Rather a custom animation plays on the spot via Character: Animate -> Change parameter.

Looking for some advice for this. Would the only option be to somehow disable the Frame-flipping script each time a custom animation plays, so it doesn't affect the animation?

Thank you for any assistance that is provided!

Comments

  • Which way is the frame-flipping set?

    Your Hierarchy has more objects than necessary if you rely on frame-flipping, since NathanWalk_R can be used for both left and right animations - no need for NathanWalk_L.

    Check your Animator's parameter values and transitions - is the "Body angle float" set correctly when the custom animation is playing? The above script will read this value and apply it, even if it's not causing any transitions in the Animator at that time.

    As custom animations in Sprites Unity Complex are handled by changing parameters, there's no built-in way for it to know if a custom animation is playing, but one trick you can try is to define a "IsCustom" bool parameter in your Animator, and set this to True when custom animations play.

    This wouldn't be used by any transitions in your Animator, but would instead by read by your script, i.e.:

    if (character.GetAnimator ().GetBool ("IsCustom"))
    {
        // Do something
    }
    
  • edited October 2020

    Frame Flipping is set to: Right Mirrors Left in the characters' Inspectors.

    Your Hierarchy has more objects than necessary if you rely on frame-flipping, since NathanWalk_R can be used for both left and right animations - no need for NathanWalk_L.

    My understanding based on this is that "NathanWalk_R" isn't needed (since "Right Mirrors Left" is selected).
    Also, custom right-facing animations would need to all be animated in the Left-facing position, is my understanding correct? That may be a bit tricky to animate in context of a scene -- though if that's the way it needs to be done, that's the way it's to be done.

    There still might be an issue though -- as mentioned above, custom clips animated facing left, flip and animate facing right when played. If any left-facing animations are needed, how could they be created in that case?

    --

    The "Body float angle" is 270.0 before the custom animation occurs (the character is standing facing right). Then, during the custom animation (which is actually animated facing right in the "Animation" view), the Angle is still 270.0 (and the character flips and animates facing left).

    --

    but one trick you can try is to define a "IsCustom" bool parameter in your Animator, and set this to True when custom animations play.

    This wouldn't be used by any transitions in your Animator, but would instead by read by your script

    Regarding this last idea: I understand setting the "isCustom" bool to True, though unsure what the script would be doing when it is True (e.g. the "// Do something" part).
    Would you be able to elaborate more on that? Just trying to understand the idea better.

    Thank you!

  • I was assuming you were using Left Mirrors Right, since you say your custom animations are all right-facing.

    What of your standard animations, i.e. idle, walk, and talk? What's important to bear in mind when using frame-flipping is that all animations that can be flipped are made in the same direction. If your animations are all right-facing, set it to Left Mirrors Right and then the character will be flipped whenever they face left, so that the right-facing animation gets played.

    A body angle of 270 suggests the character is facing right, which would correctly flip the character if you're using Right Mirrors Left - since that assumes your animations are left-facing.

    Regarding the use of the IsCustom bool: if you wanted to ensure that frame-flipping never occurs when checked (and also cancels it if frame-flipping was occuring at the time it becomes checked), you can use:

    if (character.GetAnimator ().GetBool ("IsCustom"))
    {
        doFlip = false;
    }
    else if (frameFlipping == AC_2DFrameFlipping.LeftMirrorsRight)
    {
        // etc
    
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.