- Home /
Using gyroscope to get forward backward acceleration
I want to use the iPhone gyroscope to simulate the behaviour of a kife moving forward and backward. I'm trying to use user acceleration from the gyro. I think I'm on the right way as debugging values from user acceleration y axis I get values near 0.1 when I move the iPhone forward and -0.1 when I move backward.
This is the code I'm using:
acceleration = Input.gyro.userAcceleration.y;
if (Mathf.Abs(acceleration) < 0.015) acceleration = 0;
velocity += acceleration * 10f * Time.deltaTime;
transform.position = new Vector3(transform.position.x + velocity * Time.deltaTime,transform.position.y,transform.position.z);
The problem is that knife always move forward, without any user interaction on the iPhone. Input.gyro.userAcceleration.y seems to return lot's of noise.
What can I do?
Thanks!
Answer by Mikmik · Jun 27, 2013 at 02:34 PM
For the posterity: A basic solution, might not be the most optimized, is to check if the acceleration is below or above a certain delta (threshold) you define. Basically, what I did for one of my apps:
private float delta = 1.0;
if(Input.gyro.userAcceleration.z < -delta || Input.gyro.userAcceleration.z > delta)
{
return Input.gyro.userAcceleration.z;
}
else
{
return 0.0f;
}
Your answer
Follow this Question
Related Questions
iPhone Gyroscope problem 0 Answers
Use Iphone as sterring wheel 2 Answers
Find how many Elements in Transform[] 1 Answer
Microphone noise on android/iphone 0 Answers
Obtaining and Filtering Gyroscope/Accelerometer Data 1 Answer