Search Unity

continuous swipe / drag

Discussion in 'Scripting' started by glebski, Sep 6, 2011.

  1. glebski

    glebski

    Joined:
    Nov 21, 2009
    Posts:
    367
    I'm trying to make a simple zoom script so that the camera zooms in and out based on the horizontal (x) mouse movement. Input.GetAxis("mouse X") is an obvious solution, but it doesn't work on the platform for which I'm programming (windows7 embedded infrared panel). Tried all sorts of combinations of the code below and although it works, zooming the camera, it resets the view in a way which I don't want. The zoom should be continuous, so if I drag my finger right to left I zoom out, but when I lift my finger and start another drag motion I continue from the previous location, NOT a new or reset one. Hope that makes sense and someone can help. I think I'm missing something really obvious.
    Code (csharp):
    1.  
    2.         if (Input.GetKeyDown ("mouse 0")){
    3.         originalPos.x = lastPos.x;
    4.         }
    5.         if (Input.GetMouseButton(0)){
    6.         secondPos.x = Input.mousePosition.x;
    7.        
    8.         var x = Mathf.Min(originalPos.x, secondPos.x);
    9.         var width : int = Mathf.Abs(secondPos.x - originalPos.x); ;
    10.         transform.position.z = originalPos.x - secondPos.x;
    11.         }
    12.         if (Input.GetKeyUp("mouse 0")){
    13.         lastPos.x = Input.mousePosition.x;
    14.         }
    15.        
     
  2. glebski

    glebski

    Joined:
    Nov 21, 2009
    Posts:
    367
    I'm getting there! This does what I want, although I have to divide the width, at the moment by 100 because the scale of, my project doesn't match up with the screen space scale, so the "width" alone is too large and makes the zoom too sensitive and fast. What could I divide the width by to make the zoom slower but more relevant to the scene? All and any ideas welcome. Cheers
    Code (csharp):
    1.   if (Input.GetKeyDown ("mouse 0")){
    2.         originalPos.x = Input.mousePosition.x;
    3.         }
    4.         if (Input.GetMouseButton(0)){
    5.         secondPos.x = Input.mousePosition.x;
    6.         var width : int = Mathf.Abs(secondPos.x - originalPos.x);
    7.         if (originalPos.x < secondPos.x){
    8.         transform.position.z = transform.position.z - width/100;
    9.         }
    10.         if (originalPos.x > secondPos.x){
    11.         transform.position.z = transform.position.z + width/100;
    12.         }
    13.         }
     
  3. glebski

    glebski

    Joined:
    Nov 21, 2009
    Posts:
    367
    OK, so I'm muddling through on my own and have a few questions for any gurus out there. I tested the script on the touchscreen and it works. So that's good. Question one is how to combine the PAN portion, so the translation of the camera can be more fluid, at the moment it is either horizontal or vertical or diagonal, but not really anything in between. The second question is how to link the camera movement to be relative to the screen size, so the amount the camera translates in any direction is linked to the length of the drag.
    Code (csharp):
    1. //PAN TOUCH
    2.         if (GUIControl.menuInt == 5){
    3.         if (Input.GetKeyDown ("mouse 0")){
    4.         originalPos = Input.mousePosition;
    5.         }
    6.         if (Input.GetMouseButton(0)){
    7.         secondPos = Input.mousePosition;
    8.         if (originalPos.x < secondPos.x){
    9.         transform.Translate(-width/100, 0, 0);
    10.         }
    11.         if (originalPos.x > secondPos.x){
    12.         transform.Translate(width/100, 0, 0);
    13.         }
    14.         if (originalPos.y < secondPos.y){
    15.         transform.Translate(0, -height/100, 0);
    16.         }
    17.         if (originalPos.y > secondPos.y){
    18.         transform.Translate(0, height/100, 0);
    19.         }
    20.         }
    21.         }
    22. //ZOOM TOUCH       
    23.         if (GUIControl.menuInt == 6){
    24.         if (Input.GetKeyDown ("mouse 0")){
    25.         originalPos.x = Input.mousePosition.x;
    26.         }
    27.         if (Input.GetMouseButton(0)){
    28.         secondPos.x = Input.mousePosition.x;
    29.         if (originalPos.x < secondPos.x){
    30.         transform.Translate(0, 0, width/100);
    31.         }
    32.         if (originalPos.x > secondPos.x){
    33.         transform.Translate(0, 0,-width/100);
    34.         }
    35.         }
    36.         }