Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Android Touch Input?

Discussion in 'General Discussion' started by RSharma98, May 17, 2015.

  1. RSharma98

    RSharma98

    Joined:
    Apr 27, 2014
    Posts:
    21
    Hi, I'm new to Unity and I tried to ask a question on Unity Answers but to my luck I got rejected, again. So I was wondering if someone on the Unity Forums would be kind enough to assist me.

    I tried to ask this question and I got the response "Read the docs, please.". Although I'm pretty sure everyone reads the docs before asking a question and everytime I ask a question I always get a similar response.

    Anyways, the question is as follows:

    "[Before anyone says, I'd just like to say I have looked at the similar questions and I could not really understand, hence I am asking the question again]

    Hi, I'm new to Unity and I'm making a simple game of Pong. I've got the game to work perfectly fine on PC, but I'm struggling to make it work on Android.

    The movement code I have for PC is shown below.

    1. var moveUp:KeyCode;
    2. var moveDown:KeyCode;
    3. var speed:float=15;
    4. functionUpdate(){
    5. if(Input.GetKey(moveUp)){
    6. rigidbody2D.velocity.y = speed;
    7. }
    I was wondering how I would do something similar on Android. Maybe something along the lines of

    1. if(*TouchPosition*.y>Screen.height/2){
    2. rigidbody3D.velocity.y = speed;
    3. }
    But I'm not exactly sure what 'TouchPosition' would be in JavaScript code

    I appreciate any help and thanks in advance"

    Thank you to anyone who answers or attempts to answer :)
     
  2. calmcarrots

    calmcarrots

    Joined:
    Mar 7, 2014
    Posts:
    654
    Code (csharp):
    1.  
    2. foreach (Touch touch in Input.touches)
    3. {
    4.         if (touch.position.y > Screen.height / 2)
    5.                 rigidbody.velocity.y = speed;
    6. }
    7.  
    You could also do
    Code (csharp):
    1.  
    2. Touch touch = Input.GetTouch(0);
    3. if (touch.position.y > Screen.height / 2)
    4.       rigidbody.velocity.y = speed;
    5.  
    Please take your time and study this code. If there is something you dont understand, then do some research. It could possibly save you a trip to the forums.

    Next time post in the correct forums. This is not a general discussion thing. This is a scripting question. Also, try to Google your question because this has been solved many many times. Anyways, good luck
     
    Last edited: May 18, 2015
    RSharma98 likes this.
  3. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    If you read documentation before posting, and the docs haven't given you enough information, then perhaps include in the post links to the pages you've read, and explain the things you don't understand. I personally think the Touch documentation (which you've read) is pretty good.
     
    RSharma98 likes this.
  4. RSharma98

    RSharma98

    Joined:
    Apr 27, 2014
    Posts:
    21
    I agree that they are good and very informative. But for some reason I can't understand mobile related Unity Docs but I can perfectly understand PC-related Docs. I will give it another go and hope it works out
     
  5. RSharma98

    RSharma98

    Joined:
    Apr 27, 2014
    Posts:
    21
    Thanks, and sorry for posting in the wrong forums. This code seems good and I'll try it out later on :D