Accelerometer Issue

Hi, I am not sure if I am in the right place…

So, I am making a rythm game with a friend (we are both 18) and we encountered a problem we haven’t managed to solve yet, this is why I am asking your help.

We have some kind of “head” Moving using the accelerometer and destroying notes. The problem is that it gets stuck on the right side (we tried using collider before the code down below but it travelled too fast and just go through it…)

Here is the code we are currently using…


using UnityEngine;
using System.Collections;

public class Activator : MonoBehaviour {

void Start () {

}

// Update is called once per frame
void Update () {

if (Input.acceleration.x > -0.1 || Input.acceleration.x < 0.1) {

if (transform.position.x >= -7 && transform.position.x <= 7) {
transform.Translate (Input.acceleration.x * 2, 0, 0);
}
if (transform.position.x < -7 && Input.acceleration.x > 0) {
transform.Translate (Input.acceleration.x * 2, 0, 0);
}
if (transform.position.x > 7.25 && Input.acceleration.x < 0) {
transform.Translate (Input.acceleration.x * 2, 0, 0);
}

}
}

void OnTriggerEnter2D(Collider2D col){
if (col.gameObject.tag == “GNote”)
Destroy (col.gameObject);
}
}


Thanks for your help !

using UnityEngine;
using System.Collections;

public class Activator : MonoBehaviour {


void Start () {

}

// Update is called once per frame
void Update () {

if (Input.acceleration.x > -0.1 || Input.acceleration.x < 0.1) {

if (transform.position.x >= -7 && transform.position.x <= 7) {
transform.Translate (Input.acceleration.x * 2, 0, 0);
}
if (transform.position.x < -7 && Input.acceleration.x > 0) {
transform.Translate (Input.acceleration.x * 2, 0, 0);
}
if (transform.position.x > 7.25 && Input.acceleration.x < 0) {
transform.Translate (Input.acceleration.x * 2, 0, 0);
}

}
}

void OnTriggerEnter2D(Collider2D col){
if (col.gameObject.tag == "GNote")
Destroy (col.gameObject);
}
}

First off, I’d recommend using the code tags as detailed in the sticky here: Using code tags properly - Unity Engine - Unity Discussions

What device are you using? It’s possible that the accelerometer is only outputting positive values, and so the object is only moving in one direction.

Thanks for your answer !
We are using a Samsung Galaxy A5 and a Xiaomi Redmi 3S.
Should I try a debug.log to see it ?

Are you sure this line is correct?

if (Input.acceleration.x > -0.1 || Input.acceleration.x < 0.1) {

Every value of x will pass that test - maybe you meant it to only accept values if x is < -0.1 or x > 0.1?

e.g.

if (Input.acceleration.x < -0.1 || Input.acceleration.x > 0.1) {

Yes I didn’t notice that.
Thanks for your help tonemcbride !
We did this so the accelerometer doesn’t have to update if the value is too low (making him stop twitching).
One thing I noticed is that we can go to the right corner but when we go back, it works for a really small distance tyhen stuck and can’t move either left or right anymore. I am wondering if it is because of a collider or something… I managed to go through once if I had enought speed.

Hello I forgot to come back…
Our problem is solved we simply made a mistake with the values.