Flip enemy based on player direction Unity 2D

Hey there. So what I want is fairly simple I believe. So I have a 2D enemy, and I want it to flip based on where the player is. I simply want the enemy to flip either left or right.

If the player is to the right of the enemy, the enemy should be looking right. If the player is on the left of the enemy, the enemy should be looking left. I would prefer this on the scale and not rotation. In this case, I want the snail to face the direction of the character in the X-axis, either left or right depending on where the player is.

Thank you.

PS: Would prefer it in C#.

139417-flip-enemy.gif

The SpriteRenderer of your snail has a “flipX” attribute that you can check to flip the sprite, so your code should look something like:

using UnityEngine;

[RequireComponent(typeof(SpriteRenderer))]
public class SnailFacePlayer : MonoBehaviour
{

	public Transform playerCharacter;
	private SpriteRenderer spriteRenderer;

	public void Awake()
	{
		this.spriteRenderer = this.GetComponent<SpriteRenderer>();
	}

	public void Update()
	{
		this.spriteRenderer.flipX = playerCharacter.transform.position.x < this.transform.position.x;
	}
}