Prerequisites
This project assumes you are familiar with the basic functionality of Unity. You should also review the following before moving on:
Setup
Make sure you have the following parts before getting started.
Configure Unity
In order to use Phidgets with Unity, you have to configure your project. Follow the instructions below.
Step 1
Download a copy of the Phidget libraries and unzip it as shown.
Step 2
Next, open Unity and create a new 3D project. Name it Phidgets_Unity and place it in the location of your choice.
Step 3
Navigate to the file you previously unzipped and import Phidget22.NET.dll and phidget22.dll as shown.
Note: If you are developing on macOS, you do not need to import phidget22.dll
Create Environment
Now that your project is configured, you can create your environment.
Write Code (C#)
Copy the code below into your BallScript file.
NOTE: the code below assumes you are using the 5kg load cell. If you are not, you must modify the calculateForceInNewtons function based on the table found here.
using UnityEngine;
using Phidget22;
public class BallScript : MonoBehaviour
{
//Define
static VoltageRatioInput scale;
float maxForce = 0;
float currentForce = 0;
static float offset = 0;
public Rigidbody rb;
GUIStyle fontStyle;
//This function calculates the scale's offset | The offset is used to zero the scale and ensure the measurments are accurate
static void calculateOffset()
{
float average = 0;
for (int i = 0; i < 32; i++)
{
average += (float)scale.VoltageRatio;
//Wait for a DataInterval to elapse before averaging again. If you don't wait, you will re-use the same data point multiple times.
new WaitForSeconds((float)(scale.DataInterval / 1000.0f));
}
offset = (average /= 32.0f);
}
//This function calcluates the force on the scale in Newtons | The base units of force in Unity are Newtons.
static float calculateForceInNewtons()
{
return 4700.0f * 9.81f * (float)(scale.VoltageRatio - offset);
}
//Display the current force to the screen
void OnGUI()
{
GUI.Label(new Rect(10, 10, 100, 20), "Current:" + currentForce.ToString("F2") + " N", fontStyle);
}
// Start is called before the first frame update
void Start()
{
//Used to style font on the GUI
fontStyle = new GUIStyle();
fontStyle.fontSize = 50;
//Create
scale = new VoltageRatioInput();
//Open
scale.Open(1000);
//Set data interval to minimum | This will increase the data rate from the sensor and allow us to calcluate the offset quickly
scale.DataInterval = scale.MinDataInterval;
//Automatically calculate offset
calculateOffset();
}
// Update is called once per frame
void Update()
{
//Update may be called before the scale is fully connected, so this prevents any unwanted errors
if (scale.Attached)
{
//The code below will monitor a users input and launch the ball at the peak force
currentForce = calculateForceInNewtons();
if (currentForce > maxForce)
{
maxForce = currentForce;
}
else if (Mathf.Abs(currentForce - maxForce) > 1)
{
rb = GetComponent<Rigidbody>();
rb.AddForce(0, maxForce, 0, ForceMode.Impulse);
maxForce = 0;
}
}
}
//Required for Unity and Phidgets
private void OnApplicationQuit()
{
scale.Close();
scale.Dispose();
}
}
Run Your Program
Press on the scale, you will see the force measurement on screen increase. When you release, the ball will shoot into the air.
Code Review
In the code above, there are three main elements:
- Calculating Offset. This is required so your scale can be tarred or zeroed. This is discussed in the Phidget Scale Kit project.
- Calculating force. The formula to calculate weight in kg is discussed in the Phidget Scale Kit project. You can convert from kg to Newtons by multipling by ~9.81 meters per second squared. The default units for force in Unity are Newtons.
- Determining peak force. A simple algorithm is used to calculate the peak force from the scale. When the force decreases by more than 1 Newton, the ball is launched.
Practice
- Try to create a game that uses the mechanics shown above. For example, you could create targets and have players shoot the ball at them with the right amount of force. You will need to add a force in the x/z axes to move the ball in different directions.