Skip to content

Instantly share code, notes, and snippets.

@clavis-magna
Created October 12, 2014 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clavis-magna/14863a3af4f5d9a50e72 to your computer and use it in GitHub Desktop.
Save clavis-magna/14863a3af4f5d9a50e72 to your computer and use it in GitHub Desktop.
zero gravity oculus rift character controller with arduino input
// attach this script to the OVRCameraController prefabs parent object
// add a rigid body to the OVRCameraController prefabs parent object
// turn gavity off on the rigidbody
// !! requires purchase of Uniduino from the asset store;
using UnityEngine;
using System.Collections;
using Uniduino;
public class playerScriptArduino : MonoBehaviour {
//how fast do we want to fly
public float speedMultiplier = 5;
//drag one of the oculus OVRCameraController cameras onto this in the inspector
//so we can get its rotation
public GameObject cameraForRotation;
public Arduino arduino;
//input put for button to be attached to
public int pin = 2;
//output pin with LED (usually inbuilt) attached
public int testLed = 13;
public int pinValue;
void Start()
{
arduino = Arduino.global;
arduino.Setup(ConfigurePins);
}
void Update ()
{
// read the value from the digital input
pinValue = arduino.digitalRead(pin);
// apply that value to the test LED
arduino.digitalWrite(testLed, pinValue);
gameObject.transform.rotation = cameraForRotation.transform.rotation;
rigidbody.AddForce((transform.forward * Input.GetAxis("Vertical")) * speedMultiplier);
if (Input.GetButton ("Fire1") || pinValue == 1)
{
rigidbody.AddForce(transform.forward * speedMultiplier);
}
}
void ConfigurePins( )
{
arduino.pinMode(pin, PinMode.INPUT);
arduino.reportDigital((byte)(pin/8), 1);
// the button will also make the LED on pin 13 turn on. Usefull for debugging
arduino.pinMode(testLed, PinMode.OUTPUT);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment