In this tutorial we are going to add a Potentiometer Based Steering wheel to control a Vehicle in Unity3D.
This was prepared for a workshop tutorial for a Maker Camp at Independent University, Bangladesh. For details check out Center For Cognitive Skill Enhancement (CCSE) at IUB.
Remember you can download the entire Unity3D project : https://drive.google.com/file/d/0B5e8rF1kJz8eY2FlQ1RabFo0LVE/view?usp=sharing
Now a friend of mine had setup the Arduino and Potentiometer for me , but it can be done by anyone following this tutorial : https://www.instructables.com/id/Arduino-Unity-Ra... . What was missing in that tutorial was the Unity3D code, which this instructable is all about.
Step 1: Create a New Empty Unity Project
First Open Unity and create a new project. You can either add the "Standard Asset Package" in the beggining or you can do it after creating a blank project by following the steps shown in the image. For this tutorial for ease of work we mostly need the "Vehicle Package".
Step 2: Add a Terrain and Tile to Your Terrain
Create a terrain by going to GameObject > 3D object > Terrain
Make sure the terrain has a collider.
Additionally you can add a tile to your terrain so that you can see the car move, otherwise it might be hard to notice. See the second Image.
Step 3: Add the Vehicle Prefab to the Scene
Drag the Car Prefab from Assets > Standard Assets > Vehicles > prefabs > Car
on to any place of the scene slightly above the terrain. This will create a car with controls you can use on the keyboard to play the game. Hit play and see if everything is working.
Step 4: Add a C# Script to Your Car Prefab
Now the idea is your Arduino sends out a string through the USB. Unity3D is going to capture it and show it on the UI Text Mesh as seen on the image. This is what arduino.cs file does. Car Controller reads that string from the UI Text Mesh and converts it in to an integer. Then we use some simple if/else to set the angle of steering based on the potentiometer value.