- Home /
 
               Question by 
               rupeshpamaihgari · Jan 29, 2014 at 08:12 PM · 
                javascriptguiaccelerometerlabel  
              
 
              How to set GUI X and Y positions from accelerometer.x and accelerometer.y ?
I am new to unity...Can anybody help me in setting GUI label position of x and y from the input of accelerometer using javascript..?...thankyou..!
               Comment
              
 
               
              Answer by zharik86 · Feb 13, 2014 at 08:16 AM
First, it is necessary to consider accelerometer value range. Usually it to be in the range from-1 to +1 on all three axes. (if the range another, is probably necessary calibration). When the device to be on the desktop (parallel to the earth), value of the accelerometer is (0, 0,-1). Then our guilabel shall be in screen center. I will write a simple example, but thus I use landscape orientation. For other orientations, it is possible where that will change a sign, but I am not confident in it. So(write on CSharp):
  private float hlabel = 100.0f; // height label
  private float wlabel = 100.0f; // width label
  private float xlabel = 0.0f; // position center label on X axes
  private float ylabel = 0.0f; //position center label on Y axes
  void Start() {
   xlabel = (float) (Screen.width)/2.0f;
   ylabel = (float) (Screen.height)/2.0f;
  }
  
  void LateUpdate() {
   Vector3 acceler = Input.acceleration;
   //maybe normalize accelerometr data
   acceler.Normalize();
   // creating range
   xlabel = ((float) (Screen.width)/2.0f) * (1 + acceler.x);
   ylabel = ((float) (Screen.height)/2.0f) * (1 + acceler.y);
  }
  void OnGui() {
   GUI.Label(new Rect(xlabel - wlabel/2.0f, ylabel - hlabel/2.0f, wlabel, hlabel), "it is accelerometr");
  }
I hope, it will help you.
Your answer
 
 
             