How to check if distance between two objects is getting smaller?

Hello,

i want to know how do i check if distance between two objects is getting smaller (in c# if possible).

private GameObject enemy;

enemy=GameObject.FindWithTag(“Enemy”);

float distance=(transform.position-enemy.transform.position).magnitude;

if (distance is getting smaller){
//… some stuff here

}

You must compare the current distance with the previous one: if it’s smaller, the distance has shortened. Anyway, save the current distance to compare in the next Update:

private GameObject enemy;

void Start(){
  enemy=GameObject.FindWithTag("Enemy");
}

private float lastDist = 0;

void Update(){
  float distance=(transform.position-enemy.transform.position).magnitude;
  if (distance < lastDist){ // if distance reduced...
      //............ do some stuff here
  }
  lastDist = distance; // don't forget to update last distance!
}