Shooter game has been popular since 1980s, it's relatively fun and entertaining for the most part. With 3D magnetic sensors we've provided a brand new way of playing Space Shooter. This guide specifically discusses how to build Space Shooter with Unity and Infineon's 3D magnetic 2GO kit & Rotator Knob based on their TLE493D as controller.
Step 1: Set up the Infineon 3D magnetic sensors softwareThis part is a little tricky, if you've installed Evalkit for 3D Sensors you are likely to get stuck in an infinite loop.
So under this guide we have to install it via Arduino from Infineon's GitHub page.
https://github.com/Infineon/TLE493D-W2B6-3DMagnetic-Sensor
So we can add the TLE493D Library to Arduino
And once Arduino IDE is installed, we also have to add XMC as aprt of the device per Infineon's GitHub page.
https://github.com/Infineon/XMC-for-Arduino
In short, we just need to add following to the Preference so that XMC Microcontroller shows up. And add XMC Microcontroller so the board.
https://github.com/Infineon/Assets/releases/download/current/package_infineon_index.json
After that we can start testing the magnet sensors from following code. Somehow there is a bug so you will need to do Tle493dMagnetic3DSensor.begin();
twice in order to get it running.
#include <Tle493d_w2b6.h>
Tle493d_w2b6 Tle493dMagnetic3DSensor = Tle493d_w2b6();
void setup() {
Serial.begin(9600);
while (!Serial);
Tle493dMagnetic3DSensor.begin();
Tle493dMagnetic3DSensor.begin();
Tle493dMagnetic3DSensor.enableTemp();
}
void loop() {
Tle493dMagnetic3DSensor.updateData();
Serial.print(Tle493dMagnetic3DSensor.getX());
Serial.print(" ; ");
Serial.print(Tle493dMagnetic3DSensor.getY());
Serial.print(" ; ");
Serial.println(Tle493dMagnetic3DSensor.getZ());
delay(10);
}
The default JLink uses v6.00e from the installer, if you get the following error
[Error] Infineon.DebuggerExceptions: It seems that JLink software is not installed please download from www.segger.com and install it. You can specify it by setting java property xmcFlasher.JLink.dllPath
Make sure you go to www.segger.com
and install the latest JLink. the version v6.32d and Arduino 1.8.5 would compile and upload Arduino code quiet successfully.
This step is fairly straight forward, making sure the chips are facing up.
After the knob is installed, we can test out on the same Arduino output to see both rotation as well as click.
Now we can determine whether we are rotating left or right, as well as clicking. Left and Right can be seen through rotation, and click is always double of what current value is fro the Knob. Because of the latency, it's much easier for Unity to do the logic for the controller, but in the meanwhile, we can make the LED work while pressing. Similar logic will be written in Unity, but we can have some LED fun with the board to light up the LED when firing.
//It doubles when press down, and halves when releases
if((abs(Tle493dMagnetic3DSensor.getNorm()) > abs(norm*1.5))){
fire = true;
}
else if
((abs(Tle493dMagnetic3DSensor.getNorm()) < abs(norm/1.5)))
{
fire = false;
}
if(fire)
{
digitalWrite(14, HIGH);
}
else
{
digitalWrite(14, LOW);
}
When succeed, it will come up with something like this.
Next, we have to get Unity Space Shooter, you can either do it through step-by-step guide or simply clone it via GitHub link.
git clone https://github.com/nyceane/unity-tutorial-space-shooter
For those who are interested in building space shooter step by step you can easily do this from Unity's website. https://unity3d.com/learn/tutorials/s/space-shooter-tutorial As the guide is widely available for anyone who are interested in building out gaming.
Step 5: Connect USB controls to UnityWe first need to find USB port, which can easily be done through Device Manager. This is to ensure our controller gets the Magnetic 3D sensor
Inside Unity, we need to go to Edit->Project Settings->Player, then go to Other Settings to make sure Api Compatibility is.NET 2.0 and not.NET 2.0 Sublet, this is to ensure the Serial port working.
Next, inside Unity's PlayerController.cs, we will start to implemented the code we've previously wrote in Step 3. Following code should get the fire trigger running, as we are reading all the sensor values in Unity, we can do all the logic inside Unity as well. In this case, whenever the knob is pressed, similarly, we can detect the rotation of the knob.
Modify the PlayerController.cs to following code, we'd able to move horizontally as well as pressing to fire.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
public class PlayerController : MonoBehaviour
{
public float fireRate = 0.5F;
public float speed;
public float tilt;
public Boundary boundary;
public GameObject shot;
public Transform shotSpawn;
private float myTime = 0.0F;
private float nextFire = 0.5F;
private Rigidbody rb;
private AudioSource audioSource;
SerialPort serial;
private float norm = 0;
private float x, y, z = 0;
private float x2, y2, z2 = 0;
private float azimuth = 0;
bool fireTrigger = false;
float knob = 0.0f;
void Start()
{
rb = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
serial = new SerialPort("COM3", 9600);
serial.Open ();
serial.DiscardInBuffer ();
// Debug.Log (serial.ReadLine ());
string[] temp = serial.ReadLine ().Split (';');
if (temp.Length == 5) {
x = float.Parse(temp [0].Trim ());
y = float.Parse(temp [1].Trim ());
z = float.Parse(temp [2].Trim ());
norm = float.Parse (temp [3].Trim ());
azimuth = float.Parse (temp [4].Trim ());
}
}
void Update()
{
if (serial.IsOpen) {
string[] temp = serial.ReadLine ().Split (';');
if (temp.Length == 5) {
x2 = float.Parse (temp [0].Trim ());
y2 = float.Parse (temp [1].Trim ());
z2 = float.Parse (temp [2].Trim ());
float azimuth2 = float.Parse (temp [4].Trim ());
float norm2 = float.Parse (temp [3].Trim ());
if ((Mathf.Abs (norm2) > Mathf.Abs (norm) * 1.5)) {
fireTrigger = true;
} else if ((Mathf.Abs (norm2) < Mathf.Abs (norm) / 1.5)) {
fireTrigger = false;
}
if ((int)(azimuth2 * 10) > (int)(azimuth * 10)) {
knob = -0.7f;
} else if ((int)(azimuth2 * 10) < (int)(azimuth * 10)) {
knob = 0.7f;
} else {
knob = 0.0f;
}
//reset value
x = x2;
y = y2;
z = z2;
norm = norm2;
azimuth = azimuth2;
}
}
myTime = myTime + Time.deltaTime;
if ((Input.GetButton("Fire1") || fireTrigger) && myTime > nextFire) {
nextFire = myTime + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
audioSource.Play();
nextFire = nextFire - myTime;
myTime = 0.0F;
}
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
if (moveHorizontal == 0.0f && knob != 0.0f) {
moveHorizontal = knob;
}
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.velocity = movement * speed;
rb.position = new Vector3(
Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp(rb.position.z, boundary.zMin, boundary.zMax)
);
rb.rotation = Quaternion.Euler(
0.0f,
0.0f,
rb.velocity.x * -tilt
);
}
void OnApplicationQuit()
{
if (serial.IsOpen) {
serial.Close ();
}
}
}
And whoohoo! You can now play Space Shooter using Infineon's 3D magnetic 2GO kit & Rotator Knob as controller :)
Comments