Search Unity

Bad Transition

Discussion in 'Animation' started by Silasi, Feb 10, 2019.

  1. Silasi

    Silasi

    Joined:
    Jul 25, 2016
    Posts:
    48
    I've made recording showing the problem. The idle is working well but when I start moving, the walk animation is glitching out.



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class CharacterAnim : MonoBehaviour
    7. {
    8.     Animator animator;
    9.     NavMeshAgent agent;
    10.  
    11.     void Start()
    12.     {
    13.         animator = GetComponentInChildren<Animator>();
    14.         agent = GetComponent<NavMeshAgent>();
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         float speedPercent = agent.velocity.magnitude / agent.speed;
    20.         animator.SetFloat("speedPercent", speedPercent);
    21.     }
    22. }
    23.  
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    I'm not sure what the problem is, but even if you got this approach working properly it would still be much better to use a blend tree for this. Just a simple 1D blend tree containing the two animations and using the speed parameter to blend between them.