This is an old revision of the document!


Pressure based tutorial

(updated 14 october 2016)

In this tutorial you will see a simple example of how you can use the pressure value of the Pillo in your project. Take a look at this page if you want to know more about the pressure sensor and how to use it.

Keep in mind that this tutorial uses a very basic and simplefied example of how to make a game by only using the pressure function of the Pillo.


tutorial

At the beginning of this tutorial you need to do the following:

  1. Make a new Unity 2D 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 have created the following objects: Player, Ground, GameMaster, Canvas with slider and text. It is up to you what style and position these objects get in the game. (see image 1).
  • Player: The blue sprite that will float up and down with the pressure value.
  • Ground: The Player and Ground objects have both a collider and 2D rigidbody. The ground is the only object with a kinematic Rigidbody.
  • GameMaster: Empty Object which has the pressureScript attached to it.
  • Canvas: The text and slider will indicate what the pressure is on the Pillo.

image 1

Below you see the script which has to be attached to the GameMaster object to keep your project organised and so you won't get confused with other objects and/or scripts in the future.

using UnityEngine;
using System.Collections;
using Pillo; //Needed to make use of the Pillo interface.
using UnityEngine.UI; //Needed to make use of the Unity UI interface.

public class pressureScript: MonoBehaviour 
{
	//public variables
	public PilloID pilloID1;
	public GameObject player;
	public Slider pressureSlider;
	public Text pressureText;

	//private variables
	private float pressureP1;
	private float thrust;

	void Start()
	{
		thrust = 20f; // keep in mind that you can change the multiplier value to your own liking, this value is only used as an example.
	}

	void Update ()
	{
		pressureP1 = PilloController.GetPressure (pilloID1); // Retrieves the pressure value of the Pillo and saves it in variable "pressureP1".
		pressureText.text = "Pressure: " + pressureP1.ToString("0.00"); 
		/*
		 * With pressureText you can see the actual value of the pressure on the Pillo in two decimals.
		 * The Pillo pressure can be shown in 7 decimals max. 
		*/

		if(pressureP1 > 0)
		{
			player.GetComponent<Rigidbody2D>().AddForce (transform.up * (pressureP1 * thrust)); 
		}
		pressureSlider.value = pressureP1; // Puts the value of pressureP1 in the pressureSlider so you can see how hard you're pressing on the Pillo.
	}
}

Make sure you drag and drop the GameObjects in the public variables of the script in the GameMaster. if you have done everything correctly, then this should be the result: image 2