Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Animation stuck on first frame

Discussion in 'Animation' started by BobbyCZ, Aug 22, 2021.

  1. BobbyCZ

    BobbyCZ

    Joined:
    Jul 12, 2021
    Posts:
    3
    Hello,
    I´m trying to animate my model of a knight and the animation "idle" is stuck on the beginning, weirdly if the knight transitiones to a diferent animation and back to "idle" the bar in the animator moves a bit yet animation itself doesen't change (see pictures below). I've tried tips such as "can transition to self" on/off, and setting "Wrap Mode" as Default/Loop, but nothing seems to work.
    Any help would be very much appritiated appreciated!

    The bar when first starting the game:
    when first starting.jpg

    The bar when transitioning from a diferent animation:
    after transition from animation.jpg

    Btw.: for the "RunCheck" Bool I'm using a simple script based on player.velocity, but I believe that shouldn't be any problem.

    Thanks again!
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Hey,

    I would think the issue has to be with the code. Have you tried starting the game with the code disabled? Does the issue still occur?

    If you're using CrossFade in Update, that could be the issue.
     
  3. KalOBrien

    KalOBrien

    Administrator

    Joined:
    Apr 20, 2021
    Posts:
    89
    Sounds like a code issue. Could you share it? Usually happens if the animation is being called constantly basically forcing it to constantly making it look as if its not playing.
     
    Unrighteouss likes this.
  4. BobbyCZ

    BobbyCZ

    Joined:
    Jul 12, 2021
    Posts:
    3
    You all are propably right because disabling the script let's the "Idle" animation play. Anyway, I couldn't find the issue myself so here is the script:
    (Thanks for helping!)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AnimationController : MonoBehaviour
    6. {
    7.     public Rigidbody2D player;
    8.     public Animator animator;
    9.     [Range(0.01f, 1f)] public float adjustspeed;
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.         if (player.velocity.x != 0)
    14.         {
    15.             animator.SetBool("RunCheck", true);
    16.         }
    17.         if (player.velocity.x == 0)
    18.         {
    19.             animator.SetBool("RunCheck", false);
    20.         }
    21.         animator.speed = Mathf.Abs(player.velocity.x) * adjustspeed;
    22.     }
    23. }
    (The float "adjustspeed" and line setting "animator.speed" seems to be ok, but I included it because one never knows :) )
     
  5. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Hey,

    So the reason your animation doesn't play is because on line 21 you're multiplying the animator speed by the player's x velocity, which is 0 by default. If you just put
    animator.speed = adjustspeed;
    it works without issue (if you set adustspeed to a high enough value).
     
    Last edited: Aug 24, 2021
  6. BobbyCZ

    BobbyCZ

    Joined:
    Jul 12, 2021
    Posts:
    3
    Hi,

    yes, you were right exactly hrát was happening so I fixed it. However, I wanted the animator.speed to depend on player.velocity, fór anyone wondering (or reading this thread later) I solved it by simply adding +1 to the line, so the value is always > 1.

    Thanks once more to everyone who helped me solve this! :)
     
    Unrighteouss likes this.