nested if functions keep resetting the state

hi there... i'm fairly new to coding and i have a project making an autonomous vehicle go around a course where by it has to go around a course with various obstacles. i have a load of functions written out that all work and work together just in a loop, but trouble starts where when i attempt to make 'States' (not sure if this is the right way to describe it), the states all reset themselves back to zero if there are more than 2 states. i am using if functions with the code necessary to pass each stage of the course with a measurable 'completion' criteria.
I know my code is mess and that's because I've just brought it together and like i said I'm rather new to this. Any help or direction would be appreciated. the code and a picture of the course is attached. The distance Values of 0 are because 0 is the maximum distance

bringing_it_together.ino (6.51 KB)

Please read the locked topics at the top of this forum, and anything that they link to, etc., especially the parts about using autoformat (CTRL-T in the Arduino IDE on a PC) and using code tags. The way that you have posted, I am unable to view your code on my mobile device. Not a nice approach for obtaining free assistance.

I don't see anywhere in loop() that sets the 'state' variable back to 0. It also doesn't do anything once the state gets to 3 (or more).