Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to find out if the phone is moving downward in the real world using sensors

Discussion in 'Input System' started by vvahla, Aug 23, 2022.

  1. vvahla

    vvahla

    Joined:
    Nov 5, 2019
    Posts:
    5
    Hi,

    Since I did not find a straight answer to this from the internet I decided to ask here. This is my first post in the Unity forums btw!

    I am making a game for mobile where the phone is supposed to know every time when the user does a squat in real world. The idea is that the user will put the phone in their pocket and when they squat down the phone will give some kind of feedback as well as when they get back up.

    I learned a lot about accelerometer and gyroscope and how they work. However I could not come up with a solution that will know when the user moves down in the real world regardless of how the phone is oriented.
    I believe i will have to somehow determine which way is down and how does the gravity downwards change when the user is moving down? Or maybe I'm not in the right track at all. Could someone point me in the right direction as I'm completely lost at this point, thank you!:)
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,456
    It will mostly be the gyroscope you need to use. Track acceleration and slowing down. Use that to decide if a squad is done.
    Rotation can be used to do extra security checks maybe, but it can definitely be cheated as with all motion based controlls
     
  3. vvahla

    vvahla

    Joined:
    Nov 5, 2019
    Posts:
    5
    Thanks for the reply. How can I track acceleration towards the ground regardless of the phones orientation? That is still something I have not figured out. I can get the X, Y, and Z values of the acceleration but they behave differently in all different phone orientations.
     
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,456
    Does the orientation matter? Or can you do multiple exercises at once?
    If not, maybe just use the accelerometer to count. You could maybe do a comparison between start and stop rotation of the gyro? So you need to bend a certain amount before it fully counts?
     
  5. vvahla

    vvahla

    Joined:
    Nov 5, 2019
    Posts:
    5
    The only thing I want to get is the moment when user has the phone in their pocket and has squatted down. I still just don't quite understand how can I get the change of the phones y-position related to the ground. (How high the phone is from the ground)
     
  6. vvahla

    vvahla

    Joined:
    Nov 5, 2019
    Posts:
    5
    Ok so I think I can use the acceleration.magnitude value to determine if the phone is accelerating downwards or upwards. The problem is when I stop moving the phone it momentarily thinks I'm accelerating in the opposite direction since I'm slowing down.
    For example when I move down acceleration.magnitude might be 0.8 but when I stop it goes up to 1.2 for a moment. When the phone is stable the magnitude is 1. Should I use some other parameter than acceleration.magnitude or is it best to use this parameter regardless of this issue.
     
  7. vvahla

    vvahla

    Joined:
    Nov 5, 2019
    Posts:
    5
    I have now tried to calculate this by multiplying the normalized gravity vector from the gravitysensor with the linear acceleration vector and trying to use the magnitude to know if the phone is only moving downward but that wont work either. I am losing hope as this turned out to be a lot harder than expected. If someone comes up with a solution please tell me.