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.