Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Why Might Adding An Animation Controller Cause a Character to Lose Movement?

Discussion in 'Getting Started' started by redapplesonly, Apr 2, 2023.

  1. redapplesonly


    Nov 8, 2021
    Hi all, I'm a Unity newbie, working through this awesome tutorial to build my first 2D game. The game is a side scroller, where you control a player character, using only the <-- and --> characters (left and right arrows). The character's image is built from multiple sprites (see below). When I fire up the game and press the arrow keys, the character zips to the right and the left perfectly, as it should. But then, when I add an animation, the character properly animates, but no longer moves right or left. The animation seems to be interfering with moving the character.

    First, some context: I am on Unity 2022.2.10f1 on a Windows 11 machine. My version of Microsoft Visual Studio is Version 17.5.3.

    Now to describe my character. (Her name is "Eater", as she's a girl who races left and right across the bottom of the screen trying to catch and eat food that is randomly dropping from the sky.) As I said, the Eater character is made up of multiple sprites. In an attempt to keep organized, I hierarchically placed those sprites in parent containers. (See "Character Heirachy" image, attached) The following code is attached to the player to cause her to move:

    Code (CSharp):
    1.     void Start()
    2.     {
    3.         rb = GetComponent<Rigidbody2D>();
    4.         anim = GetComponent<Animator>();
    5.     }
    7.     // Update is called once per frame
    8.     void Update()
    9.     {
    10.         if (input != 0)
    11.         {
    12.             // Player is moving
    13.             anim.SetBool("isRunning", true);
    14.         }
    15.         else
    16.         {
    17.             anim.SetBool("isRunning", false);
    18.         }
    20.         if (input < 0)
    21.         {
    22.             // Player is moving right
    23.             transform.eulerAngles = new Vector3(0, 0, 0);
    24.         }
    25.         else if (input > 0)
    26.         {
    27.             // Player is moving left
    28.             transform.eulerAngles = new Vector3(0, 180, 0);
    29.         }
    30.     }
    32.     private void FixedUpdate()
    33.     {
    34.         // We use FixedUpdate() (not Update()) because the Player object uses RigidBody 2D Physics
    35.         input = Input.GetAxisRaw("Horizontal");
    36.         // Move the player:
    37.         rb.velocity = new Vector2(input * speed, rb.velocity.y);
    38.     }
    This all works just great. The script is attached to the "Eater Character" game object, which is the top object in the hierarchy.

    But then, I created an Animation Controller with two Animations ("Idle" and "Running") and got the transitions to work just how I want them. So far, so good.

    But when I attach the Animation Controller to "Eater Character" - the same object where I attached the C# script - something weird happens. Now when I play the game, the Eater character animates just fine in both her Idle and Running states. If I push the <-- or --> keys, she'll turn to face the direction that I wish to go. But she never physically moves left or right! She simply remains locked in position.

    If I remove the Animation Controller, I revert back to the original behavior - the character can move just fine. Adding the Animation Controller seems to be interfering with movement.

    (See image "Components" for a view of both the C# script and the Animator Controller in the Component Inspector)

    I know this is prob not enough information, but does anyone see what I'm doing wrong? Thank you.

    Attached Files: