Gyroscope based tutorial

(Updated 24 november 2016)

In this tutorial I will show you an example of how to use the gyroscope in your unity game by controlling all three axes with the Pillo. To learn more about the gyroscope feature click here.

Click here if you want to download this unity project.


Tutorial

For this tutorial you need to do the following:

  1. Make a new Unity 3D project and import the PDK package. If you don't have the PDK yet you can download it here .
  2. Add the PilloController prefab to your project's hierarchy (The prefab can be found in Pillo > _Core > Prefabs). This GameObject has a script attached to it and without this script you cannot use any of the Pillo functions!
  3. For this tutorial I am using two Cube objects named “blueDice and blackDice” with a dice texture on them. It is up to you what style and position these objects get in the game. (see image 1).
  • both dices: cube game objects that contain the script with the gyroscope feature.

image 1

Below you see the script that reads the gyroscope value from the Pillo receiver and use it to rotate the dice in the game. For now we use one script attached to both dices instead of making a GameMaster object to have a seperate overview of their gyro values.

using UnityEngine;
using System.Collections;
using Pillo;

public class diceScript : MonoBehaviour 
{
	public PilloID pilloID;

	//Saving the gyro value from the receiver.
	public float GyroX = 0.0f;
	public float GyroY = 0.0f;
	public float GyroZ = 0.0f;

	//Gyro multiplier to multiply the gyro rotation speed;
	public float GyroXMultiplied;
	public float GyroYMultiplied;
	public float GyroZMultiplied;

	public float GyroMultiplication = 1f;

	void Update () 
	{
		multiplyGyro ();

		// get raw gyro data
		GyroX = PilloController.GetGyroX(pilloID);
		GyroY = PilloController.GetGyroY(pilloID);
		GyroZ = PilloController.GetGyroZ(pilloID);

		transform.Rotate(GyroXMultiplied, GyroYMultiplied, GyroZMultiplied);

		if(Input.GetKey(KeyCode.Space)) //Press spacebar to reset the positions of the dices.
		{
			transform.rotation = new Quaternion (0, 0, 0, 0);
		}
	}

	private void multiplyGyro()
	{
		if(GyroX > -0.02f && GyroX < 0.02f) 
		{
			//If the gyro value is between these two values, then keep it still by multiplying it with 0.
			GyroXMultiplied = 0f;
		}
		else
		{
			GyroXMultiplied = GyroX * GyroMultiplication;
		}

		if(GyroY > -0.02f && GyroY  < 0.02f)
		{
			//If the gyro value is between these two values, then keep it still by multiplying it with 0.
			GyroYMultiplied = 0f;
		}
		else
		{
			GyroYMultiplied = GyroY * GyroMultiplication;
		}

		if(GyroZ > -0.02f && GyroZ < 0.02f)
		{
			//If the gyro value is between these two values, then keep it still by multiplying it with 0.
			GyroZMultiplied = 0f;
		}
		else
		{
			GyroZMultiplied = GyroZ * GyroMultiplication;
		}
	}
}

NOTE: The pillo GyroMultiplication is important for gyroscope based games with the Pillo! The reason for this is because if you don't multiply the gyro value, then it would automatically reset to 0 if you don't move your Pillo.

If you play the game then this should be the result: image 2


Pillo shake

to be added.