Search Unity

Animation transition

Discussion in 'Scripting' started by rennster200, Jul 18, 2019.

  1. rennster200


    Jun 21, 2019
    So, I created this script where my pet follows me and I all animations are working(2) but the transition is really bad... From Idleing to walking its like really bad...cant describe it but I assume you can imagine it.. How can I create transition between them? Parameters in animator controller seem not to work so this is my option...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    4. public class pet : MonoBehaviour
    5. {
    7.     public float speed;
    8.     public float range;
    9.     public Transform player;
    10.     public CharacterController controller;
    11.     public Animation move;
    12.     public Animation Idle;
    13.     static Animator anim;
    14.     private object target;
    16.     private void Start()
    17.     {
    18.         anim = GetComponent<Animator>();
    19.         move = GetComponent<Animation>();
    20.         Idle = GetComponent<Animation>();
    21.     }
    22.     void Update()
    23.     {
    24.         if (!inRange())
    25.         {
    27.             chase();
    28.         }
    29.         else
    30.         {
    31.             transform.LookAt(player.transform);
    32.             Idle.Play("idleLookAround");
    33.             //anim.SetBool("isWalking", false);
    34.             //anim.SetBool("IsIdle", true);
    35.         }
    36.     }
    37.     bool inRange()
    38.     {
    40.         return Vector3.Distance(transform.position, player.position) < range;
    42.     }
    43.     void chase()
    44.     {
    45.         Idle.Stop("idleLookAround");
    46.         move.Play("walk");
    47.         //anim.SetBool("IsIdle", false);
    48.         //anim.SetBool("isWalking", true);
    50.         transform.LookAt(player.position);
    51.         controller.SimpleMove(transform.forward * speed);
    53.     }
    54. }
  2. StarManta


    Oct 23, 2006
    This script....really shouldn't be working at all tbh. I'm surprised it doesn't error out every single frame. If it doesn't, then you must have gone out of your way to add an Animation component to your object. Based on the commented-out lines it looks like you attempted to use Animator and then gave up on it for some reason and changed to using Animation.

    "Animation" is the wrong class name. That class is deprecated (although admittedly, not all parts of the documentation are clear about this) and should generally never be used unless you're maintaining a project that was built circa 2011. Remove it from your script, if you have a component called "Animation" on your object remove that, if you were following a tutorial that told you to use it abandon that tutorial immediately.

    My best advice would be to follow a tutorial on the Animator component and learn how that's supposed to work. Basically you shouldn't ever be telling the animator "play this animation clip"; rather, you tell the Animator "We're walking forward" and you set up the Animator so that it understand that when you're walking forward, it needs to play clip XYZ.

    In the case of walking, you'll want to set up a blend tree, with your idle animation at the "0" position and your walking animation at the "1" position. In your script, you'll set the parameter for that blend tree to some value in between, and to smoothly transition, you just need to Lerp the value from one to the other. (This will be covered in more detail in the Animator tutorial)
    Lysander likes this.