Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to detect jumps with accelerometer

Discussion in 'Scripting' started by odival, Aug 17, 2016.

  1. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
    Hello, I'm trying to use my Android phone and it's accelerometer to detect whenever the player jumps in the real world. What is the best approach for reliable jump detection via accelerometer?

    So far I have used Input.acceleration.x (since I'm using the phone in landscape position) and a treshold variable and it kinda works, however, the jump function can also be triggered by fast rotational motions.

    To avoid this I tried using something like the pseudocode below, to check not only acceleration in the X axis but also the magnitude of the movement:
    Code (CSharp):
    1. //This is pseudocode, just to illustrate what I did
    2.  
    3. if (acceleration.x > treshold && magnitude > treshold2)
    4. {
    5. //jump logic here
    6. }
    Unfortunatelly I can see no difference from the previous iteration and so I decided to ask you guys for help and suggestions.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Its going to be almost impossible to distinguish a jump in the real world from other similar events, like the user simply moving the phone up and down.

    I would go full blown empirical for this. Get a bunch of different phones. Get a bunch of different users. Run them through a series of tests doing jumps and not jumps. Log the data from the accelerometer. Then figure out what exactly constitutes a jump from the data you have gathered.

    Its not a straight forward exercise. The real world is never quite as pretty as the simulations we build for it.
     
    odival likes this.
  3. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
    Yeah, thanks. I do agree that this is necessary. However, code-wise, what would be the best approach? Is it a good idea to work with Input.aceleration or should I stick with magnitude? Is it possible to check the magnitude in a given axis only? If I use both the aceleration and magnitude, using a simple if ( A > x && M > y) a good way to do it?