Moving an enemy towards the player and taking him back to his original position is not working

Hi,

I am trying to find if player is with in the range of attack and if he is within the range of attack then am moving the enemy towards the player. This part of the program is working fine. But when player is moved to, out of range again, then I want the enemy to move back to his original position. This is not happening. enemy is not moving back to his original position but standing still once the player is out of range. Below is my enemy script.

====================================================

var delay: float = 1;

var range = 10;

var target: Transform;

private var originalPosition: Vector3;

function Awake()
{

target = GameObject.FindWithTag("Player").transform;
Debug.Log("Inside Awake");
originalPosition = transform.position;
}

function Update () {

if(CanAttackTarget() == true)
{
    Debug.Log("Inside Update, inside if loop of CanAttackTarget");
    RotateTowardsTarget();
    MoveTowardsTarget();
    positionChanged = 1;
}
else
{
    Debug.Log("Out of Range");
    if(positionChanged == 1)
    {
        transform.LookAt(originalPosition);
        transform.position = Vector3.Slerp(transform.position, originalPosition, Time.deltaTime * delay);
        positionChanged = 0;
    }
}
}

function MoveTowardsTarget(){

transform.Translate(Vector3.forward * Time.deltaTime);
}

function RotateTowardsTarget(){

//Same as transform.LookAt()
var targetDir = target.position - transform.position; 
var finalRotation = Quaternion.LookRotation(targetDir);
transform.rotation = finalRotation;
}

function CanAttackTarget(){
Debug.Log("Inside CanAttackTarget");
//Check if the distance is out of range
if(Vector3.Distance(transform.position, target.position) > range)
{
    print("out of range");
    Debug.Log("Inside CanAttackTarget, inside if loop of not within the range");
    return false;
}

var hit : RaycastHit;

//Check if there's is some objects inbetween the target and current object
if(Physics.Linecast(transform.position, target.position, hit))
{
    Debug.Log("Inside CanAttackTarget, inside if loop of Linecast");
    if(hit.collider.gameObject.tag != "Player")
    {
        Debug.Log("Inside CanAttackTarget, inside if loop of Linecast, inside if loop of something is in between");
        print("out of range");
        return false;
    }
    else
    {
        Debug.Log("Inside CanAttackTarget, inside if loop of `Linecast, inside else loop of something is in between");`
        print("In the range");
        return true;
    }
}
Debug.Log("NEW HARI");
return true;
}

===============================================

In Update function I have an if-else statement to check if the player is in the range or not. "If" part of the code is working fine. But "else" part, when the player is not in the range and position of the enemy is changed. I expect the enemy to move back to his original position (see the code in the if(positionChanged == 1)), which is not happening.

Can someone please help me here?

Thanks in advance, Regards, Hari Vinodh

The problem is the positionChanged variable. Your character will move towards your original position for exactly one frame. Then positionChanged is set to 0 and it stops moving.