In my game i am working on the enemies AI and I want in c# that whenever specific object comes within a certain distance, to move upwards if object is closer to the bottom side of the enemy and move down if the object is closer to the top of enemy.
if (GameObject.Find("Laser(Clone)") != null)
{
float projectileDistance = Vector2.Distance(this.transform.position, GameObject.Find("Laser(Clone)").transform.position);
Debug.Log(projectileDistance);
if(projectileDistance <=4.8)
{
rb.AddForce(transform.up * speed * Time.deltaTime);
}
}//this is attached to the enemy gameobject
So basically i want to know if there is a way of like splitting an object into the top half and bottom half and detect wether another object is closer to which side.