You can just stop movement before closing the gate.
Also, you can use a distance node rather than subtracting and getting the length.