Search Unity

[iOS] Moving an object when a tilt is detected -- Help

Discussion in 'Scripting' started by SirCrono, Apr 10, 2012.

  1. SirCrono

    SirCrono

    Joined:
    Apr 3, 2012
    Posts:
    6
    Hi, I'm working on a proof of concept for a new project and I've been trying to come up with a solution for this particular problem.

    I need an object (a cube) to move when a tilt in the device is detected, the movement has to be exact. So far I have two incomplete solutions. This first solution I came up with is this


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Controller : MonoBehaviour {
    6.    
    7.     public float posicionX = -0.5f;
    8.     public float posicionZ = -0.5f;
    9.     public float posicionY = 0.5f;
    10.    
    11.     // Update is called once per frame
    12.    
    13.     void Update () {
    14.        
    15.         // Default position of the cube
    16.        
    17.         transform.position = new Vector3(posicionX, posicionY, posicionZ);
    18.        
    19.        
    20.        
    21.         //Obtain tilt value from accelerometer -- Movement in X
    22.         float tiltAngleX = Input.acceleration.x;
    23.         Debug.Log ("Change in X: " + tiltAngleX);
    24.        
    25.         //Define direction of movement
    26.         if (tiltAngleX>0.5)
    27.             posicionZ ++;
    28.         else
    29.             if(tiltAngleX<-0.5)
    30.                 posicionZ --;
    31.        
    32.         //Obtain tilt value from accelerometer -- Movement in Y
    33.         float tiltAngleY = Input.acceleration.y;
    34.        
    35.         //Define direction movement
    36.         if (tiltAngleY>0.5)
    37.             posicionX --;
    38.         else
    39.             if(tiltAngleY<-0.5)
    40.                 posicionX ++;
    41.    
    42.     }  
    43.        
    44. }
    45.  
    With this solution, I have the exact movement that I want, and if the device is held with certain degree of inclination the movement is constant, the problem here is that since the update is called every frame, the movement is too fast, and I'm looking for a way to make it slower.

    The second solution that I tried, was using a rigid body and applying a force, it works but the movement it's too imprecise.

    I would appreciate if anybody has an insight on how to solve this particular problem.

    Thanks to all and regards.

    SirCrono.
     
  2. SirCrono

    SirCrono

    Joined:
    Apr 3, 2012
    Posts:
    6
    Nevermind, I've found a solution.

    Thanks.

    SirCrono
     
  3. Niki.j

    Niki.j

    Joined:
    Oct 17, 2012
    Posts:
    157
    Hello SirCrono,

    i am new in unity 3D gaming,and i am trying to move my rigid body in space using accelerometer.
    the game play is in landscape.

    and i am stuck with the same problem.and i found you had solved the problem before.
    any suggestions or code will be very much helpful for me.


    Thanks in Advance!
    Niki.j