Using GaussSense Desktop for Unity Applications

Tutorials - GaussToys Developers

With GaussSense Desktop, you can bring the novel interactions supported by Mini GaussSense into your 2D and 3D games for one of the most powerful game engines: Unity. Here we are going to learn how to use GaussSense Desktop for Unity and add GaussSense to your existing games.

🙋🏼 Before starting, please connect an Arduino Board with Mini GaussSense and upload the Arduino firmware to the board 👉🏼 Head First Mini GaussSense.

1. Preparing GaussSense Desktop

Please set up your GaussSense Desktop and Mini GaussSense following First Steps with GaussSense Desktop

2. Importing GaussSense Websocket Package

Please download the ⬇️ GaussSense Websocket Unity Package. (file type: unitypackage)

In the Unity Editor, click Assets → Import Package → Custom Package… to import the GaussSense-Websocket.unitypackage.

After connecting Mini GaussSense, GaussSense Desktop would start a WebSocket server (port: 5100) to send the sensor data.
Click on the scene Scenes/GaussSenseWebsocket.unity to see how to work with data sent from GaussSense.

Add GaussSense Websocket Package to Existing Projects

We are going to use a free project on Unity Asset Store called Into The Space

Check out the final result here: SpaceShooter2D GitHub Repository 🚀

1. Select Assets → Import Package → Custom Package… to import the GaussSense-Websocket.unitypackage.

2. Create an empty object called GaussSense in your scene. (Right-click → Create Empty in the Hierarchy Window.) Then drag Scripts/GaussSense.cs onto this object.

3. Open Player_Script.cs on the PlayerShip object. This script controls the spaceship with arrow keys. We are now going to add some code to Play_Script.cs for GaussSense interactions.

First, add the followings in the Start() function:

private GaussSense gs;
void Start()
{
  gs = GameObject.Find("GaussSense").GetComponent();
}

This line searches for the GaussSense object we have added to the scene and get the GaussSense.cs component.

4. We could then access the north/south point and the bipolar midpoint as GData of Mini GaussSense with this GaussSense object.
We could also get the IDs of the NFC tag when using NFC module for Mini GaussSense.

Get Methods Meaning
getNorthPoint()Get the north point object. Methods: getX(), getY(), getIntensity()
getSouthPoint()Get the south point object. Methods: getX(), getY(), getIntensity()
getBipolarMidpoint()Get the bipolar midpoint object. Methods: getX(), getY(), getIntensity(), getAngle(), getPitch()
getTagID()Get the IDs of NFC tag. Return List<int>

In the FixedUpdate() function, add the followings:

GData bipolarMidpoint = gs.getBipolarMidpoint();
if (bipolarMidpoint != null && bipolarMidpoint.getIntensityInGauss() > 0) {
  moveHorizontal = (bipolarMidpoint.getX() - 0.5f) * 2;
  moveVertical = -(bipolarMidpoint.getY() - 0.5f) * 2;

  float angle = -bipolarMidpoint.getAngle() * Mathf.Rad2Deg;
  this.transform.rotation = Quaternion.Euler(0, 0, angle);
}

We could now control the spaceship by moving and rotating a magnet on Mini GaussSense.

Congratulations! Now you could create new ways of playing for your Unity games with Mini GaussSense in 4 steps 🏅

Back to Tutorials