Skip to content

Instantly share code, notes, and snippets.

@dzeitman
Last active November 17, 2019 00:42
Show Gist options
  • Save dzeitman/affd372ba2a324bcd8d77157d45a6864 to your computer and use it in GitHub Desktop.
Save dzeitman/affd372ba2a324bcd8d77157d45a6864 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
namespace Bose.Wearable
{
public enum Movement{
jump, duck
}
[AddComponentMenu("Bose/Wearable/MovementDetector")]
public class MovementDetector : MonoBehaviour
{
public Movement _movementType;
public AudioSource sound1;
public AudioSource sound2;
public float impulseSensitivty = 10f;
public float timeBetweenImpulse = 1.5f;
public UnityEvent onMovementDetected;
// If you'd like to visually display Jump / Crouch on the screen
public Text debugText;
private Vector3 lastFrameAcceleration = new Vector3(0, 0, 0);
private WearableControl _wearableControl;
private void Awake()
{
if (debugText != null)
debugText.gameObject.SetActive(false);
// Begin in absolute mode and cache the wearable controller.
_wearableControl = WearableControl.Instance;
// Establish a requirement for the acceleration sensor
WearableRequirement requirement = GetComponent<WearableRequirement>();
if (requirement == null)
{
requirement = gameObject.AddComponent<WearableRequirement>();
}
requirement.EnableSensor(SensorId.Accelerometer);
requirement.SetSensorUpdateInterval(SensorUpdateInterval.EightyMs);
}
// after first frame is assigned, we can get the difference between this and last frame.
bool firstFrameAssigned;
// used to count time and enable/disable impulses
float curTime = 0f;
bool disableImpulse = false;
bool jumped = false;
public void Jump()
{
Debug.Log("Jump Detected");
if (jumped)
{
//
GetComponent<Renderer>().material.color = new Color(0, 255, 0); //C sharp
jumped = false;
sound1 = GetComponent<AudioSource>();
sound1.Play(0);
}
else {
GetComponent<Renderer>().material.color = new Color(255, 0, 0); //C sharp
jumped = true;
sound2 = GetComponent<AudioSource>();
sound2.Play(0);
}
}
private void Update()
{
if (_wearableControl.ConnectedDevice == null)
{
return;
}
SensorFrame frame = _wearableControl.LastSensorFrame;
Vector3 newAcceleration = frame.acceleration;
if (!firstFrameAssigned)
{
lastFrameAcceleration = newAcceleration;
firstFrameAssigned = true;
}
else {
Vector3 diff = lastFrameAcceleration - newAcceleration;
lastFrameAcceleration = newAcceleration;
transform.position = diff*0.25f;
if (!disableImpulse){
if (_movementType == Movement.jump)
{
if (diff.y > impulseSensitivty)
{
disableImpulse = true;
if (debugText != null)
{
debugText.gameObject.SetActive(true);
debugText.text = "Jump!";
}
if (onMovementDetected != null)
onMovementDetected.Invoke();
Jump();
}
}
else if (_movementType == Movement.duck)
{
if (diff.y < -impulseSensitivty)
{
disableImpulse = true;
if (debugText != null)
{
debugText.gameObject.SetActive(true);
debugText.text = "Duck!";
}
if (onMovementDetected != null)
onMovementDetected.Invoke();
Debug.Log("Duck Detected");
}
}
}
}
if (disableImpulse){
if (curTime < timeBetweenImpulse){
curTime += Time.deltaTime;
}
else{
curTime = 0;
disableImpulse = false;
if (debugText != null)
debugText.gameObject.SetActive(false);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment