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

Accelerometer with Device in Vertical Position?

Discussion in 'Getting Started' started by bcgreen24, May 28, 2016.

  1. bcgreen24

    bcgreen24

    Joined:
    May 28, 2016
    Posts:
    9
    Hi, all-- so after going through the 'Roll-a-Ball' tutorial, I added code for controlling the ball with an accelerometer. It works great except for the fact that the phone has to be 'flat'-- i.e., parallel to the ground. What I'd like to do is modify my code so that the phone can held vertically (i.e., perpendicular to the ground) when tilting it back and forth on the 'x' axis to move the ball forward and back on the 'z' axis.

    Here's the code I have so far:

    Code (CSharp):
    1.     void FixedUpdate () {
    2.         if (SystemInfo.deviceType == DeviceType.Desktop){
    3.             playerX = Input.GetAxis ("Horizontal");
    4.             playerY = Input.GetAxis ("Vertical");
    5.         } else {
    6.             playerX = Input.acceleration.x;
    7.             playerY = Input.acceleration.y;
    8.         }
    9.         Vector3 movement = new Vector3 (playerX, 0.0F, playerY);
    10.         rb.AddForce (movement * 10);
    11.     }
    Hopefully what I'm talkin' about makes sense... :)

    Thanks!
    Bryan
     
    Last edited: May 29, 2016