Saving Characters from Crossy Road
How a VR game was replicated to train senseless drivers from Crossy Road.
On November 20, 2014, Mat Hall and his co-developers launched an amazingly successful arcade video game app called Crossy Road. This app earned $10 million in its first 90 days and I jumped on the bandwagon in playing it after I saw my dad kill his first virtual chicken in the game. All my cousins were playing it at that point, and everyone was making “Why did the chicken cross the road?” jokes.
I got some good laughs from the game, but the entire time I was just thinking: “These cars suck at avoiding obstacles.” I mean, they just keep senselessly killing chickens, witches, soccer players, ducks, and even unicorns! As an 8-year-old at the time, I thought it would have made more sense if I drove the cars instead. So now, to whoever has been driving those cars terribly, I replicated a game just for you…
The Ultimate Driving Practice Arena
I replicated the Player Control game from Unity into Prototype Driver:
The player of this game simply has to control the vehicle to rotate or move forward/backward to avoid as many obstacles as they can in order to reach the end of the road (*cough cough* Crossy Road drivers pay attention). Here’s to how I replicated the game:
Putting It All Together
The two main parts to putting this game together were:
- Being familiar with the Unity editor/workflow
- Programming C# scripts for player and camera functions
Setting Up the World
My first step was to download and import assets from the Unity Asset store into the Unity editor. When the assets have successfully imported, I created a new scene where the only thing present was the background terrain and a floating road.
Since everything was empty, I added a vehicle to the scene, which is the player game object, and other obstacles I positioned like crates, rocks, barrels, etc. For every object, I added a rigidbody component so the objects would follow Unity’s physics system, and I could adjust the mass of objects. After dragging, duplicating, dropping, positioning, and organizing many game objects, I positioned the main camera right behind the vehicle at a downward angle. My completed VR world looked like this:
Programming the Main Camera
Unfortunately, this camera can’t move anywhere unless I communicate to the Unity editor that this little guy can be set free to follow the player game object when it moves. I wouldn’t want the main camera to float in the same place when the vehicle will eventually be able to drive away later on. If this happens, the player won’t be able to see much after the vehicle drives away.
To program the camera so that it follows the player game object as it moves later on, I created a new C# script called FollowPlayer and attached it to the main camera in the Unity editor. I then added a public GameObject player to the top of the script so then I could drag the player game object onto the empty player variable in the editor’s inspector to associate the two objects.
I then created an offset variable under the category of Vector3 (a structure used to pass 3D positions and directions around in Unity) which contains the coordinates of the initial player game object. Under Update(), I set the transform position of the camera equal to the sum of the player transform position and offset. This makes sure that every frame updates the camera’s position to follow the player’s movement.
Sitting in the Driver’s Seat
This little car can’t do a thing right now. No matter what controls the player presses, it is just a game object that won’t budge.
In the script, I set up public float variables so I could adjust the speed of the vehicle, how fast the vehicle turns, and so users can change the horizontal/vertical input during the game. Under Update(), horizontal input involves how what the user inputs affect how the vehicle moves left and right. VerticalInput involved how the user input affects how the vehicle moves forward and backward.
For moving forward and backward, I used a translation function to move the player game object like sliding a piece of paper along the y-axis of a graph. For moving left and right, I wanted the car to turn in a smooth way, so I used the rotation function. Notice how within the functions, speed and turn speed are multiples so that I could change the speed in which the car moved forward/backward or turned left/right.
Take a good look at this poor chicken! It’s been squashed and burnt so many times with its buddies that they can’t even afford health insurance anymore. To the Crossy Road drivers out there, spend some time training in Prototype driver so you can stop injuring these innocent characters who just want to cross the road. While they practice on that, I’ll practice making more Unity games and release another one soon.