Recognize And Read Input From An External Accelerometer

Can anyone help me to coax Unity to recognize and read input from an external accelerometer (HC-06) that is connected via Bluetooth to an Android device? The script will be attached to a game object that will respond to changes in (9) axes in near time.

Here is a cool little script by N3K EN reproduced below that would drive the game nicely if I could get Unity to play with the external accelerometer, which clips to a real-world version of the game object. I’d greatly appreciate any advice on how to expand this script to make that work.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Accelerometer : MonoBehaviour
{
    public bool isFlat = true;
    private Rigidbody rigid;
    private void Start()
    {
    rigid = GetComponent<Rigidbody>();
    }

    private void Update()
    {
        Vector3 tilt = Input.acceleration;

        if (isFlat)
            tilt = Quaternion.Euler(0, 0, 0) * tilt;

       rigid.AddForce(tilt);
    }
}

A minor additional question is whether you think the internal accelerometer that is integrated into many Android (or iOS) devices needs to be disabled to make this work.

Thanks!

You’ll need to somehow bridge Unity over to a Bluetooth connection. Unity doesn’t do this but there are packages on the asset store to do it. No idea about any of them. Google up “unity asset store bluetooth.” Depending on the package you’ll also have to handle pairing, disconnection, and probably some kind of stack / channel selection to know what part of the Bluetooth stack you want to talk to for your hardware.

As for the script above, have you actually tried it with the internal accelerometer? It doesn’t at first glance appear like it will do anything except make the in-game object fall with “real gravity,” which if you’re not watching it with a moving camera or catching it with some other collider geometry, will cause to to quickly disappear from view.

Kurt-Dekker—Thanks for this helpful advice. I will check it out. Your comment about gravity is also appreciated; this particular game object has rigid body geometry with “use gravity” unchecked.