Scripting for AI

I’m making a pong clone and having trouble scripting for the opponent. I’m trying to use transform.Translate and ball.transform.right to get it to move, but right now it only goes to the right, then gets stopped by the boundary of the playing field. Here is the script I am currently using:

using UnityEngine;
using System.Collections;

public class AI : MonoBehaviour

{

public GameObject ball;
public float speed;

void FixedUpdate ()
{
	transform.Translate (ball.transform.right * speed * Time.deltaTime);
}

}

How do I fix this?

You can actually do this editing the transform.position directly: // i guess that the ball.transform.right thing is the actual x axis position

if (ball.transform.right > transform.position.x) {
rigidbody2d.velocity = new Vector2 (speed, 0);
} else if (ball.transform.right < transform.position.x) {
rigidbody2d.velocity = new Vector2 (speed*-1f, 0);
}

I think you can do something like that (i think the code is not 100% correct but the basic idea is to attach a rigidbody that will only move on the x axis using the velocity).

More info here