Search Unity

Touch Controls

Discussion in '2D' started by jpet1991, Nov 12, 2018.

  1. jpet1991

    jpet1991

    Joined:
    Sep 10, 2018
    Posts:
    35
    Hello,

    I was wondering how I could convert this script into using touch controls on Android or IOS.

    Would it be as easy as just replacing Input.getMouseButttonDown or Input.mousePosition with GetTouch?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DragObject : MonoBehaviour
    6. {
    7.     [SerializeField] float moveSpeed;
    8.     [SerializeField] float saveDelay = 0.2f;
    9.                      float power = 5f;
    10.    
    11.     Rigidbody2D rb;
    12.  
    13.     Vector2 dir;
    14.     Vector2 lastPosition;
    15.  
    16.     DestroyObject inAir;
    17.    
    18.  
    19.     bool nextSave = true;
    20.     bool canBePushed;
    21.     private bool following;
    22.    
    23.  
    24.  
    25.     // Use this for initialization
    26.     void Start()
    27.     {
    28.         following = false;
    29.         lastPosition = transform.position;
    30.         rb = GetComponent<Rigidbody2D>();
    31.  
    32.         inAir = FindObjectOfType<DestroyObject>();    
    33.     }
    34.  
    35.  
    36.  
    37.     // Update is called once per frame
    38.     void Update()
    39.     {
    40.         MouseClicksToDrag();
    41.     }
    42.  
    43.  
    44.     //drags objects
    45.     private void MouseClicksToDrag()
    46.         //detects if object has mouse clicked on it
    47.     {
    48.         if (Input.GetMouseButtonDown(0))
    49.         {
    50.             RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector3.forward);
    51.  
    52.             if (hit.collider != null && hit.collider.gameObject == this.gameObject)
    53.             {
    54.                
    55.                 following = true;
    56.             }
    57.         }
    58.  
    59.         // detects if object has been released to add forces
    60.         if (Input.GetMouseButtonUp(0) && following)
    61.         {
    62.             following = false;
    63.             dir = (Vector2)transform.position - lastPosition;
    64.            
    65.             canBePushed = true;
    66.            
    67.         }
    68.  
    69.         if (following)
    70.         {
    71.             transform.position = Vector2.Lerp(transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition), moveSpeed);
    72.         }
    73.  
    74.         if (nextSave)
    75.         {
    76.             StartCoroutine("SavePosition");
    77.         }
    78.        
    79.     }
    80.  
    81.     void FixedUpdate()
    82.     {
    83.             IfCanBePushed();    
    84.     }
    85.  
    86.  
    87.     private void IfCanBePushed()
    88.     {
    89.        
    90.         {
    91.             if (!inAir.IsFallingTrue())
    92.             {
    93.                
    94.                 if (canBePushed)
    95.                 {
    96.                    
    97.                     canBePushed = false;
    98.                     rb.velocity = dir * power;
    99.                 }
    100.  
    101.             }
    102.            
    103.         }  
    104.  
    105.  
    106.     }
    107.  
    108.     IEnumerator SavePosition()
    109.     {
    110.         nextSave = false;
    111.         lastPosition = transform.position;
    112.         yield return new WaitForSeconds(saveDelay);
    113.         nextSave = true;
    114.     }
    115.  
    116.  
    117.  
    118.  
    119.  
    120. }
    121.  
    122.  
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    I don't have any device to test, also you can try to make it working :D Just as example for touch input
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DragObject : MonoBehaviour
    6. {
    7.     [SerializeField] float moveSpeed;
    8.     [SerializeField] float saveDelay = 0.2f;
    9.     float power = 5f;
    10.  
    11.     Rigidbody2D rb;
    12.  
    13.     Vector2 dir;
    14.     Vector2 lastPosition;
    15.  
    16.     DestroyObject inAir;
    17.  
    18.  
    19.     bool nextSave = true;
    20.     bool canBePushed;
    21.     private bool following;
    22.  
    23.     bool isTouched = false, endTouch = false;
    24.     Vector3 touchPosition;
    25.     int touchId;
    26.  
    27.  
    28.     // Use this for initialization
    29.     void Start()
    30.     {
    31.         following = false;
    32.         lastPosition = transform.position;
    33.         rb = GetComponent<Rigidbody2D>();
    34.  
    35.         inAir = FindObjectOfType<DestroyObject>();
    36.     }
    37.  
    38.  
    39.  
    40.     // Update is called once per frame
    41.     void Update()
    42.     {
    43.         //here you can try to use Input.GetTouch(0), "for loop" is for multitouch
    44.         //maybe you should use id of the touch too, int touchID = touch.fingerId;
    45.         //the id can be needed by check if the needed touch was moved
    46.         //like if (touch.fingerId == touchId) do something
    47.         for (int i = 0; i < Input.touchCount; i++)
    48.         {
    49.             Touch touch = Input.GetTouch(i);
    50.             if (touch.phase == TouchPhase.Began)
    51.             {
    52.                 isTouched = true;
    53.                 touchPosition = touch.position;
    54.                 //touchId = touch.fingerId;
    55.             }
    56.  
    57.             if (touch.phase == TouchPhase.Ended)
    58.             {
    59.                 endTouch = true;
    60.             }
    61.  
    62.             if (touch.phase == TouchPhase.Moved)
    63.             {
    64.                 //if (touch.fingerId == touchId)
    65.                 touchPosition = touch.position;
    66.             }
    67.         }
    68.  
    69.         MouseClicksToDrag();
    70.     }
    71.  
    72.  
    73.     //drags objects
    74.     private void MouseClicksToDrag()
    75.     //detects if object has mouse clicked on it
    76.     {
    77.         //if (Input.GetMouseButtonDown(0))
    78.         if (isTouched)
    79.         {
    80.             isTouched = false;
    81.             //RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector3.forward);
    82.             RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(touchPosition), Vector3.forward);
    83.             if (hit.collider != null && hit.collider.gameObject == this.gameObject)
    84.             {
    85.  
    86.                 following = true;
    87.             }
    88.         }
    89.  
    90.         // detects if object has been released to add forces
    91.         //if (Input.GetMouseButtonUp(0) && following)
    92.         if (endTouch && following)
    93.         {
    94.             endTouch = false;
    95.  
    96.             following = false;
    97.             dir = (Vector2)transform.position - lastPosition;
    98.  
    99.             canBePushed = true;
    100.  
    101.         }
    102.  
    103.         if (following)
    104.         {
    105.             //transform.position = Vector2.Lerp(transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition), moveSpeed);
    106.             transform.position = Vector2.Lerp(transform.position, Camera.main.ScreenToWorldPoint(touchPosition), moveSpeed);
    107.         }
    108.  
    109.         if (nextSave)
    110.         {
    111.             StartCoroutine("SavePosition");
    112.         }
    113.  
    114.     }
    115.  
    116.     void FixedUpdate()
    117.     {
    118.         IfCanBePushed();
    119.     }
    120.  
    121.  
    122.     private void IfCanBePushed()
    123.     {
    124.  
    125.         {
    126.             if (!inAir.IsFallingTrue())
    127.             {
    128.  
    129.                 if (canBePushed)
    130.                 {
    131.  
    132.                     canBePushed = false;
    133.                     rb.velocity = dir * power;
    134.                 }
    135.  
    136.             }
    137.  
    138.         }
    139.  
    140.  
    141.     }
    142.  
    143.     IEnumerator SavePosition()
    144.     {
    145.         nextSave = false;
    146.         lastPosition = transform.position;
    147.         yield return new WaitForSeconds(saveDelay);
    148.         nextSave = true;
    149.     }
    150. }
    151.  
     
  3. jpet1991

    jpet1991

    Joined:
    Sep 10, 2018
    Posts:
    35
    thank you very much, I will try to test this out soon and let you know if this works!