0
\$\begingroup\$

I am beginning to use Unity's Timeline for the first time.

I am creating a simple intro sequence where the Animator Gameobject does a position displacement animation (the movements and rotations are too complex to make some sort of waypoint system, making an Animation was the best way to go). And then, after the sequence is done, I reposition the parent so its Animator child has a local position of { 0, 0, 0 }.

The problems is that when I do that, the child offsets itself and I realized it has to do with the animation and the root motion, which the Timeline activates on its own but ignores if I set it to false.

The hierarchy of the objects is as simple as this:

  • Parent
    • Child Animator

The main code that does all the aforementioned is the following:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

[RequireComponent(typeof(PlayableDirector))]
public class TEST_MoskarIntroAnimation : MonoBehaviour
{
    [SerializeField] private Transform parent;
    [SerializeField] private Animator animator;
    private PlayableDirector director;

    /// <summary>TEST_MoskarIntroAnimation's instance initialization.</summary>
    private void Awake()
    {
        director = GetComponent<PlayableDirector>();
    }

    /// <summary>TEST_MoskarIntroAnimation's starting actions before 1st Update frame.</summary>
    private IEnumerator Start ()
    {
        director.Play();
        
        float duration = (float)director.duration;
        WaitForSeconds wait = new WaitForSeconds(duration);

        yield return wait;

        Debug.Log("[TEST_MoskarIntroAnimation] Sequence is finished...");

        animator.transform.SetParent(null);
        parent.position = animator.transform.position;
        animator.transform.SetParent(parent);
        animator.transform.localPosition = Vector3.zero;
        animator.transform.localRotation = Quaternion.identity;
    }
}

I have tried:

Which is reflected on the following class (which is not reflected on the previous sample code but I used for testing, enabling/disabling flags before and after the sequence):

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

[Flags]
public enum Override
{
    None = 0,
    DontOffsetFromParent = 1,
    ApplyRootMotion = 2,
    ApplyBuiltinRootMotion = 4
}

[RequireComponent(typeof(Animator))]
public class OnAnimatorMoveOverrider : MonoBehaviour
{
    [SerializeField] private Override _overrideActions;     /// <summary>Things to override on OnAnimatorMove.</summary>
    private Animator _animator;                             /// <summary>Animator's Component.</summary>

    /// <summary>Gets and Sets overrideActions property.</summary>
    public Override overrideActions
    {
        get { return _overrideActions; }
        set { _overrideActions = value; }
    }

    /// <summary>Gets animator Component.</summary>
    public Animator animator
    { 
        get
        {
            if(_animator == null) _animator = GetComponent<Animator>();
            return _animator;
        }
    }

    /// <summary>Callback for processing animation movements for modifying root motion.</summary>
    private void OnAnimatorMove()
    {
        if((overrideActions | Override.DontOffsetFromParent) == overrideActions)
        {
            Transform parent = transform.parent;

            if(parent == null) return;

            Vector3 position = parent.position;
            position = animator.deltaPosition;
            parent.position = position;
        }
        if((overrideActions | Override.ApplyBuiltinRootMotion) == overrideActions) animator.ApplyBuiltinRootMotion();

        animator.applyRootMotion = ((overrideActions | Override.ApplyRootMotion) == overrideActions);
    }
}

But either it does nothing or does not let the Timeline to do the animation. I don't want to patch a solution where I animate a copy and after the sequence I deactivate that copy and put the one that plays on the scene, since it is something that would be implemented to many more characters and they have many dependencies on other components, and I don't feel that it would be wise to make Animator-only copies given the fact that the originals may change, and so on.

Any more information I may need to provide, please let me know, and thanks in advance.

\$\endgroup\$

0

You must log in to answer this question.

Browse other questions tagged .