Search Unity

[Animation in Timeline] Character doesn't keep the animation state after Timeline ends.

Discussion in 'Timeline' started by SamTerrae, Jan 29, 2020.

  1. SamTerrae

    SamTerrae

    Joined:
    Sep 10, 2019
    Posts:
    11
    Hello guys, I'm just new to Unity and I have some troubles with the Animation of my character in Timeline.

    I'm making a 2D top-down game like Pokémon.

    <Notice> WALL OF TEXT...

    Describe the trouble:
    1. (Solved) First, I couldn't move my character around after the Timeline has ended. Then, I follow some guidance on the Internet and write the code below, however, solve my problem.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Playables;
    5.  
    6. public class TimelineManager : MonoBehaviour
    7. {
    8.     public Animator playerAnimator;
    9.     public RuntimeAnimatorController playerAnim;
    10.     public PlayableDirector director;
    11.  
    12.     private MonoBehaviour scriptMovement;
    13.  
    14.     private void Awake()
    15.     {
    16.         scriptMovement = GetComponent<PlayerMovement>();
    17.     }
    18.  
    19.     void OnEnable()
    20.     {
    21.         playerAnim = playerAnimator.runtimeAnimatorController;
    22.         playerAnimator.runtimeAnimatorController = null;
    23.  
    24.         scriptMovement.enabled = false;
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         if (director.state != PlayState.Playing)
    31.         {
    32.             playerAnimator.runtimeAnimatorController = playerAnim;
    33.  
    34.             scriptMovement.enabled = true;
    35.         }
    36.     }
    37. }
    2. (Solved but confused a lot) Then, I'm stuck in the question how to keep the state (including position, animation...) of my character after Timeline ends. After looking for many answers on the Internet, I did solve the prob. I've changed my Char's Rigidbody2D to Kinematic and tick at the box "Apply Root Motion" in Animator component. Everything went smoothly.
    In the process of finding out the answer, I accidentally disabled the script decribed above. It turned out that when I configure the components' properties like that, the issue with character's movement after Timeline ends no longer appears... I don't know how...

    3. Then, so far, I have a little problem after Timeline ends (again :<). When my character goes right and stops, I want him to keep the state "IdleRight". My character, however, keeps turning his face to the screen instead. I change the "Post-extrapolate" option to "Continue" to infinity with the hope that my character will stay the same as the last key frame has ended. But it's hopeless.

    Here some of my components settings and pictures show the problem elaborately:
    1. Player's Components & Timeline Settings:


    2. Which I expected after Timeline:


    3. Which it really is afterwards:


    I hope someone could explain this to me... Thank you you guys anyway.
     
  2. SamTerrae

    SamTerrae

    Joined:
    Sep 10, 2019
    Posts:
    11
    Somehow, I think the prob comes from the Animator itself :< I'm using Blend Tree to control the Animation when character moving around.
    I set up 2 parameters "MoveX" and "MoveY" for 2D Simple Directional and use GetAxisRaw method.
    I believe when the Timeline processed, it didn't SetFloat to the parameters. As a consequence, when the Animator took the control back, it would use the Animation representing 2 parameters in the Blend Tree.
     
  3. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    That seems correct to me. The animator controller is still running while timeline is running, so when the timeline ends it will dictate the state of the object.
     
  4. SamTerrae

    SamTerrae

    Joined:
    Sep 10, 2019
    Posts:
    11
    Do you have any idea to fix that because I tried to use signal receiver for my character. The signal receiver component, however, couldn't access to the method SetFloat of the Animator Controller.
    That was the only idea I can follow so far but it seems to be hopeless.
    Please tell me if you come up with any other idea. Thank you for all of these.
     
  5. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    Ah...SetFloat takes two values, but UnityEvent only has one. You can work around that by using a script like this one to set the id through on a separate monobehaviour.

    Attach it to the same component as the animator, set the id you want, then set the signal receiver to call AnimatorHelper.SetFloat().

    A better solution would be a set of markers/receivers that allow you to do this directly - maybe someone else has one that can share? But this should work for your use case.
     

    Attached Files:

  6. SamTerrae

    SamTerrae

    Joined:
    Sep 10, 2019
    Posts:
    11
    That's really nice of you to help me out. I'll try this. You are my savior.