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. Dismiss Notice

Touch Input Script

Discussion in 'Scripting' started by jcastaldo13, Mar 12, 2015.

  1. jcastaldo13

    jcastaldo13

    Joined:
    Apr 17, 2014
    Posts:
    10
    I was following the tutorial by Mr. Buckner on moving the space shooter game to mobile. I copied the scripts and followed the video exactly. I am making a pong game and used the scripts for moving the pong paddle with my finger. The problem I am having is the paddle does not follow my finger exactly. It just moves in the direction I move my finger. My question is did I do something wrong or is there a better way to go about using the touch input.

    Thank you
     
    Last edited: Mar 12, 2015
  2. Mike-Geig

    Mike-Geig

    Unity Technologies

    Joined:
    Aug 16, 2013
    Posts:
    220
    Please put your code into the post (instead of linking the files). Click the Insert->Code in the post tools and paste your code into it. Thanks.
     
  3. jcastaldo13

    jcastaldo13

    Joined:
    Apr 17, 2014
    Posts:
    10
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.EventSystems;
    4. using System.Collections;
    5.  
    6. public class TouchScreenControls : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler {
    7.  
    8.     public float smoothing;
    9.  
    10.     private Vector2 origin;
    11.     private Vector2 direction;
    12.     private Vector2 smoothDirection;
    13.     private bool touched;
    14.     private int pointerID;
    15.  
    16.     void Awake () {
    17.         direction = Vector2.zero;
    18.         touched = false;
    19.     }
    20.  
    21.     public void OnPointerDown (PointerEventData data) {
    22.         if (!touched) {
    23.             touched = true;
    24.             pointerID = data.pointerId;
    25.             origin = data.position;
    26.         }
    27.     }
    28.  
    29.     public void OnDrag (PointerEventData data) {
    30.         if (data.pointerId == pointerID) {
    31.             Vector2 currentPosition = data.position;
    32.             Vector2 directionRaw = currentPosition - origin;
    33.             direction = directionRaw.normalized;
    34.         }
    35.     }
    36.  
    37.     public void OnPointerUp (PointerEventData data) {
    38.         if (data.pointerId == pointerID) {
    39.             direction = Vector3.zero;
    40.             touched = false;
    41.         }
    42.     }
    43.  
    44.     public Vector2 GetDirection () {
    45.         smoothDirection = Vector2.MoveTowards (smoothDirection, direction, smoothing);
    46.         return smoothDirection;
    47.     }
    48. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.EventSystems;
    4. using UnityEngine.UI;
    5.  
    6. public class PlayerControls : MonoBehaviour {
    7.  
    8. //    public KeyCode moveUp = KeyCode.UpArrow;
    9. //    public KeyCode moveDown = KeyCode.DownArrow;
    10.  
    11.     public float speed = 10.0f;
    12.  
    13.     public TouchScreenControls pad;
    14.  
    15.     void FixedUpdate()
    16.     {
    17.  
    18.         Vector2 direction = pad.GetDirection ();
    19.         Vector3 movement = new Vector3 (0.0f, direction.y, 0.0f);
    20.         GetComponent<Rigidbody2D>().velocity = movement * speed;
    21.     }
    22. }    
     
    Last edited: Mar 12, 2015
  4. Mike-Geig

    Mike-Geig

    Unity Technologies

    Joined:
    Aug 16, 2013
    Posts:
    220
    Please post the other one. Also, please put both segments of code in the original post. That will make it easier for anyone reading this in the future.
     
  5. jcastaldo13

    jcastaldo13

    Joined:
    Apr 17, 2014
    Posts:
    10
    Ok, the other one is being moderated for some reason
     
  6. jcastaldo13

    jcastaldo13

    Joined:
    Apr 17, 2014
    Posts:
    10
    It wouldnt let me put the code in the original post. It says its inappropriate. I posted the second script under the first one
     
  7. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
  8. jcastaldo13

    jcastaldo13

    Joined:
    Apr 17, 2014
    Posts:
    10
    I tried that already. Didn't fix the issue. Just made the paddle jump faster or slower once it responded. It didn't follow my finger any better.
     
  9. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    if you want the padle to follow exatly your finger you need to transform the toucharea (your display pixel resulution) to the gameworld position

    Code (CSharp):
    1.         GameObject padle;
    2.  
    3.  
    4.         void Update()
    5.         {
    6.             if(Input.touchCount > 0)
    7.             {
    8.                 Vector2 touchPos = Input.touches[0].position;
    9.                 Vector3 touchPosinWorldSpace = Camera.main.ScreenToWorldPoint(new Vector3(touchPos.x, touchPos.y, Camera.main.nearClipPlane));
    10.                 padle.transform.position = touchPosinWorldSpace;
    11.             }
    12.         }
     
  10. jcastaldo13

    jcastaldo13

    Joined:
    Apr 17, 2014
    Posts:
    10
    That works. Thank you, It does bring up a few other problems though. Is there a way to keep it from moving across the x axis. I only want it to move up and down and keep the touches in the touch pad area I designate? Is this possible?
     
  11. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    yes, transform.position and touchPosinWorldSpace are a Vector3 so they have x, y and z axis.

    in the above Scipt you just need to fix the x value. For the left padle different than the right paddle of course.

    Code (CSharp):
    1.             GameObject padle;
    2.  
    3. public float padlePosX = 2f;
    4.  
    5.             void Update()
    6.             {
    7.                 if(Input.touchCount > 0)
    8.                 {
    9.                     Vector2 touchPos = Input.touches[0].position;
    10.                     Vector3 touchPosinWorldSpace = Camera.main.ScreenToWorldPoint(new Vector3(touchPos.x, touchPos.y, Camera.main.nearClipPlane));
    11.  
    12.                     touchPosinWorldSpace.x = padlePosX ;
    13.                     padle.transform.position = touchPosinWorldSpace;
    14.                 }
    15.             }
    16.  
     
  12. jcastaldo13

    jcastaldo13

    Joined:
    Apr 17, 2014
    Posts:
    10
    Thank you so much for your help. Everything works perfectly now!! :)