Animal Sprite now flips in right direction when moving

This commit is contained in:
d-hain 2022-06-16 16:36:56 +02:00
parent 3c0cdf14e3
commit e0212ff174

View file

@ -6,6 +6,7 @@ using Random = UnityEngine.Random;
public class Animal : MonoBehaviour { public class Animal : MonoBehaviour {
private Rigidbody2D _rigidbody; private Rigidbody2D _rigidbody;
private SpriteRenderer _spriteRenderer;
private Sprite _animalSprite; private Sprite _animalSprite;
private Animator _animator; private Animator _animator;
private int _animMoveID; private int _animMoveID;
@ -17,7 +18,8 @@ public class Animal : MonoBehaviour {
private void Start() { private void Start() {
_rigidbody = gameObject.GetComponent<Rigidbody2D>(); _rigidbody = gameObject.GetComponent<Rigidbody2D>();
_animalSprite = gameObject.GetComponent<SpriteRenderer>().GetComponent<Sprite>(); _spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
_animalSprite = _spriteRenderer.GetComponent<Sprite>();
_animator = gameObject.GetComponent<Animator>(); _animator = gameObject.GetComponent<Animator>();
_animMoveID = Animator.StringToHash("moving"); _animMoveID = Animator.StringToHash("moving");
@ -36,6 +38,13 @@ public class Animal : MonoBehaviour {
Random.Range(-1f, 1f)); Random.Range(-1f, 1f));
direction.Normalize(); 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; _rigidbody.velocity = movementSpeed * direction;
_animator.SetBool(_animMoveID, true); _animator.SetBool(_animMoveID, true);
yield return new WaitForSeconds(randTime); yield return new WaitForSeconds(randTime);