I have a script which has an ‘enemy’ PingPong backwards and forward, but follow the player once they get in range. Once the player is out of range I want them to return to their original position but they just appear there rather than moving back towards the original position. Any help?
var target: Transform;
var speed= 2;
var minDistance = 1;
var range;
var myHealth: EnemyScript;
var originalPosition: Vector2;
function Start() {
myHealth = GetComponent(EnemyScript);
};
function Update () {
if(myHealth.enemyHealth == 0){
Destroy(this);
};
range = Vector2.Distance(transform.position,target.position);
if (range < minDistance) {
move();
}else{
idleMove();
};
};
function move(){
transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
if (range > minDistance) {
transform.position = Vector2.MoveTowards(transform.position,originalPosition, speed * Time.deltaTime);
};
};
function idleMove(){
transform.position = Vector2(Mathf.PingPong(Time.time*1, originalPosition.x+1), transform.position.y);
};