How to Unity
Random Walk Script in Unity
Make your objects walk like drunks!
In this simple tutorial, I’m going to show how I easily implemented a Random Walk feature in Unity, via C# scripting.
Before diving into the tutorial and the instructions, let’s revise what is a Random Walk.
Random Walk
A Random Walk is a probability process describing a path made of random steps, each one taken starting from the last position. A random walk is often used to describe physical systems and may be modelled in 1, 2, or 3 dimensions. Assuming that the step is always of a given length:
- in 1D, the object can randomly walk “left or right”, each time by one step
- in 2D, the object can make a step in any direction on a plane and it will end up at any point a circle centred in its initial position and having a radius equal to step length
- in 3D, it’s similar to the 2D case but this time the object may end up at any point of a sphere surface around its starting position.
The time, or space, if given a constant speed, between two steps is often called the mean free path. To complex things a little more we can also randomly generate this parameter.
Unity Implementation
In a Unity project, let’s consider a 3D object, moving on a plane (XZ). We need a random number generator to provide the direction of motion…