How do you make a characterController jump automatically when it reaches the edge of a platform?

I’m making a 2D game where several characterControllers (created as prefabs) move by themselves along platforms. I’m trying to make it so that they jump automatically when they reach the edge of a platform, but all of the jumping scripts that I’ve looked at so far don’t seem to work. I have it so that the game recognizes when the character is touching the ground and begins moving them automatically, but that’s it so far. Could someone just give me a basic idea of the lines that I will need to use to make this happen? Thanks for any help.

2 solutions I can think of:

You could place a trigger plane at the end of each platform and when entering the plane, it jumps. Since you are in 2D unity:

public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
CharacterController controller;
bool onceJump = false;
void Start(){
   controller = GetComponent<CharacterController>();
}
void Update() {
    if(controller.isGrounded){
       moveDirection = Vector3.zero;
   moveDirection.x = 1;
       moveDirection.x = speed;
       moveDirection = transform.TransformDirection(moveDirection);
       moveDirection *= speed;
    }
    moveDirection.y -= gravity * Time.deltaTime;
    controller.Move(moveDirection * Time.deltaTime);
}
void OnTriggerEnter(Collider col){
   if(col.transform.tag == "Player")moveDirection.y = jumpSpeed;
}

Other way is to use the isGrounded variable. As soone as your guy will start falling, bam it jumps.

public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
CharacterController controller;
bool onceJump = false;
void Start(){
	controller = GetComponent<CharacterController>();
}
void Update() {
    
    if (controller.isGrounded) {
        moveDirection = Vector3.zero;
    moveDirection.x = 1;
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;   
		onceJump = true;
    }
	else if (!controller.isGrounded && onceJump){
            moveDirection.y = jumpSpeed;
		onceJump = false;
	}
    moveDirection.y -= gravity * Time.deltaTime;
    controller.Move(moveDirection * Time.deltaTime);
}

Working and tested.

So if you are grounded, you walk, if you are not grounded, you jump but only once. If you are not grounded but you cannot jump, you are going down until you are grounded.