Can't change direction when enemy collides with obstacle

I’m having trouble getting my enemy game object to change direction when it collides with an obstacle. I have it set up to move randomly, and will change direction after a certain amount of time has passed, but when I try to use OnCollisionEnter to detect collisions and change its direction nothing happens. It keeps trying to move in the same direction until the time passes and it changes direction that way. I have researched and tried to implement the solutions I found, but nothing has worked. Also, I’m pretty new to coding, and I have been able to get as far as I have by cobbling together different bits of code, so the script may be a bit Frankenstein-y, that may be part of the problem.

Any help/advice is much appreciated. My script is below.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy_Controller : MonoBehaviour
{

    private float latestDirectionChangeTime;
	private readonly float directionChangeTime = 3f;
	private float characterVelocity = 2f;
	private Vector2 movementDirection;
	private Vector2 movementPerSecond;

    void Start()
    {
        latestDirectionChangeTime = 0f;
        calcuateNewMovementVector();
    }
	
void calcuateNewMovementVector()
     {
        //create a random direction vector with the magnitude of 1, later multiply it with the velocity of the enemy
        movementDirection = new Vector2(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)).normalized;
        movementPerSecond = movementDirection * characterVelocity;
     }
	
void Update()
    {
        //if the changeTime was reached, calculate a new movement vector
       if (Time.time - latestDirectionChangeTime > directionChangeTime)
        {
            latestDirectionChangeTime = Time.time;
            calcuateNewMovementVector();
        }


        //move enemy: 
        transform.position = new Vector2(transform.position.x + (movementPerSecond.x * Time.deltaTime),
        transform.position.y + (movementPerSecond.y * Time.deltaTime));

        var pos = Camera.main.WorldToViewportPoint(transform.position);
        pos.x = Mathf.Clamp01(pos.x);
        pos.y = Mathf.Clamp01(pos.y);
        transform.position = Camera.main.ViewportToWorldPoint(pos);
 


    }

    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.tag == "Obstacle")
        {
            movementDirection *= -1;
        }
    }
}

that is because eventhough you modified movementDirection, it is not updated in the update function.

The only other occurence of “movementDirection” is in the if (Time.time - latestDirectionChangeTime > directionChangeTime)

as a result it will only change when the change time is reached