AndyAtari’s Driving Simulator: A Unity Prototype

Andrew Alteri
4 min readMay 30, 2021

Hello Unity neighbor! Unit 1 of Unity’s Create with Code Live 2021 asks participants to create a short driving game with simple physics and player control. The initial goal is to move a car down the road with player input, avoiding obstacles along the way. After you achieve that goal, the unit offers four bonus features for participants to attempt. The bonus features are challenges that test your skills and knowledge of Unity and C#. I will talk about how I solved the second bonus feature in this post. If you are following along, I strongly suggest that you start with intro tutorials in the Create with Code course before following me to the bonus features.

Bonus Feature 2 challenges developers to add oncoming vehicles for the player to dodge. Instead of creating a fast-paced racing game, I decided to create a relaxing driving simulator. Oncoming traffic wouldn’t exactly be relaxing, so I added planes flying overhead instead. After adding the plane asset to my scene, I created a new C# script named OncomingPlaneController and dragged it into the plane object. Next came the real challenge: having the plane fly with code. Let me show you my final solution and then explain my process.

Let’s dive into this Update() method. This method is performing two key actions: it moves the plane forward and increases the plane’s elevation at a consistent speed to give the effect that the plane is lifting off. I call the transform.Translate method to move the plane object on the x, y, and z axes. So, in this case I use Vector3.forward and Vector3.up to move the plane on the z axis and the y axis. Vector3.forward is shorthand for Vector3(0, 0, 1) and Vector3.up is shorthand for Vector3(0, 1, 0). To maintain full control over the plane’s speed, I multiply Vector3 by a float number every second. To ensure a consistent flight speed, I use Unity’s Time method and call the deltaTime interval so the plane moves forward and a flies a little bit higher every second.

Now, I understand that most people reading this article are probably familiar with variables, but if you are new to development, let me show you why I added two variables at the top of the script. Declaring a private float variable and initializing it right away will assign a value to the variable that I can use anywhere in my script. “private” means I cannot access this variable globally, but only within this class, and “float” means the value will be an integer with a floating decimal point. If I have a speed of 10.0, I can declare it at the top and use it as many times as I would like, changing the numerical value at any time. The syntax is also easy to read, so other developers can quickly find the speed value. If I simply typed a 10.0 in the translate method itself, I would be hard coding the value which would cause frustrations later if I wanted to alter my speed. It is best practice to avoid hard coding to maintain flexibility.

Lastly, I made the plane’s propeller spin. To do this, I created another C# script and added it as a component to my propeller object.

As you can see in the code above, I wrote a script similar to the OncomingPlaneController, but instead of using Translate I used Rotate. The Unity Rotate method will rotate the object around the z axis creating a spinning motion. If I used Translate, the propeller would fly right off the plane and possibly crash into the vehicle!

Now I have a nice plane flying overhead, creating a soothing atmosphere. Hopefully this post will help you along if you were stuck on this challenge. On the other hand, please let me know if you came up with a better solution to this bonus feature. I welcome all forms of collaboration and am eager to improve my knowledge in Unity and C#. Come join me next time as I tackle basic gameplay in Unity. Have a great week, and keep creating!

--

--