Enemy MoveTowards getting stuck on obstacles

I am making a 2D Top-down game and I am having trouble getting the enemy AI working properly. The enemy is following me properly, but it is getting stuck on objects in the way. How can I get the enemy to detect once it collides with an obstacle and interrupt the MoveTowards to change direction to hopefully avoid the obstacle?

The code that I am using for the enemy is as follows:

void FixedUpdate() { transform.position = Vector3.MoveTowards(transform.position,target.position,speed*Time.fixedDeltaTime); }

How can I detect if the enemy is colliding with an object and interrupt the MoveTowards function? Or is there a better way to do this?

Thanks.

What you are ultimately looking for is called pathfinding, i don’t know much about it, but there are plugins that can help.

Also answering your question : to interrupt moveforward, you could create a boolean condition “collision”.
Use raycast or any detection method (overlap circles etc), if the condition is false, move forward is working, if collision is true, then you turn it off… But you still need a behavior to avoid the obstacle and here pathfinding is really your best bet to achieve something acceptable.