Animal Sprite now flips in right direction when moving
This commit is contained in:
parent
3c0cdf14e3
commit
e0212ff174
1 changed files with 11 additions and 2 deletions
|
|
@ -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");
|
||||||
|
|
||||||
|
|
@ -35,7 +37,14 @@ public class Animal : MonoBehaviour {
|
||||||
Random.Range(-1f, 1f),
|
Random.Range(-1f, 1f),
|
||||||
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);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue