mahdi syed
Published © Apache-2.0

Steering Wheel Game Controller

In this tutorial, you'll create a video game controller that mimicks a steering wheel, to play a car game made with Unity!

IntermediateFull instructions provided4 hours89
Steering Wheel Game Controller

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
×1
Inertial Measurement Unit (IMU) (6 deg of freedom)
Inertial Measurement Unit (IMU) (6 deg of freedom)
×1

Software apps and online services

Unity
Unity
Arduino IDE
Arduino IDE

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

Steering Wheel 3D-Print

You will have to alter this file to match the dimensions and orientations of your components. DO NOT BLINDLY PRINT TIHS!!!

Code

Camera Script

C#
Attach this to the camera object in the scene.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Camera : MonoBehaviour
{
    public float smoothing;
    public float rotSmoothing;
    public Transform player;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void FixedUpdate()
    {
      transform.position = Vector3.Lerp(transform.position, player.position, smoothing);
      transform.rotation = Quaternion.Slerp(transform.rotation, player.rotation, rotSmoothing);
      transform.rotation = Quaternion.Euler(new Vector3(0, transform.rotation.eulerAngles.y, 0));  
    }
}

Car Drive

C#
Attach this to the car game object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class CarDrive : MonoBehaviour
{
    public float speed;
    public float turnSpeed;
    public float gravityMultiplier;
    public UDPRecieve manager;

    public string data;
    // public 

    private Rigidbody rb;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        data = manager.data;
        Accelerate();
        Turn();
        Fall();
    }

    void Accelerate ()
    {
        // if (Input.GetKey(KeyCode.W))
        // {
            Vector3 forceToAdd = transform.forward;
            forceToAdd.y = 0;
            rb.AddForce(forceToAdd * speed * 10);
        // }
       if (Input.GetKey(KeyCode.S))
        {
            // Vector3 
            forceToAdd = -transform.forward;
            forceToAdd.y = 0;
            rb.AddForce(forceToAdd * speed * 25); //* 10
        }

        Vector3 locVel = transform.InverseTransformDirection(rb.velocity);
        locVel = new Vector3(0, locVel.y, locVel.z);
        rb.velocity = new Vector3(transform.TransformDirection(locVel).x, rb.velocity.y, transform.TransformDirection(locVel).z);
    }

    void Turn ()
    {
        if (data == "left")
        {
            rb.AddTorque(-Vector3.up * turnSpeed * 10);
        }
        else if (data == "right")
        {
            rb.AddTorque(Vector3.up * turnSpeed * 10);
        }
    }

    void Fall()
    {
        rb.AddForce(Vector3.down * gravityMultiplier * 10);
    }

    void OnCollisionEnter(Collision col)
	{
	if(col.gameObject.tag == "Finish")
	{
	SceneManager.LoadScene("SampleScene");
	}
	}
}

UDP Receive

C#
Attach this to the car game object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

public class UDPRecieve : MonoBehaviour
{

    Thread recieveThread;
    UdpClient client;
    public int port = 5052;
    public bool startRecieving = true;
    public bool printToConsole = true;
    public string data;
    // Start is called before the first frame update
    void Start()
    {
        recieveThread = new Thread(new ThreadStart(ReceiveData));
        recieveThread.IsBackground = true;
        recieveThread.Start();
    }

    // Update is called once per frame
    private void ReceiveData()
    {
        client = new UdpClient(port);
        while(startRecieving)
        {
            try
            {
                IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
                byte[] dataByte = client.Receive(ref anyIP);
                data = Encoding.UTF8.GetString(dataByte);

                if(printToConsole){print(data);}
            }
            catch(Exception err)
            {
                print(err.ToString());
            }
        }
    }
}

Credits

mahdi syed
4 projects • 2 followers
I'm a passionate programmer with extensive experience in robotics, app design, web design, IOT, and AI.

Comments