Advertisement
Guest User

orbit zoom

a guest
Jan 27th, 2014
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.12 KB | None | 0 0
  1. public Transform player; //main camera
  2. public Transform targetObject; //the object that camera to orbit
  3. public float distance = 10.0f;
  4. public float xSpeed = 250.0f;
  5. public float ySpeed = 120.0f;
  6. public float yMinLimit = -20.0f;
  7. public float yMaxLimit = 80.0f;
  8.  
  9. private float x = 0.0f;
  10. private float y = 0.0f;
  11. private int xsign = 1;
  12.    
  13. //[Zoom]
  14. public Vector3 lastZoomPosition;
  15. public float zoomSpeed = 5.0f;
  16.    
  17. //buckets for caching our touch positions
  18. private Vector2 currTouch1 = Vector2.zero, lastTouch1 = Vector2.zero, currTouch2 = Vector2.zero, lastTouch2 = Vector2.zero;
  19.    
  20. private float currDist = 0.0f, lastDist = 0.0f, zoomFactor = 0.0f;
  21. //[Zoom]
  22.  
  23. public void OnTouchMoved()
  24. {
  25.     if (Input.touches.Length == 2) {
  26.         Zoom();
  27.     }else if (Input.touches.Length == 1){
  28.         x += xsign * Input.GetTouch(touch2Watch).deltaPosition.x * xSpeed *0.02f;
  29.  
  30.             y -= Input.GetTouch(touch2Watch).deltaPosition.y * ySpeed *0.02f;
  31.        
  32.         y = Mathf.Clamp(y, 0, 90);
  33.        
  34.         Quaternion rotation = Quaternion.Euler(y, x, 0);
  35.             Vector3 tempDistance = new Vector3(0.0f, 0.0f, -distance);
  36.         Vector3 position = rotation * tempDistance + targetObject.position;
  37.        
  38.             player.rotation = rotation;
  39.             player.position = position;
  40.            
  41.        
  42.     }
  43.  
  44. }
  45.  
  46. void Zoom()
  47. {
  48.     //Caches touch positions for each finger
  49.     switch(TouchLogic.currTouch)
  50.     {
  51.         case 0://first touch
  52.                 currTouch1 = Input.GetTouch(0).position;
  53.                 lastTouch1 = currTouch1 - Input.GetTouch(0).deltaPosition;
  54.                 break;
  55.         case 1://second touch
  56.                 currTouch2 = Input.GetTouch(1).position;
  57.                 lastTouch2 = currTouch2 - Input.GetTouch(1).deltaPosition;
  58.                 break;
  59.         }
  60.  
  61.         if(TouchLogic.currTouch >= 1)
  62.         {
  63.             currDist = Vector2.Distance(currTouch1, currTouch2);
  64.             lastDist = Vector2.Distance(lastTouch1, lastTouch2);
  65.         }
  66.         else
  67.         {
  68.             currDist = 0.0f;
  69.             lastDist = 0.0f;
  70.         }
  71.  
  72.         //Calculate the zoom magnitude
  73.         zoomFactor = Mathf.Clamp(lastDist - currDist, -30.0f, 30.0f);
  74.  
  75.         //apply zoom to camera
  76.         player.Translate(Vector3.forward * zoomFactor * zoomSpeed * Time.deltaTime);
  77.        
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement