Flipping a sprite horizontally
Perhaps the simplest 2D animation is a simple flip, from facing left to facing right, or facing up to facing down, and so on. In this recipe we'll add a cute bug sprite to the scene, and write a short script to flip its horizontal direction when the Left and Right arrow keys are pressed.
Getting ready
For this recipe, we have prepared the image you need in a folder named Sprites
in folder 1362_03_01
.
How to do it...
To flip an object horizontally with arrow key presses, follow these steps:
- Create a new Unity 2D project.
- Import the provided image
EnemyBug.png
. - Drag an instance of the red Enemy Bug image from the Project | Sprites folder into the scene. Position this GameObject at (
0
,0
,0
) and scale to (2
,2
,2
). - Add an instance of C# script class
BugFlip
as a component to your Enemy Bug GameObject:using UnityEngine; using System.Collections; public class BugFlip : MonoBehaviour { private bool facingRight = true; void Update() { if (Input.GetKeyDown...