A* Pathfinding not returning shortest route

I have a tile based game. If I click a point that is unwalkable directly to the left or right of my character, the path returned is above or below this target tile and not the position to the left/right, no matter which settings I try.


Given the red circle character and clicking the green square wall, A* returns the green path to point 1 instead of point 2 where it should stop. Clicking the purple square wall, A* returns the purple path to point 3 instead of point 4 where it should go.

The system find the path to any of the closest walkable points to the target, if there are multiple, it will choose one of them arbitrarily (i.e the one it finds first).

To solve your problem, you could make the walls walkable, but add a HUGE penalty to walking on them so that characters will avoid them whenever possible, this will make sure the path ends inside the wall, but you could just make sure that they will not try to enter walls while walking.