Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Malapropos · Dec 11, 2014 at 05:51 PM · 2danimatorcharacter-movement

Having animator component enabled prevents character movement

I have a 2D game with character gameobject that has following components:

Transform, CharacterMovement (Script), BoxCollider2D, SpriteRenderer, Rigidbody2D, AudioSource, Animator, Shader (Sprites/Default)

If animator component is enabled, the movement acts weird. If a character starts in a specific world position everything works fine. Character moves and animates as expected. If I move it anywhere else and start the game character doesn't move at all. Only things my animations change are Sprites or Scale, no position changes. Enabling or disabling root motion in animator doesn't make a difference. What I thought was particularly weird is that I can't move the character even using the editor translation arrows while the game is running. Here is my character movement script:

 using System.Collections;
 using UnityEngine;
 
 public class CharacterMovement : MonoBehaviour
 {
     public bool IsMoving;
     public AudioClip[] Steps;
 
     [HideInInspector]
     public float Speed;
     private Animator _animator;
     private AudioSource _as;
 
     void Start()
     {
         IsMoving = false;
         _animator = GetComponent<Animator>();
         _as = GetComponent<AudioSource>();
         Speed = GameManager.Instance.PlayerMovementSpeed;
     }
 
     void Update()
     {
         if ((Input.GetKeyDown(KeyCode.LeftArrow) ||
              Input.GetKeyDown(KeyCode.A)) && !IsMoving)
         {
             Debug.Log("Pressed left");
             StartCoroutine(MoveUntilStopped(Vector3.left));
         }
         if ((Input.GetKeyDown(KeyCode.RightArrow) ||
              Input.GetKeyDown(KeyCode.D)) && !IsMoving)
         {
             StartCoroutine(MoveUntilStopped(Vector3.right));
         }
         if ((Input.GetKeyDown(KeyCode.UpArrow) ||
              Input.GetKeyDown(KeyCode.W)) && !IsMoving)
         {
             StartCoroutine(MoveUntilStopped(Vector3.up));
         }
         if ((Input.GetKeyDown(KeyCode.DownArrow) ||
              Input.GetKeyDown(KeyCode.S)) && !IsMoving)
         {
             StartCoroutine(MoveUntilStopped(Vector3.down));
         }
     }
 
     public void StopMoving()
     {
         StopAllCoroutines();
         IsMoving = false;
         if (_animator.GetCurrentAnimatorStateInfo(0).IsName("WalkRight"))
             _animator.Play("IdleRight");
         if (_animator.GetCurrentAnimatorStateInfo(0).IsName("WalkLeft"))
             _animator.Play("IdleLeft");
         if (_animator.GetCurrentAnimatorStateInfo(0).IsName("WalkUp"))
             _animator.Play("IdleUp");
         if (_animator.GetCurrentAnimatorStateInfo(0).IsName("WalkDown"))
             _animator.Play("IdleDown");
     }
 
     private IEnumerator MoveUntilStopped(Vector3 direction)
     {
         Vector3 start = transform.position;
         float Progress = 0.0f;
 
         LayerMask mask = (1 << 0) | (1 << 8);    // Default mask
         RaycastHit2D hit = Physics2D.Raycast(start, direction, 100, mask);
         Vector3 targetLocation;
         Boulder boulder = null;
         IsMoving = true;
         if (hit)
         {
             if (hit.collider.tag == "Boulder")
             {
                 boulder = hit.collider.gameObject.GetComponent<Boulder>();
                 if (boulder && boulder.IsMoving)
                 {
                     IsMoving = false;
                     yield break;
                 }
             }
             targetLocation = hit.collider.gameObject.transform.position - direction;
         }
         else
             targetLocation = start + direction;
         float distance = Vector3.Distance(start, targetLocation);
 
         if (_animator && targetLocation != start)
         {
             if (direction == Vector3.up)
                 _animator.Play("WalkUp");
             if (direction == Vector3.down)
                 _animator.Play("WalkDown");
             if (direction == Vector3.left)
                 _animator.Play("WalkLeft");
             if (direction == Vector3.right)
                 _animator.Play("WalkRight");
         }
 
         while (Progress < 1.0f)
         {
             //yield return new WaitForEndOfFrame();
             Progress += Speed * Time.deltaTime / distance;
             transform.position = Vector3.Lerp(start, targetLocation, Progress);
             yield return null;
         }
         // Done moving
         if (_animator)
         {
             if (direction == Vector3.left)
                 _animator.Play("IdleLeft");
             if (direction == Vector3.right)
                 _animator.Play("IdleRight");
             if (direction == Vector3.up)
                 _animator.Play("IdleUp");
             if (direction == Vector3.down)
                 _animator.Play("IdleDown");
         }
         if (boulder)
             boulder.Push(direction);
 
         transform.position = targetLocation;
         IsMoving = false;
     }
 
     public void PlayRandomStepSound()
     {
         int SoundIndex = Random.Range(0, Steps.Length - 1);
         _as.PlayOneShot(Steps[SoundIndex]);
     }
 }

Input fires correctly and Speed has a proper value from GameManager (3). I hear the character step sounds that are played by the animations (by calling PlayRandomStepSound), but the character doesn't move, unless I have the character start from that specific location in the world (-10, 4, -1). If I disable the Animator component the movement works as expected but I need the animations to work. My Unity version is 4.6.0f3.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dangerdex · Mar 30, 2016 at 09:43 PM 0
Share

@malapropos was there ever a solution found to this? I am running into exactly the same issue :(

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Malapropos · Apr 03, 2016 at 08:42 AM

This project was made with Unity 4.x and eventually upgrading the project to Unity 5 fixed my problem. Must have been some fix implemented between versions.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Nerull22 · Dec 11, 2014 at 06:03 PM

This is just an educated guess, as I'm not entirely positive, but do you have "Apply Root" on the animator checked? Try it either way. I think it's supposed to be checked, but as I said, educated guess here.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Malapropos · Dec 11, 2014 at 06:13 PM 0
Share

That was one of the first things I tried. Enabling or disabling root motion in Animator component doesn't make any difference. This feels like issue that would be something $$anonymous$$or like that, but I haven't been able to find it. $$anonymous$$aybe I've been looking at it for too long.

avatar image andysctu Malapropos · Feb 18, 2016 at 06:07 PM 0
Share

Have you found a solution yet?

avatar image Malapropos andysctu · Apr 03, 2016 at 08:41 AM 0
Share

I don't remember my workaround at the time. When I later made a html5 release of the game and upgraded the project for Unity 5 it started working. Didn't change anything else and I still have no idea what was wrong. Sorry for the late response.

avatar image Delivionos · May 03, 2019 at 01:07 PM 0
Share

good educated guess, activating the Root inside the Animator Controller solved the problem

avatar image Donnald_Johnson · Mar 12, 2021 at 09:33 AM 0
Share

Thanks, kind stranger! You saved a lot of my time. It's 2021, but it worked.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Animation does not start 1 Answer

Best Way - Animation - Multiple GameObjects. 0 Answers

Animator Controller 2D RPG Best Practices 0 Answers

Create an idle motion with a 2D sprite. 0 Answers

Handling animation unity 4.3 2d game 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges