Random Direction

Our boss is accompanied by two “bits” that bounce back and forth randomly along a path to each side of the boss.

Here’s how we do it. First we check to see if the bit has moved at least 1 world unit. This prevents the bit from being too erratic. If it is at least 1 world unit away, it calls the ChooseDirection method. This method essentially “flips a coin:” Heads it chooses right, tails it chooses left. Then the bit does a boundary check. If it has reached the boundary of its path, it will override the coin flip and move away from the boundary. The bit then makes a note of its new position, to make sure it will move at least 1 world unit in the chosen direction in the future. The bit begins mowing in the chosen direction, and the process starts all over again!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store