How do I move an object directly to a point with obstacles in the way

I’m trying to move my player to a point clicked on (getting the point clicked and its world position are good, I don’t need help with that) and I have it working that it will send the character to the position and stop when it gets there

function MoveCube(cubeDestination : Vector3, continueMove : boolean) {   
	moving = continueMove;
	var maxMoveTime = 10.00;
	var moveTime = 0.00;
	
	while ((moveTime < maxMoveTime) && moving) {
		var cubeHeading = cubeDestination - this.transform.position;
		var cubeDistance = cubeHeading.magnitude;
		var cubeDirection = cubeDestination / cubeDistance;
		this.transform.Translate(Vector2(cubeDirection.x/speed, cubeDirection.y/speed));
		moveTime += 0.01;
		
		if (cubeDistance <= 1) {
			moving = false;
		}
		
		yield WaitForSeconds(0.01);
	}
	
	moving = false;
}

This is my function that moves the character, and it works well, but my only issue is that when the character hits a wall, it’s supposed to slide along it (which it does just fine) but when it gets to the end of the wall and continues moving, it goes in the same previous direction but not towards the same point. Can anyone help me make it continuously head for the same point, even if it ends up being moved in another direction temporarily by another force?

Try changing

var cubeDirection = cubeDestination / cubeDistance;

to

var cubeDirection = cubeHeading / cubeDistance;

In general though, NavMeshes and NavMeshAgents seem like the do exactly what you want and would greatly simplify your code once you get the hang of them.

Unity manual - NavMesh

I figured it out, the issue was that because my player had a rigidbody, it was having added force from sliding sideways and so I just increased drag to 9999 and it slid and then went straight to the target as desired. And I fixed that so that it was

var cubeDirection = cubeHeading / cubeDistance