Search Unity

Unity 2D Drag and Drop with Android not accurate

Discussion in 'Scripting' started by victorML, Apr 20, 2018.

  1. victorML

    victorML

    Joined:
    Jul 5, 2017
    Posts:
    38
    Hi, my name is Víctor Martínez and I'm having problems making a 'Drag and Drop' Script with Android.

    I found a lot of posts trying to answere this question but I couldn't achieve my objective.

    My script is easy to understand.
    Button1 SpriteRenderer is passed as the Sprite I want to move with my fingers but the results are a really not accurate movement of the button getting dropped randomly.

    I tryed with RigidBody2d with other scripts but nothing, any help plis ?

    Code (CSharp):
    1. public class buttonScript : MonoBehaviour {
    2.     public SpriteRenderer button1;
    3.     public SpriteRenderer button2;
    4.     public Vector3 touchPosition;
    5.     Touch touch;
    6.  
    7.     bool buttonTouched(SpriteRenderer button,Touch touch){
    8.         bool buttonTouched = false;
    9.         //Button Position Coordinates
    10.         Vector3 buttonPosition = button.transform.position;
    11.         float bX = buttonPosition.x;
    12.         float bY = buttonPosition.y;
    13.         float bZ = 9.9f;
    14.  
    15.  
    16.         //Touch Position Coordinates
    17.         touchPosition = new Vector3 (touch.position.x, touch.position.y, bZ ); //Touch position in pixel coordinates
    18.  
    19.         touchPosition = Camera.main.ScreenToWorldPoint (touchPosition); //Pass touch position to world coordinates
    20.  
    21.         if((touchPosition.x>=bX-0.6 && touchPosition.x<=bX+0.6) && (touchPosition.y<=bY+0.6 && touchPosition.y>=bY-0.6)){
    22.             buttonTouched = true; //Button was touched
    23.         }
    24.         return buttonTouched;
    25.  
    26.     }
    27.     // Use this for initialization
    28.     void Start () {
    29.  
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update () {
    34.         if (Input.touchCount > 0) {
    35.             print(button1.transform.position);
    36.             touch = Input.GetTouch (0);
    37.             if (buttonTouched (button1, touch)) {
    38.                 //If button was touched...
    39.                 switch (touch.phase) {
    40.  
    41.                 case(TouchPhase.Began): //Case where button was touched once
    42.                     button2.enabled = !button2.enabled;
    43.  
    44.                     break;
    45.                 case(TouchPhase.Moved): //Case where you're moving the finger
    46.  
    47.                     button1.transform.position = touchPosition;
    48.                     break;  
    49.  
    50.                 }
    51.  
    52.             }
    53.  
    54.         }
    55.            
    56.     }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Do you want to try this? I wonder was your "0.6" adjustment so you could keep dragging?
    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. /** Script goes on what was 'button1'. Added a box collider2D to button1.
    5.  * Added reference for the camera and button2 in the inspector.
    6.  * **/
    7. public class Test3 : MonoBehaviour, IPointerDownHandler, IDragHandler
    8. {
    9.     public SpriteRenderer button2;
    10.     [SerializeField]
    11.     Camera mainCam;
    12.     public void OnDrag(PointerEventData eventData)
    13.     {
    14.         Vector3 wp = eventData.position;
    15.         wp.z = Mathf.Abs(mainCam.transform.position.z - transform.position.z);
    16.         wp = mainCam.ScreenToWorldPoint(wp);
    17.         transform.position = wp;
    18.     }
    19.  
    20.     public void OnPointerDown(PointerEventData eventData)
    21.     {
    22.         button2.enabled = !button2.enabled;
    23.     }
    24. }
    This script will accurately drag the button1 around. It will toggle button2 enabled when button1 is clicked.
    You didn't have any 'drop' functionality.
     
    victorML likes this.
  3. victorML

    victorML

    Joined:
    Jul 5, 2017
    Posts:
    38
    Hi, first of all thanks to your answere.
    The 0.6 adjustment in my script was due to make bigger the zone where touch.began was detected, otherwise the touch is very hard to detect to me because it's refering to a very exact point, for example (3.8,-3.5), with my 0.6 adjustment you can touch between (4,4,-2.9)-(3,2,-4,1).

    I tried your Script, my button1 has now a rigidbody2D and a box Collider 2D, camera and button2 are passed by instructor but I'm not getting any result, not drag nor !button2.enable.

    I'm supposed to add an void Start and void Update method on your script or it was supposed to work without this ?

    Additionally I got this error:

    UnityException: get_main is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'test3'.
    See "Script Serialization" page in the Unity Manual for further details.
    test3..ctor () (at Assets/test3.cs:13)
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    The only thing that I forgot to mention was that you need a physics 2d raycaster on your camera for this to work. Sorry about forgetting that.
    Note that this script must be on the Button1 game object.

    What's 'get_main' ? Is that something you have?

    Edit: If you get the script working, but still think the area of touch is too small for some reason, you can expand the collider and that should make the area larger... if that's acceptable*. :)
     
    Last edited: Apr 21, 2018