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

Accelerometer input - How to limit rotation on both X and Z axis

Discussion in 'Scripting' started by skinner92, Sep 29, 2016.

  1. skinner92

    skinner92

    Joined:
    Feb 23, 2014
    Posts:
    112
    Hey guys. Take a look at the game I'm trying to build. The idea is to put a sphere inside of the map, and challenge the player to take the ball from one side to the other without letting it fall.



    As you can see, I have several problems:
    • The platform rotates very fast (I need somehow to limit the rotation speed).
    • The platform rotates indefinitely. This is what I'm focusing at right now. There needs to be a "max angle" that the platform can reach on both axis.
    • The platform ends up rotating in the Y-Axis. I need to block this angle and allow only for X and Z rotations.
    This is the script I'm using and a screenshot of the camera position when the game starts:



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TableMovement : MonoBehaviour {
    5.  
    6.     float ROTATE_AMOUNT = 1f; // at full tilt, rotate at 2 degrees per update
    7.  
    8.     float GetTiltValue_X() {
    9.    
    10.         float TILT_MIN_X = 0.1f;
    11.         float TILT_MAX_X = 0.15f;
    12.  
    13.         // Work out magnitude of tilt
    14.         float tilt = Mathf.Abs(Input.acceleration.x);
    15.  
    16.         // If not really tilted don't change anything
    17.         if (tilt < TILT_MIN_X) {
    18.             return 0;
    19.         }
    20.         float tiltScale = (tilt - TILT_MIN_X) / (TILT_MAX_X - TILT_MIN_X);
    21.  
    22.         // Change scale to be negative if accel was negative
    23.         if (Input.acceleration.x < 0) {
    24.             return tiltScale;
    25.         } else {
    26.             return -tiltScale;
    27.         }
    28.  
    29.     }
    30.  
    31.     float GetTiltValue_Y() {
    32.  
    33.         float TILT_MIN_Y = 0.1f;
    34.         float TILT_MAX_Y = 0.15f;
    35.  
    36.         // Work out magnitude of tilt
    37.         float tilt = Mathf.Abs(Input.acceleration.y);
    38.  
    39.         // If not really tilted don't change anything
    40.         if (tilt < TILT_MIN_Y) {
    41.             return 0;
    42.         }
    43.         float tiltScale = (tilt - TILT_MIN_Y) / (TILT_MAX_Y - TILT_MIN_Y);
    44.  
    45.         // Change scale to be negative if accel was negative
    46.         if (Input.acceleration.y < 0) {
    47.             return -tiltScale;
    48.         } else {
    49.             return tiltScale;
    50.         }
    51.  
    52.     }
    53.  
    54.     void Update() {
    55.    
    56.         float tiltValue_X = GetTiltValue_X();
    57.         float tiltValue_Y = GetTiltValue_Y();
    58.  
    59.         transform.RotateAround (Vector3.zero, Vector3.right, tiltValue_Y * ROTATE_AMOUNT);
    60.         transform.RotateAround (Vector3.zero, Vector3.forward, tiltValue_X * ROTATE_AMOUNT);
    61.  
    62.     }
    63. }
    I've tried using:
    Code (CSharp):
    1. Mathf.Clamp(transform.rotation.x, -15f, 15f); Mathf.Clamp(transform.rotation.z, -15f, 15f);
    But it has no effect and the table keeps rotating. Any ideas? Thanks!
     
  2. skinner92

    skinner92

    Joined:
    Feb 23, 2014
    Posts:
    112
    Ideas? Thanks!
     
  3. SilverHuber

    SilverHuber

    Joined:
    Mar 24, 2015
    Posts:
    5
    Have you found a solution for this? I'm also trying to limit the rotation.
     
  4. skinner92

    skinner92

    Joined:
    Feb 23, 2014
    Posts:
    112
    Nope, I never found a solution to this problem :/
     
  5. LeeClayberg

    LeeClayberg

    Joined:
    Feb 14, 2016
    Posts:
    1
    I would recommend using the gyroscope instead of the accelerometer. I just recently made a labyrinth game, and I found that using the gyroscope gives you the ability to check boundaries.


    //if statement to check boundaries of tilt
    if (GyroToUnity (Input.gyro.attitude).x < 0.2 && GyroToUnity (Input.gyro.attitude).x > -0.2 && GyroToUnity (Input.gyro.attitude).z < 0.2 && GyroToUnity (Input.gyro.attitude).z > -0.2) {
    transform.rotation = GyroToUnity (Input.gyro.attitude);
    }

    //converts the data from the gyroscope to a quaternion with the phone Landscape Right, screen facing up[/SIZE]
    private static Quaternion GyroToUnity(Quaternion q)
    {
    return new Quaternion(-q.x /2, 0, -q.y /2, q.w);
    }
    [SIZE=4][/SIZE]


    I hope this helps