- Home /
How to make a camera zoom on a particular part of image plane?
Am creating a sniper game and would like to know when I double tap on a device the camera should only zoom on that particular place and when double tap again it should be back to its normal mode.
I did a script where I have been able to achieve zoom and tilt functions to some extent though it is not 100% right.. I would highly appreciate if anyone can solve my problem and can fix my code, Thanks a ton in advance :)
{ 
 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 curAc;
 private Vector3 zeroAc;
 
 
 void Start()
 {
     sniperMode = false;
     ResetAxes();
 }
 
 
 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() // Tilt Function
 {
     if(sniperMode=sniperMode)
     {                        
         curAc = Vector3.Lerp (curAc,Input.acceleration,filter*Time.deltaTime);
         
         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
     {
         isplaying = false;            
     }
 }
 void ResetAxes()
 {
     zeroAc = Input.acceleration;
     curAc  = Vector3.zero;
 }
}
Answer by Maerig · May 01, 2014 at 08:13 AM
You can get a ray from the camera to the tapped position like this :
 Ray ray camera.ScreenPointToRay(Input.GetTouch(i).position);
Then, you can zoom in the ray direction.
Oh, and you'll need to change if(sniperMode=sniperMode) to if(sniperMode==sniperMode). 
Your answer
 
 
             Follow this Question
Related Questions
Camera movement and zoom bounds 0 Answers
How do you differentiate between pinch to zoom with two fingers and a two finger drag? 2 Answers
Camera and Mouse Movement 0 Answers
Camera Zoom/custom projection having trouble. 1 Answer
How to add Pinch to Zoom? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                