x


Problems with input.acceleration to implement Shake

I am trying to use the iPhones gyro to implement a shake feature. I particularly care about the direction of the shake.

What I'm finding is "Input.acceleration" doesn't return a acceleration at all but an value corresponding to the tilt of the device.

If you leave the device still but not level , you always get a reading. Hence, if I prop the device up you will find that either Input.acceleration.x or Input.acceleration.y comes back a consistent large value.

A cut down example of the code looks something like this:

var dir : Vector3 = Vector3.zero;    
dir.x = -Input.acceleration.y;    
dir.z = Input.acceleration.x;
dir *= 30;
myObject.rigidbody.velocity += dir;

This is fine if I want a tilting type of control (above works quite well for that) but I'm after acceleration.

Acceleration events I dont think will help me either.

Ideas?

more ▼

asked May 13 '12 at 02:17 PM

Fabkins gravatar image

Fabkins
675 15 20 26

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Now I've come to the acceptance that acceleration isnt acceleration at all but just the current tilt, the solution was pitfully simple. Acceleration is just the difference in tilt since the last reading.

So the code looks something like this:

var dir : Vector3 = Vector3.zero;    
dir.x = - (Input.acceleration.y - lastA);    
dir.z = (Input.acceleration.x - lastB);     
lastA=Input.acceleration.y;
lastB=Input.acceleration.x;
dir *= 100;
myObject.rigidbody.velocity += dir;

Now when I tilt the device I get an initial force in the direction I tilted but the force doesnt persist if I leave the device tilted at an angle.

more ▼

answered May 19 '12 at 11:25 AM

Fabkins gravatar image

Fabkins
675 15 20 26

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1960
x1

asked: May 13 '12 at 02:17 PM

Seen: 605 times

Last Updated: May 19 '12 at 11:25 AM