Tilt Movement help Please!

Am working on a simple sniper game.I did used a backdrop image and camera. Basically camera will have a two modes. Sniper and normal mode. When user double tap the screen the camera zoom and enters in to a sniper mode and the user can use the device tilt to pan vertical and horizontal over the backdrop image. Field of View will be 60(Sniper Mode), if that helps.

Problem:

1.Currently the device only works when the device is flat(In Picture:A) and calculate accordingly. But I need the user can use the tilt function from any angle like B and C. Is there a way?.. Please help me out to solve this. Thanks, for your time.

  1. Also, is there a way to reset the camera to default position in normal mode and in zoom mode it will be controlled using tilt functions from device.

{

public Texture2D sniperTexture;  
public Material  sniperMat;

public  bool sniperMode=false;
private bool isplaying = false;

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;
public float filter= 5.0f;

private Vector3 acceleration;

void Start()
{
  sniperMode = false;	 
}


void OnPostRender()
{
	if(sniperMode)
	Graphics.Blit( sniperTexture,null,sniperMat); 
}


void FixedUpdate()
{ 
	DoubleTap();
	gyroMovement();

}


void DoubleTap()

{
	for (var i=0; i< 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()		
{  
	if(sniperMode=sniperMode)
	{
	
	acceleration = Input.acceleration;
	
	acceleration = Vector3.Lerp (acceleration,Input.acceleration,filter*Time.deltaTime);
	
	Vector3 dir = new Vector3 (acceleration.x,acceleration.y,0);
	
	if(dir.sqrMagnitude > 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
	{
		isplaying = false;
	    
	}
}

void cameraReset()

{

}

}

using UnityEngine;
using System.Collections;

public class TiltToControll : MonoBehaviour {
	
	private Vector3 curAc;
	private Vector3 zeroAc;
	public int smooth=2;

	void Start(){
ResetAxes();
	}
	
	void Update(){
		
		tiltupdown();
		
	}
	
	void ResetAxes(){
		zeroAc = Input.acceleration;
		curAc = Vector3.zero;
	}
	
	
	
	void tiltupdown(){
		curAc = Vector3.Lerp(curAc, Input.acceleration-zeroAc, Time.deltaTime/smooth);
		
		transform.Translate(0,curAc.y,0);
		
	
		
		
	}
}

Hope this helps

What we are doing here is in the start() we are resetting the axis ie taking the current input acceleration at start and subtracting it from the input acceleration in each time tiltupdown() is called…

{
public float maxX = 5.0f;
public float minX = -5.0f;
public float maxY = 5.0f;
public float minY = 5.0f;

public float speed = 6.0f;
public float filter= 5.0f;

public  bool  sniperMode = false;                                                              // Sniper Mode Boolean
public  bool  isplaying  = false;

private Vector3 curAc;                                                                          // Current Acceleration
private Vector3 zeroAc;                                                                         // Zero Acceleration
private Vector3 tempAcc;                                                                        // temp Acceleration

public Vector3  normCamPos;

void Start()
{
	sniperMode = true;                                                                             // Sniper Mode Toggle
	ResetAxes();                                                                                 // Reset Axis for Tilt Function in Zoom Mode/ It works from any position depending on the user.
}	

void FixedUpdate()                                                                              
{
	gyroMovement();
	DoubleTap();
}

void DoubleTap()		
{
	for (var i=0; i< Input.touchCount; ++i)
	{
		if(Input.GetTouch(i).phase == TouchPhase.Began)
		{
			if(Input.GetTouch(i).tapCount == 2)
			{
				sniperMode=!sniperMode;											       
			}				
		}
		else
		{
			isplaying   = false;
			ResetAxes();
		}			
	}
}

void gyroMovement()                                                                                     // Tilt Movement
{
	if(sniperMode=sniperMode)
	{
		curAc = Vector3.Lerp(curAc,Input.acceleration-zeroAc,filter*Time.deltaTime);                    // Vecor 3 values are stored in CurrentAccelration variable.
		
		Vector3 dir = new Vector3(curAc.x,curAc.y,0);                                             
		
		if(dir.sqrMagnitude > 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
	{
		ResetAxes();
	}
}

void ResetAxes()                                                                                        // Reset Axis Function for tilt.
{
	zeroAc = Input.acceleration;
	curAc =  Vector3.zero;
}