Search Unity

I have issue with mobile touch control

Discussion in 'Getting Started' started by adyian, Feb 17, 2017.

  1. adyian

    adyian

    Joined:
    Feb 17, 2017
    Posts:
    4
    Hi i just started making my first game in unity 5 on android. I working on game pong
    and i want use mobile DualTouchControls only move touch area. But i´m begginer and i dont now how to control Padle what i created on android.On pc i can control my padle with ↑ and ↓ . I don´t now how to code script to make my padle move vertical when i touch the screen in my mobile (android).This is padle script:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Padle : MonoBehaviour {

    public float paddleSpeed = 1;
    public Vector3 playerPos;

    void Update ()
    {
    float yPos=gameObject.transform.position.y + (Input.GetAxis("Vertical") * paddleSpeed);
    playerPos=new Vector3 (-20,Mathf.Clamp(yPos, -12.0f, 12.5f),0);
    gameObject.transform.position=playerPos;
    }
    }


    BTW SORRY FOR MY ENGLISH NOt MY NATIVE LANGUAGE :)
     

    Attached Files:

    • Padle.cs
      File size:
      806 bytes
      Views:
      795
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Please use code tags when you post code; it makes it much easier to read.

    To do this, you will iterate over Input.touches, and move the paddle towards the touch position (for any touch on the correct side of the screen). Something like this (rough, untested code):

    Code (CSharp):
    1. void Update() {
    2.     foreach (Touch touch in Input.touches) {
    3.         // Get the position of the touch, in screen coordinates
    4.         Vector3 touchPos = touch.position;
    5.        
    6.         // Convert that to world coordinates
    7.         touchPos.z = Mathf.Abs(Camera.position.z);
    8.         Vector3 hitPos = Camera.main.ScreenToWorldPoint(touchPos);
    9.        
    10.         // ignore this touch if it's on the wrong side of the screen!
    11.         if (hitPos.x < 0) continue;
    12.        
    13.         // Now, move our transform towards the position hit (in Y only).
    14.         Vector3 myPos = transform.position;
    15.         myPos.y = Mathf.MoveTowards(myPos.y, hitPos.y, paddleSpeed * Time.deltaTime);
    16.         transform.position = myPos;
    17.     }
    18. }
    NOTE: In your code, you forgot to multiply paddleSpeed by Time.deltaTime. This is very important; otherwise the actual speed will vary with the frame rate. To get a constant speed, use paddleSpeed * Time.deltaTime. But this means you will need to increase your paddleSpeed value by a factor of 50 or 60 (that is, initialize paddleSpeed to 60 instead of 1).
     
  3. adyian

    adyian

    Joined:
    Feb 17, 2017
    Posts:
    4
    Still not working on my mobile . I can´t move padle vertical.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You'll have to debug it then. My post was just example code to get you started.
     
  5. adyian

    adyian

    Joined:
    Feb 17, 2017
    Posts:
    4
    Okey i try. Thanks for help
     
  6. adyian

    adyian

    Joined:
    Feb 17, 2017
    Posts:
    4
    And i have problem with unity remote 5 on android. When i input usb in my notebook and try play game nothing happened. I edit on editor setthings i enable mobile input but still nothing happened. I dont now why.