Search Unity

PC Standalone to Android mobile.[Ping Pong Game] swipe touch problem

Discussion in 'Scripting' started by ianmhart, Jan 22, 2016.

  1. ianmhart

    ianmhart

    Joined:
    Mar 16, 2014
    Posts:
    28
    Hi guys. I have a problem i want to convert the game into android mobile game not on the PC.
    My problem is how can i convert Keyboard key to Swipe touch here's the code below:


    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var moveUp : KeyCode;
    4. var moveDown : KeyCode;
    5.  
    6. var speed : float = 10;
    7.  
    8. function Update ()
    9. {
    10.     if (Input.GetKey(moveUp))
    11.     {
    12.         rigidbody2D.velocity.y = speed;
    13.     }
    14.     else if (Input.GetKey(moveDown))
    15.     {
    16.         rigidbody2D.velocity.y = speed *-1;
    17.     }
    18.     else
    19.     {
    20.         rigidbody2D.velocity.y = 0;
    21.     }
    22. }


    Here the SS :


    Thank you guys.
     
    Last edited: Jan 22, 2016
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    The basics will be you will accept the touch input from the Input class and compare it with the onscreen location of the player's paddle. If they are above the paddle, then do the code for "Up," or else do the code for "Down."

    If you want the paddle to instantaneously track the touch, then you can convert the other way, and drive the position of the paddle based on screen-to-world-converted touch.

    If you want to convert back and forth between Screen and World space, there are lots of handy methods in the Camera class, and lots of code examples out on the intertubes.

    I made a cheeseball one-script pong project here last year:

    https://bitbucket.org/kurtdekker/pong_solitaire

    Help yourself, I forget exactly how I did everything in there. There might be something useful, not sure. :)
     
    ianmhart likes this.
  3. MOSTY

    MOSTY

    Joined:
    Sep 6, 2013
    Posts:
    266