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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rotating Screen with Accelerometer

Discussion in 'Scripting' started by LeeDenbigh, Oct 21, 2015.

  1. LeeDenbigh

    LeeDenbigh

    Joined:
    Jul 7, 2014
    Posts:
    48
    Hi, I have a platform game that uses the accelerometer to move left and right. I'd like to add a feature were the screen rotates opposite to the accelerometer so that the camera is always in the original position, so the scene doesn't move with the rotation.

    Can anyone help me please?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    You're going to need the Mathf.Atan2() function, which takes cartesian coordinates and gives you a rotation (in radians).

    The code in my tilt-to-control game looks like so:

    Code (csharp):
    1. float lateral = Input.acceleration.x;
    2. float vertical = Mathf.Sqrt(
    3.      Input.acceleration.y * Input.acceleration.y +
    4.      Input.acceleration.z * Input.acceleration.z);
    5. float angle = -(Mathf.Atan2 ( lateral, vertical) * 180.0f) / Mathf.PI;
    6.  
    And then angle is used to set the Z rotation on the camera with something like

    Code (csharp):
    1. camTransform.localRotation = Quaternion.Euler( 0, 0, angle);
    Of course that assumes your camera is looking down the Z axis.

    Note that the above code combines the Y and Z accelerometer axis to consider what is the vertical component, which lets you play in a wider range of possible holding-the-device orientations.
     
    LeeDenbigh likes this.
  3. LeeDenbigh

    LeeDenbigh

    Joined:
    Jul 7, 2014
    Posts:
    48
    Thank's so much for that, it works. Is there a way that I can make it look more smooth though, as the screen is extremely shaky.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Excellent question, sir! Look into damping it (the value of angle) with an accumulated value.

    Essentially you would have a class level member variable that might be called accumulatedAngle.

    You would compute angle as above, but you would instead feed accumulatedAngle into the transform for the camera.

    In between the calculation of angle and the application of accumulatedAngle, you would do a weighted filter such as this line here:

    Code (csharp):
    1. accumulatedAngle = Mathf.Lerp( accumulatedAngle, angle, 2.5f * Time.deltaTime);
    You can try other values for 2.5f, which is the "snappiness" of the filter.
     
  5. LeeDenbigh

    LeeDenbigh

    Joined:
    Jul 7, 2014
    Posts:
    48
    How would I acquire "accumulatedAngle"? Sorry, I'm still a bit of a rookie with scripting, Kurt.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    The code above essentially gives you a variable low-pass filter (look that term up) that smooths out the high-frequency jitter that comes in via accelerometer.

    "accumulatedAngle" is the variable name I chose for the filter's one single state cell. It's about the simplest low-pass filter you can create.