title: Android device tilt from various positions
body: <p>I am developing a 2D sniper game in Unity. It's a very simple game involving a camera and a plane (backdrop image on it).</p>
  
  <p>I want the camera to change between sniper and  normal mode by double tapping the device. In normal mode the camera will be static. So, no movement at all. In sniper mode the camera pans on the x and y axis depending on the device position. I did achieved this effect (not correctly, though).</p>
  
  <p>Problem: Camera should take the tilt from the position in which the user hold their device. Currently it is working from flat position only.</p>
  
  <pre class="lang-c# prettyprint-override"><code>public Texture2D sniperTexture; // Sniper Texture
  public Material  sniperMat; // Sniper Material
  
  public   bool  sniperMode = false; // Sniper Mode Boolean
  private  bool  isplaying  = false; // Sniper and Tilt Switch
  
  public  float maxX = 0.0f;
  public  float minX = 0.0f;
  public  float maxY = 0.0f;
  public  float minY = 0.0f;
  
  public  float speed = 6.0f; // Speed for Current Acceleration
  public  float filter= 5.0f; // Filter Value for Tilt Function
  
  
  private Vector3 curAc; // Current Acceleration
  private Vector3 zeroAc; // Zero Acceleration
  private Vector3 tempAcc; // temp Acceleration
  
  public Vector3  normCamPos;
  
  
  void Start()
  {
      sniperMode = false; // Sniper Mode Toggle
      ResetAxes(); // Reset Axis for Tilt Function in Zoom Mode/ It works from any position depending on the user.
  }   
  
  
  void OnPostRender() // During Post Render Sniper Texture is been displayed
  {
      if(sniperMode)
      Graphics.Blit( sniperTexture,null,sniperMat); // Sniper Texture
  }
  
  
  void FixedUpdate()                                                                              
  {
      DoubleTap();
      gyroMovement();
  }
  
  
  void DoubleTap()        
  {
      for (var i=0; i&lt; Input.touchCount; ++i)
      {
          if(Input.GetTouch(i).phase == TouchPhase.Began)
          {
              if(Input.GetTouch(i).tapCount == 2)
              {
                  sniperMode=!sniperMode;
                  Camera.main.fieldOfView = sniperMode ? 20 : 60;
                  isplaying = true;                          
              }               
          }
          else
          {
                 isplaying = false;                       
          }           
      }
  }
  
  
  void gyroMovement() // Tilt Movement
  {
      if(sniperMode=sniperMode)
      {   
          tempAcc = Vector3.Lerp(curAc,Input.acceleration-zeroAc,filter*Time.deltaTime);                    // Vecor 3 values are stored in CurrentAccelration variable.
  
          Vector3 dir = new Vector3(tempAcc.x,tempAcc.y,0);                                             
  
          if(dir.sqrMagnitude &gt; 1)           
  
          dir.Normalize();
  
          transform.Translate(dir*speed*Time.deltaTime);
          transform.Translate(dir*speed*Time.deltaTime);          
  
          var pos = transform.position;
  
          pos.x = Mathf.Clamp(pos.x, minX,maxX);
          pos.y = Mathf.Clamp(pos.y, minY,maxY);
  
          transform.position = pos;
      }
      else
      {
          transform.position = normCamPos;
          isplaying = false;
      }
  }
  
  void ResetAxes() // Reset Axis Function for tilt.
  {
      zeroAc = Input.acceleration;
      curAc =  Vector3.zero;
  }
  </code></pre>