As an alternative to changing the X-component of the local scale, we could change the flipX Boolean property of the GameObject's SpriteRenderer component by changing the contents of our BugFlip script to the following:
using UnityEngine;
using System.Collections;
public class BugFlip : MonoBehaviour {
private bool facingRight = true;
private SpriteRenderer _spriteRenderer;
void Awake() {
_spriteRenderer = GetComponent<SpriteRenderer>();
}
void Update() {
if (Input.GetKeyDown(KeyCode.LeftArrow) && facingRight) {
_spriteRenderer.flipX = facingRight;
facingRight = false;
}
if (Input.GetKeyDown(KeyCode.RightArrow) && !facingRight){
_spriteRenderer.flipX = facingRight;
facingRight = true;
}
}
}
Once again, we are basing our code on the assumption that the sprite begins by facing right.