# 2D zombie chase player AI (sprite)

Hello. I am new to ai and i want to make ai that chase player the chaser will be a zombie (sprite) the player is a sprite too. The zombie should chase the player in x only the game is like mario but i dont know how to do that i tried doing lookat on cube but then it looks at the x z y that is not what i want soo i tried doing this on sprite:

``````using UnityEngine;
using System.Collections;

public class EnemyChase : MonoBehaviour {

public Transform player;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

Vector3 lookpos = player.position;
lookpos.y = transform.position.y;
transform.LookAt (lookpos);

}
}
``````

then the sprite does look in y only but the sprite is we can say invisible i tried and x too the same the zombie sprite is invisible and by invisible i mean its rotating in that direction that you cannot see it in 2d. Anyway soo how can i make a simple ai that would chase the player in both directions left and right? the player can jump to another direction thats why i need the zombie to chase at left and right directions. I would like to be able set the zombie chase speed and get a bool what direction the zombie is chasing (left or right) for animations. I think this is confusing what i am trying to say is i just want to make a zombie that chase a player in both directions with ability to set chase speed and get bool what direction the zombie is chasing. The game looks like mario. I hope you understand now please help me.

I’m not sure to hunderstand all your problem but if you just need that the ennemy go in the player direction on the X axis you can do something like that :

`````` void Update () {
float moveSpeed = 5;
Vector3 posToGo = new Vector3(player.position.x , transform.position.y, transform.position.z);
transform.position = Vector3.MoveTowards(transform.position, posToGo , moveSpeed * Time.deltaTime);

}
``````

Or more hardcoded :

``````         void Update () {
float moveSpeed = 5;
Vector3 playerX = player.position.x;

if (playerX < transform.position.x)
transform.position = new Vector3(transform.position.x - moveSpeed, transform.position.y , transform.position.z);
else
transform.position = new Vector3(transform.position.x + moveSpeed, transform.position.y , transform.position.z);

}
``````

I hope this help…