Search Unity

Move ball gyroscope ANDROID

Discussion in 'Scripting' started by Bydark, Oct 9, 2013.

  1. Bydark

    Bydark

    Joined:
    Oct 9, 2013
    Posts:
    10
    Hi everyone, I have a problem:

    I'm trying to get a ball follow a route, and that in turn can rotate your address with Android gyroscope.

    I tried with the script gyroscope for Android and iPhone, but can not get the ball to rotate on itself, without changing their address on the stage, here are this script:

    Code (csharp):
    1. // iPhone gyroscope-controlled camera demo v0.3 8/8/11
    2. // Perry Hoberman <hoberman@bway.net>
    3. // Directions: Attach this script to main camera.
    4. // Note: Unity Remote does not currently support gyroscope.
    5.  
    6. var gyroBool : boolean;
    7. var gyro : Gyroscope;
    8. var rotFix : Quaternion;
    9.  
    10. function Start() {
    11.    
    12.     var originalParent = transform.parent; // check if this transform has a parent
    13.     var camParent = new GameObject ("camParent"); // make a new parent
    14.     camParent.transform.position = transform.position; // move the new parent to this transform position
    15.     transform.parent = camParent.transform; // make this transform a child of the new parent
    16.     camParent.transform.parent = originalParent; // make the new parent a child of the original parent
    17.    
    18.     gyroBool = Input.isGyroAvailable;
    19.    
    20.     if (gyroBool) {
    21.        
    22.         gyro = Input.gyro;
    23.         gyro.enabled = true;
    24.        
    25.         if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
    26.             camParent.transform.eulerAngles = Vector3(90,90,0);
    27.         } else if (Screen.orientation == ScreenOrientation.Portrait) {
    28.             camParent.transform.eulerAngles = Vector3(90,180,0);
    29.         }
    30.        
    31.         if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
    32.             rotFix = Quaternion(0,0,0.7071,0.7071);
    33.         } else if (Screen.orientation == ScreenOrientation.Portrait) {
    34.             rotFix = Quaternion(0,0,1,0);
    35.         }
    36.         //Screen.sleepTimeout = 0;
    37.     } else {
    38.         print("NO GYRO");
    39.     }
    40. }
    41.  
    42. function Update () {
    43.     if (gyroBool) {
    44.         var camRot : Quaternion = gyro.attitude * rotFix;
    45.         transform.localRotation = camRot;
    46.     }
    47. }

    Then I put this code also to the ball, to go it forward at a constant speed, but when you turn the phone there is no way to change the address, this is the code:

    Code (csharp):
    1. var ConstantSpeed : float = 10;
    2.  
    3. function Start () {
    4.  
    5. }
    6.  
    7. function Update () {
    8.  
    9. transform.position.x = transform.position.x + ConstantSpeed;
    10.  
    11. }

    I hope you can help me with this problem, THANK YOU!