I want to make a PONG AI that can be beatable. but if i make a AI then either it will be unbeatable or ERROR. please help!!!

Here is the Code:
using UnityEngine;
using System.Collections;

public class Ai : MonoBehaviour {

Transform Ball;
Transform Enemy;

 float step;

public float speed;

Transform midline;

void Start ()
{
	Enemy = GameObject.Find ("Ball").transform;
}

void Update () 
{
	if (Ball < 0)
	{
		gameObject.SetActive (false);
	}
	else 
	{
		gameObject.SetActive (true);
		step = speed * Time.deltaTime;
	    transform.position = Vector3.MoveTowards (transform.position.x, Ball.position.x, step);

	} 
}

}

I am a beginner. So plz help me.

The difficulty in pong, from what I remember is when the AI can ‘see’ the ball. You could do this:

  • Add a circle to the AI-paddle
  • Define it’s radius based on difficulty level
  • Detect a collision of the ball with this circle (using Is Trigger)
  • In the collision trigger code, start moving the AI-paddle towards the ball
  • Limit the speed of the AI-paddle (can also be based on difficulty)

This will mean the paddle can miss the ball, if it reacts too late as the ball is either moving too fast or the paddle is out of position.