I'm having problems with my enemy AI

I am currently working on a platformer and I am having this problem my enemies where their AI throws them out of the level instead of moving from side to side on the platform they are on. I used this tutorial

. this is my script, any help would be very much appreciated

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class enemyAI : MonoBehaviour
{
   public float speed;
  // public float distance;
   public int maxHealth = 100;
   int currentHealth;
   private bool movingRight = true;
   public Transform groundDetection;
   public ParticleSystem blood;
  // public Transform player;
  

void Start()
{
    currentHealth = maxHealth;
    //player = GameObject.FindGameObjectWithTag("Player").transform;
}

public void TakeDamage(int damage)
{
     currentHealth -= damage;

     if(currentHealth <= 0)
     {
        
         Die();
     }
}

void Die()
{
     Debug.Log("Enemy died");
     blood.Play();
     Destroy(gameObject);
}
  void Update()
  {
    transform.Translate(Vector2.right * speed * Time.deltaTime);

   /* RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, distance);
    if(groundInfo.collider == false)
    {
        if(movingRight == true)
        {
            transform.eulerAngles = new Vector3(0, -180, 0);
            movingRight = false;
        }
        else
        {
            transform.eulerAngles = new Vector3(0, 0, 0);
            movingRight = true;
        }
    }*/
  }
}

.

Y-Hello there,

the script looks fine, and it’s working as expected.

Make sure to uncomment the raycast by removing “/" and "/” else the object wont know when it reached the edge.

Also make sure to have a BoxCollider2D on your ground, set the float “distance” greater than 0, (try something like a 100 at first, this really depends on your scene sizes)