- Home /
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.
@malapropos was there ever a solution found to this? I am running into exactly the same issue :(
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.
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.
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.
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.
good educated guess, activating the Root inside the Animator Controller solved the problem
Thanks, kind stranger! You saved a lot of my time. It's 2021, but it worked.
Your answer
Follow this Question
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