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

IR Touchscreen Navigation

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

  1. glebski

    glebski

    Joined:
    Nov 21, 2009
    Posts:
    367
    So, I have a row of buttons in a selection grid so the buttons can only be selected one at a time. Buttons 5, 6 7 are used for navigating the scene PAN, ZOOM and ORBIT respectively. The touchscreen device does not recognise Input.GetAxis("Mouse x") etc, so a workaround seems to be to calculate mouse position on button down and the difference while dragging.

    Issues: I would like the camera to be offset by the same amount as the length of the drag, following the same direction. To rotate an object, perhaps the total screen width could be somehow converted to 360 degrees, so if I swipe the whole length of the screen the camera orbits around once? Both pan and zoom should slow down the closer you get to a target object.

    Anyone willing to throw in their penny's worth?

    Cheers

    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/(distance*10), 0, 0);
    10.         }
    11.         if (originalPos.x > secondPos.x){
    12.         transform.Translate(width/(distance*10), 0, 0);
    13.         }
    14.         if (originalPos.y < secondPos.y){
    15.         transform.Translate(0, -height/(distance*10), 0);
    16.         }
    17.         if (originalPos.y > secondPos.y){
    18.         transform.Translate(0, height/(distance*10), 0);
    19.         }
    20.         }
    21.         }
    22. //ZOOM TOUCH       
    23.         if (GUIControl.menuInt == 6){      
    24.         if (Input.GetKeyDown ("mouse 0")){
    25.         originalPos = Input.mousePosition;
    26.         }
    27.         if (Input.GetMouseButton(0)){
    28.         secondPos = Input.mousePosition;
    29.         if (originalPos.x < secondPos.x){
    30.         transform.Translate(0, 0, width/(distance*10));
    31.         }
    32.         if (originalPos.x > secondPos.x){
    33.         transform.Translate(0, 0,-width/(distance*10));
    34.         }
    35.         }
    36.         }
    37. //ORBIT TOUCH
    38.         if (GUIControl.menuInt == 7){
    39.         if (Input.GetKeyDown ("mouse 0")){
    40.         originalPos = Input.mousePosition;
    41.         }
    42.         if (Input.GetMouseButton(0)){
    43.         secondPos = Input.mousePosition;   
    44.         x += (secondPos.x - originalPos.x)/100;
    45.         y -= (secondPos.y - originalPos.y)/100;
    46.         var rotation = Quaternion.Euler(y, x, z);
    47.         var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
    48.         transform.rotation = rotation;
    49.         transform.position = position;
    50.         }
    51.         }