- Home /
Unity Realistic accelerometer controls
Vector3 acc = Vector3.Zero;
void update(){
acc = Vector3.Lerp(acc, Input.acceleration, Time.deltaTime);
transform.position = new Vector3(transform.position.x, (acc * -aVerticalRotation).x, transform.position.z);
}
I have the above code to move my GameObject vertically (along the y-axis). This works fine except that I want the object to move relative to my motion i.e.
If I tilt the phone some X amount very fast it will move distance Z whereas if I tilt the phone with amount X but slower the distance will be A where Z > A.
I've tried to get the current reading subtracting it from the previous reading and multiplying it with the rest but apart from causing a jittery effect it didn't work exactly as I wanted it to.
Can some one help me achieve this ? I'm quite new to Unity.
Thanks
Answer by genetixsparkz · Aug 20, 2011 at 01:27 PM
from what i understand you basically want it so if you move really fast you impart some additional momentum to the object so it moves further.
your got a jittery affect because you multiplied by too high a number (basically you moved the mouse so great a distance in so short a time it skipped whole sections of the screen before being rendered again)
you could prevent this by doing addition instead such as
if current - previous <= 1 add 0 if current - previous <= 5 add 1 if....... all the way up to whatever is a max which doesnt cause jittering like else add 10
alternatly if you used an algorithm you could simply make sure the number returned by that wasn't too high if it was set a limit and redefine it to the max limit so for example if your old algorithm was
new_x = (current - previous) * old_x now you would add
if new_x > MAX_FOR_SMOOTH_TRANSITION new_x = MAX_FOR_SMOOTH_TRANSITION
finally if you really want to get complicated
read this paper on how microsoft made an algorithm for the mouse acceleration in windows. (which is basically what your doing)
http://msdn.microsoft.com/en-us/windows/hardware/gg463319
google windows mouse acceleration algorithm for greater detail
Good to know that my reasoning was correct after all. :) Good idea, thanks for the tips. I'll try it out and see how it goes.
Great my old algorithms is now working perfectly :) !!
Your answer
Follow this Question
Related Questions
Third Person Controller Rotation 0 Answers
How to change the forward direction of a player and make turning smoother 0 Answers
Using Accelerometer to move and rotate a 3d cube from left to right 0 Answers
How can I rotate my player with a Joystick? 0 Answers
Rotate towards movement direction 1 Answer