diff --git a/Assets/Scripts/Animal.cs b/Assets/Scripts/Animal.cs index a8ca361..114d49f 100644 --- a/Assets/Scripts/Animal.cs +++ b/Assets/Scripts/Animal.cs @@ -6,6 +6,7 @@ using Random = UnityEngine.Random; public class Animal : MonoBehaviour { private Rigidbody2D _rigidbody; + private SpriteRenderer _spriteRenderer; private Sprite _animalSprite; private Animator _animator; private int _animMoveID; @@ -17,7 +18,8 @@ public class Animal : MonoBehaviour { private void Start() { _rigidbody = gameObject.GetComponent(); - _animalSprite = gameObject.GetComponent().GetComponent(); + _spriteRenderer = gameObject.GetComponent(); + _animalSprite = _spriteRenderer.GetComponent(); _animator = gameObject.GetComponent(); _animMoveID = Animator.StringToHash("moving"); @@ -35,7 +37,14 @@ public class Animal : MonoBehaviour { Random.Range(-1f, 1f), Random.Range(-1f, 1f)); direction.Normalize(); - + + // Flip sprite in moving Direction + if(direction.x > 0) { + _spriteRenderer.flipX = true; + }else if(direction.x < 0) { + _spriteRenderer.flipX = false; + } + _rigidbody.velocity = movementSpeed * direction; _animator.SetBool(_animMoveID, true); yield return new WaitForSeconds(randTime);