using UnityEngine; public class EnemyMovement : MonoBehaviour { [SerializeField] private float moveSpeed = 1f; private Rigidbody2D myRigidbody; private bool movingRight = true; // Track the direction void Start() { myRigidbody = GetComponent(); } void FixedUpdate() { // Move the enemy horizontally myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y); } void OnTriggerExit2D(Collider2D other) { // Change the direction and flip the enemy moveSpeed = -moveSpeed; FlipEnemyFacing(); } void FlipEnemyFacing() { // Flip the sprite by changing local scale based on the movement direction movingRight = !movingRight; Vector3 theScale = transform.localScale; theScale.x *= -1; // Flip horizontally transform.localScale = theScale; } }