- Home /
How to flip enemy 2D Sprite
In my game, I have the player castle in the center of the map and I have enemy spawn positions at the edge of the map. I am trying to find a way to make the enemy sprite face the direction they are moving in (left or right), I know about the "Flip" mechanic on the Sprite Renderer but all the tutorials I find are based on player input and none actually grab the direction of the gameobject.
Answer by AbdeCodeEnjoyer · Oct 20, 2021 at 03:47 AM
@rickycolon9540 If you want this script to run on each enemy instance, you could do something like this
public float speed;
public Transform target;
public SpriteRenderer spriteRenderer;
void Update ()
{
//direction of the target depending of this object
Vector2 direction = (target.position - transform.position).normalized;
//supposing that your object is initially looking right
spriteRenderer.flipX = direction.x < 0;
//if you are using another method of movement like rigidbody then do the same but with
//rigibody.velocity = direction * speed * Time.deltaTime
transform.position += direction * speed * Time.deltaTime;
}
Additionally if your target does not move, you can remove the first two lines in the update callback and put them in the start callback. (Therefore running your game a little faster since your target wont be moving)
So where could i fit that in my script?
public class Enemy : MonoBehaviour {
float Speed = .5f;
bool Left = false;
bool Right = false;
// Use this for initialization
void Start()
{
Left = true;
}
// Update is called once per frame void Update() {
if (Left == true)
{
transform.Translate(Vector3.left* Speed * Time.deltaTime);
}
if (Right == true)
{
transform.Translate(Vector3.right* Speed * Time.deltaTime);
}
}
void goRight() { Left = false; Right = true; }
void goLeft() { Left = true; Right = false; }
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "PatLeft")
{
goRight();
}
if (collision.gameObject.tag == "PatRight")
{
goLeft();
}
if (collision.gameObject.name == "flame")
{
Destroy(gameObject);
Destroy(collision.gameObject);
}
}
}
Your code already seems to work, All i can see that you could modify is the use of the two variables doing the same thing Instead of using two variables controlling your enemy's direction, I propose that you use only one variable called "goRight". Instead of having two conditions in the update you could have only one and write instead something like the following:
transform.Translate(goRight ? Vector3.right : Vector3.left * speed * Time.deltaTime);
As for the collision part it's already pretty well done you would just need to modify goRight method to something like:
//therefore having only one method didctating the value of the direction
void setDirection(bool isRight){goRight = isRight;}
then in the collsion part ounce again you would just have to reWrite it to something like
if (collision.gameObject.tag == "PatLeft")
setDirection(true);
if (collision.gameObject.tag == "PatRight")
setDirection(false);
if (collision.gameObject.name == "flame")
{
Destroy(gameObject);
Destroy(collision.gameObject);
}
Your answer