From e0212ff1744684b652229306688eef915bafb23e Mon Sep 17 00:00:00 2001 From: d-hain Date: Thu, 16 Jun 2022 16:36:56 +0200 Subject: [PATCH] Animal Sprite now flips in right direction when moving --- Assets/Scripts/Animal.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) 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);