Well since you are doing your “move around” with just translating it up ( assuming you are simulating an enemy jump ) I could offer a solution to detect the wall. Use raycasting.
What can you do is casting a ray in the mid point or whereever you want of enemy, and then if that ray detects a collision, and if that collision is tagged “Wall” you can order enemy to jump.
public bool canJump;
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.right);
if(hit.collider.tag == "Wall" && canJump)
{
canJump = false;
transform.Translate(Vector3.up * 1f * Time.deltaTime);
}
now over there, i have a boolean variable called canJump, this is due to fact that raycasting will be done in update or fixedupdate, which means that if you do not disable it after one frame of detection, it will run the code more than one time. Because it will still be casting it forward, which may continue hitting the wall until the enemy gets over the wall, your enemy can translate up more that once, that can lead it to fly to the sky. Now last part is, we need to enable it again, which can be done with different ways :
Use a coroutine.
If you have a nearly constant amount of time between enemy jumping and falling back again, once it has jumped, you can call a corouitine, wait for some time, and then activate canJump again to be able to start detecting collisions again.
StartCoroutine(WaitForSomeTime());
IEnumerator WaitForSomeTime(){
yield return new WaitForSeconds(0.5f);
canJump = true;
}
Use a backwards ray, same as above, just use -Vector2.right, and if that hits a wall, meaning that enemy has passed over the wall and can detect a new wall, just make canJump true again.
Use logic, you can simply make canJump true if enemy is grounded, false if enemy is not grounded. It all depends on your game logic.
Or as i said, any other logic that can be use in order to make canJump true / or false, depends on your gameplay. Good luck !