Could use some math and pathfinding help

i have a moving part going toward a target. i also have a cube that i do not want the moving part to go through.

one way i’ve done this (and it does not work well) is check raycast results in each direction that is straight on the axis sand if there is a result then we make the direction axis 0. so if it detects result on x we make x in the direction 0 and so on. this works a bit but it’s jank and the moving part will often not go toward the target but go in a straight line.

originally i thought about getting a 90 degree point given my moving parts position and raycast results position. the trouble i’ve been having with this is i can’t get it to be completely paralel with the block part i don’t want it to go through so the moving part becomes jittery and doesn’t move smoothly.

does anyone have any suggestions?