need rotate when change position relative to char

hi there, i am making a 2d game and i have a basic ai, the enemy follow me and atack me when in short range, i need my enemy rotate when he es in x local negative relative to char, i try this code. if (transform.InverseTransformPoint(engineTransform.position).x > 0.0) { // right } else { // left }

but dosnt work very well when the enemy its to close to player.

this is my full code for ai. var target : Transform; var movespeed :int; var rotationSpeed :int; var dist : float; var spoted :int = 5; var atackRange :float = 1.5;

function Start (){ target = GameObject.FindWithTag("Player").transform;

}

function Update () { //dibuja una linea entre el objetivo y el enemigo, tambien se puede poner target.position en vez de target.transform.position, ya que el target es de tipo transform Debug.DrawLine(target.transform.position, transform.position, Color.blue); dist = Vector3.Distance( target.transform.position, transform.position);//target.position.magnitude - transform.position.magnitude ;

if (transform.InverseTransformPoint(target.position).x < 0) {
    // left
    Debug.Log (" estoy a la izquierda"); // i am in the left position

}

if (dist < spoted && dist > atackRange ){
    // mueve al target
    //movimiento local , global seria Vector3.forward
    animation.Play ("run");
    Debug.Log ("estoy a la derecha"); // i am in right position
    //var movimiento = transform.Translate(transform.forward * movespeed * Time.deltaTime, Space.World);
    var movimiento = target.transform.position - transform.position;
    movimiento.Normalize();
    var mover = movimiento * movespeed;

    transform.position = transform.position + ( mover * Time.deltaTime);

}
else if (dist < atackRange){

    animation.Play ("attack");
}
else {
    animation.Play ("idle");

}

} thanks for help.

if(target.position.x > transform.position.x) Debug.Log ("i am in left"); else if(target.position.x < transform.position.x) Debug.Log ("i am in right"); else Debug.Log ("same place, i am dead.");

this is the code, someone help me in other forum.