Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

dragging the camera by touch

Discussion in 'iOS and tvOS' started by snottlebocket, Mar 13, 2010.

  1. snottlebocket

    snottlebocket

    Joined:
    Feb 6, 2010
    Posts:
    18
    Hey guys,

    I'm new to unity and doing a few experiments to learn. Right now I'm trying to make a camera you can move by 'dragging' your finger across the iphone screen.

    The reference suggested that the IphoneInput class was a bit overkill when I don't need multitouch so I used the regular Input class.

    I wrote a basic script that detects mouseDown and mouseUp and sets a velocity for the camera based on mouse / finger movement. Plus a friction value to allow the user to create a nice smooth throwing motion for the camera. The script is attached to the camera.

    Unfortunately my smooth dragging and throwing script seems to work reasonably well when running it inside Unity. It's a bit choppy when using the remote on my ipod touch.

    And oddly enough the throwing effect is completely absent when I build and run on my iphone touch. I can move the camera using touch, but the velocity / friction / throwing effect is completely absent.

    I pasted my script below, if anyone can shed light on what's happening I'd be quite grateful. Any general commentary is welcome to, I'm sure there's better ways to create a draggable camera.

    Code (csharp):
    1.  
    2. var oldX : float = 0.0;
    3. var oldY : float = 0.0;
    4. var vx : float = 0.0;
    5. var vy : float = 0.0;
    6. var friction : float = 0.98;
    7. var dragging : boolean = false;
    8.  
    9. function Update () {
    10.     if(Input.GetMouseButtonDown(0)){
    11.         dragging = true;
    12.         oldX = Input.mousePosition.x;
    13.         oldY = Input.mousePosition.y;
    14.     }
    15.     if(dragging){
    16.         vx = Input.mousePosition.x - oldX;
    17.         vy = Input.mousePosition.y - oldY;
    18.         oldX = Input.mousePosition.x;
    19.         oldY = Input.mousePosition.y;
    20.     }
    21.     if(Input.GetMouseButtonUp(0)){
    22.         dragging = false;
    23.         print("stop dragging");
    24.    }
    25.    print(vx + "  " + vy);
    26.    vx = vx * friction;
    27.    vy = vy * friction;
    28.     transform.position.x += vx;// Input.mousePosition.x - oldX;
    29.     transform.position.y += vy; //Input.mousePosition.y - oldY;
    30.     if(vx < 0.1){
    31.     vx = 0;
    32.     }
    33.     if(vy < 0.1){
    34.     vy = 0;
    35.     }
    36.  
    37.  }
    38.