Unity2D AI for Top Down Zelda-Type movement

I’ve been trying to wrap my head around enemy movement for a couple weeks now, and I’m not quite sure I understand very much at all.

I was using some code from a tutorial I found at some point.
Here it is

    void Update()
    	transform.Rotate(new Vector3(0,-90,0),Space.Self);	
    	if (Vector3.Distance(transform.position,target.position)>1f)
    		transform.Translate(new Vector3(speed * Time.deltaTime,0,0) );

This causes the enemy to chase the player down, but causes the sprite to rotate on all axis.

What I want, and can’t seem to figure out, is for enemies to only have left-looking and right-looking states, and still move towards the player in all directions.

So there would be no actual rotation, just a flipping of the sprite direction.

If you’re wanting left/right only. You won’t want a transform.LookAt. That will use all axes. Try a simple if check, to see if the player is to the left or right of the enemy. Do this on the “Enemy Movement/AI” script. Basically, if(target.position.x < transform.position.x) mySprite = facing left; else my Sprite = facing right.

On another note, you might want to make a generic velocity by having your “monster” track his own position. Example…
//Enemy Movement Script

Vector3 currentPosition, lastPosition;

currentPosition = transform.position;
lastPosition = currentPosition;

     if(currentPosition.x > lastPosition.x){
      //We are moving to the right
     //set sprite to rightFacingSprite
     if(currentPosition.x < lastPosition.x){
      //We are moving to the left
     //set sprite to leftFacingSprite

     //Update the new positions
     lastPosition = currentPosition;
     currentPosition = transform.position;

This way, even if your monster hasn’t “sensed/angroed” the “hero”, he will still look back and forth as he moves along his default path. (Assuming, he has a default path.)

Hope this helps.
NOTE* That code isn’t meant to just work out of the box, but rather give you an idea of what needs done.

-Dwayne Pritchett