Skip to content

Instantly share code, notes, and snippets.

@holmesal
Created April 24, 2016 01:26
Show Gist options
  • Save holmesal/d19f63b86d42f37d9721328a474afc4c to your computer and use it in GitHub Desktop.
Save holmesal/d19f63b86d42f37d9721328a474afc4c to your computer and use it in GitHub Desktop.
Controlling a First Person Controller in Unity with a Balance Board
using UnityEngine;
using System.Collections;
using UnityStandardAssets.Characters.FirstPerson;
public class OSCReceiver : MonoBehaviour {
public FirstPersonController fpc;
// public string RemoteIP = "127.0.0.1";
public string RemoteIP = "192.168.29.102";
public int SendToPort = 9000;
public int ListenerPort = 8000;
private Osc handler;
// Use this for initialization
void Start () {
UDPPacketIO udp = GetComponent<UDPPacketIO>();
udp.init(RemoteIP, SendToPort, ListenerPort);
handler = GetComponent<Osc>();
handler.init(udp);
handler.SetAllMessageHandler(AllMessageHandler);
}
private void AllMessageHandler(OscMessage oscMessage) {
string msgString = Osc.OscMessageToString(oscMessage);
string msgAddress = oscMessage.Address;
ArrayList values = oscMessage.Values;
// Wii Balance Board array values:
// [bottom-left, bottom-right, top-left, top-right, sum, cx, cy]
float cx = (float)values[5];
float cy = (float)values[6];
// COP values from balance board are in range 0:1
// Let's map them to an input axis -1:1
float forwardThrottle = (cy - 0.5f) * 2;
float sidewaysThrottle = (cx - 0.5f) * 2;
// Creating an input vector from these values and using it to control a first-person controller
Vector2 boardInput = new Vector2(sidewaysThrottle, forwardThrottle);
fpc.boardInput = boardInput;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment