unity 2d C # script follows the enemy player.

hello.

I have this script for development in 3D.
the enemy follows the player, but it does not work well for a 2d game.
How do you change in 2d so that the enemy follows the player, using the x-axis?

script:

using UnityEngine;
using System.Collections;

public class EnemyInseguePlayer : MonoBehaviour {

	public Transform player;
	public float playerDistance;
	public float rotationSpeed;
	public float moveSpeed;


	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
		playerDistance = Vector3.Distance (player.position, transform.position);

		if(playerDistance < 15f)
		{
			LookAtPlayer ();
		}
		if(playerDistance < 12f)
		{
			chase ();
		}
	
	}

	void LookAtPlayer()
	{
		Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
		transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationSpeed);
	
	}


	void chase()
	{
	      
		transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
		}
	}

I recently coded an enemy to only chase the player on an X axis in Unity2D. My object has a rigid body, so I can get away with using this:

void chase() {
	transform.position = Vector2.MoveTowards ( transform.position.x, player.position.x, moveSpeed * Time.deltaTime );
}

If it has to be restrained to the X-axis with code, try making a new Vector2 to specify the position, then plug that into the MoveTowards. Make sure your playerXAxis is being updated frequently. Just tested this and it works for me.

Vector2 playerXAxis;
playerXAxis = new vector 2 ( player.position.x, transform.position.y )

void chase() {
transform.position = Vector2.MoveTowards ( transform.position, playerXAxis );
}

I have found this code works, based on this video tutorial link text

using UnityEngine;
using System.Collections;

public class FollowAI : MonoBehaviour
{
	public GameObject target; //the enemy's target
	public float moveSpeed = 5; //move speed
	void Start()
	{
		target = GameObject.FindGameObjectWithTag("Player");
	}
	void Update()
	{

		Vector3 targetDir = target.transform.position - transform.position;
		float angle = Mathf.Atan2(targetDir.y,targetDir.x) * Mathf.Rad2Deg - 90f;
		Quaternion q = Quaternion.AngleAxis(angle,Vector3.forward);
		transform.rotation = Quaternion.RotateTowards(transform.rotation,q,180);
		transform.Translate(Vector3.up * Time.deltaTime * moveSpeed);
	}
}

If your view is not from above, but from side I suggest adding:

if(Vector3.Dot(transform.up, Vector3.down) > 0)
{
transform.Rotate(180f, 0f, 0f);
}