Simple Unity Controller
Description
This project is a very simple controller that can give input to Unity when pressing buttons I designed it to control a game I made where the player can go left and right. Of course you can always adjust it for more controls. There is no need to use any extra libraries.
Materials
Components needed
- -Arduino 2x resistor
- -[~220 Ohm
- -Wires
- -2x Buttons
- -Breadboard
Breadboarding
I started by connecting the arduino GND port to a negative(-) pin on the left side of the breadboard, and connecting the 5V to a positive(+) on the right side of the breadboard.
After that I put the buttons in the place I wanted the mto be. And connected them using with the wires and resistors.
Coding the Arduino
First of all you will need to write some code for you Arduino to run. I put comments in the code that explain what it does.
//Declare pins that buttons are connected to. const int buttonPin1 = 3; const int buttonPin2 = 4;
void setup() { //Start the serial Serial.begin(9600); //Configure the pins as output. pinMode(buttonPin1, INPUT); pinMode(buttonPin2, INPUT); }
void loop() { //Read the state of the button if(digitalRead(buttonPin1) == HIGH) { //print this line if state is HIGH. Serial.println("Left"); delay(20); } if(digitalRead(buttonPin2) == HIGH) { //print this line if state is HIGH. Serial.println("Right"); delay(20); } }
Unity
If you don't have a game ready, this code will work on any gameobject in unity.
if this is the case you will need a gameobject to move.
For the sake of simplicity we will create a cube by going to GameObject->3D Object->Cube
once the cube is in your scene, select it and press the Add Component button, and create new script.
Also you will need to change the Api compatibility level in order for the System.IO.Ports library to work.
To do this go to Edit->Project Settings->Player
scroll down in this menu until you find the Api Compatibility Level and select .NET 2.0 instead of .Net 2.0 subset
You are now ready to start coding.
using System.Collections;<br>using System.Collections.Generic; using UnityEngine; using System.IO.Ports;
public class PlayerMovement : MonoBehaviour { public float speed; float movement;
SerialPort sp = new SerialPort("COM3", 9600);
void Start() { //Run the code to open the serial port OpenSerialPort(); }
void OpenSerialPort() { //Open the serial port sp.Open(); sp.ReadTimeout = 1; }
void Movement(string Direction) { //Check what direction the arduino has passed on if (Direction == "Left") { movement = -1; } if (Direction == "Right") { movement = 1; } //calculate the amount by which the gameobject will be moved float translation = movement * speed; //Apply the movement to the gameobject transform.Translate(translation, 0, 0); }
void Update() { if (sp.IsOpen) { try { //while the serialport is open move execute the movement function and pass the line that the Arduino is printing Movement(sp.ReadLine()); } catch (System.Exception) {
}
} } }
Final Notes
Check if your components are working
A problem that I encountered during the creation of this was that all the wires and code were right and there should be no problem at all, but it wasn’t working.In my case it was a wire that wasn’t working, although the same can happen with some of your other components.