Search Unity

Dragging on Touch

Discussion in 'Scripting' started by KingKong320, Aug 9, 2018.

  1. KingKong320

    KingKong320

    Joined:
    Mar 2, 2018
    Posts:
    21
    i am dragging an object on touch began and touch move,but when i touch on object it get position of object instantly?


    case TouchPhase.Began:

    userDragCounter += 1;

    indexVal = -1;
    Vector2 worldPoint = Camera.main.ScreenToWorldPoint (touch.position);
    hit = Physics2D.Raycast (worldPoint, Vector2.zero);

    rb = hit.transform.GetComponent<Rigidbody2D> ();
    rb.isKinematic = true;

    if (PlayerPrefs.GetInt ("Turns") > 0) {



    //if you touch the car
    if (GetComponent<Collider2D> () == Physics2D.OverlapPoint (touchPos)) {

    //get the offset between position you touch
    deltax = touchPos.x - transform.position.x;
    deltay = touchPos.y - transform.position.y;

    //if touch began within the car collider
    //then it is allowed to move

    moveAllowed = true;

    //restriction some rigidbody properties
    rb.freezeRotation = true;
    rb.velocity = new Vector2 (0, 0);
    GetComponent<BoxCollider2D> ().sharedMaterial = null;
    }
    }
    break;

    //you move your finger
    case TouchPhase.Moved:

    if (PlayerPrefs.GetInt ("Turns") > 0) {

    //if you touches the car and move is allowed
    // if (GetComponent<Collider2D> () == Physics2D.OverlapPoint (touchPos) && moveAllowed) {
    dragging = true;
    rb.isKinematic = false;


    if (hit.transform.tag == "hblock" || hit.transform.tag == "PlayerTile" || hit.transform.tag == "hblock3" || hit.transform.tag == "PlayerTilex6" || hit.transform.tag == "hblock3x6" || hit.transform.tag == "hblockx6") {

    rb.MovePosition (new Vector2 (touchPos.x - (deltax), transform.position.y));

    print ("horizontallll");

    } else if (hit.transform.tag == "vblock" || hit.transform.tag == "vblock3" || hit.transform.tag == "vblock3x6" || hit.transform.tag == "vblockx6") {
    rb.MovePosition (new Vector2 (transform.position.x, touchPos.y - (deltay)));

    print ("verticalllllll");
    }
    // }
    }
    break;
     
  2. KingKong320

    KingKong320

    Joined:
    Mar 2, 2018
    Posts:
    21
    i want to keep it smooth either i start touch or moving?
    Please help me
     
  3. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    In the first frame of a touch, as I have seen it, both the TouchPhase.Began and the TouchPhase.Moved are called. This would be so that you can uniquely handle the beginning of the touch (for example, you might want to record the time, touch location or details of some other related objects). This logic can then be distinct from the Move phase without the need to have an "if (this_is_the_first_frame_of_the_touch)" in the Move code.

    So you only want to handle your dragging logic in the Moved phase. You can see an example of something like this in Unity's Touch Phase script reference
     
    KingKong320 likes this.
  4. christian_zc

    christian_zc

    Joined:
    Aug 9, 2018
    Posts:
    3
    How would you get those touch phases using Unity 2018.2.?
    Since the update i can get any touch input anymore.
    (Desperately trying to change my whole project to the event system but i have no idea how to read out touch related information anyways...)
     
  5. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Take a look at the link I posted at #3. I am not aware of Unity fundamentally changing things here. If they have then it would have been mentioned in the release notes somewhere. Do you see such an announcement?
     
  6. christian_zc

    christian_zc

    Joined:
    Aug 9, 2018
    Posts:
    3
    Hey,

    https://docs.unity3d.com/ScriptReference/EventSystems.TouchInputModule.html
    Thats the only hint i was able to find so far. The release notes are not mentioning it anywhere... but still, touch.phase and touch.Count etc is not giving me any data using unity2018.2. Version 2018.1 is working perfectly fine.

    Apperently Touch requires now the use of the UI-Eventsystem which i was not able to get running in my project so far.

    Best,
    Christian.
     
  7. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Hi,
    The main Unity Touch Input Module page says to now use the StandaloneInputModule.

    However, they both appear to be listed under the "Event System Reference" node in the documentation. So it is not clear that they have changed any underlying requirements (although based on what you are saying it would appear they maybe have?).

    I am using 2017.2.1f1. If I add an Event System into a new scene, it has a "Standalone Input Module" component by default. Furthermore, if I add a "Touch Input Module" component, it says "deprecated: no need to use this as handled by Standard Input Module".

    So maybe this change from Touch to Standalone Input Module happened a while ago?
     
  8. christian_zc

    christian_zc

    Joined:
    Aug 9, 2018
    Posts:
    3
    Temporary solution:
    I finally solved this super annoying problem.
    edit> Project Settings> Input> Active Input Handling> (was "both") set to: "Input Manager"

    Since my project is a group project and we use a lot of plugins and assets from older projects i could not find the script or setting or whatsoever that was causing that Unity could not get any input via touch. But... setting off the new input system worked for me for now. Now i have to find the bug in my project but maybe that temp solution helps somebody else as well...

    Best,
    Chrsitian.
     
    Doug_B likes this.
  9. KingKong320

    KingKong320

    Joined:
    Mar 2, 2018
    Posts:
    21
    Thankx to all of you but i have achieved it by saving tag and name of game objects.here is the code

    case TouchPhase.Moved:

    if (PlayerPrefs.GetInt ("Turns") > 0) {

    //if you touches the car and move is allowed
    // if (GetComponent<Collider2D> () == Physics2D.OverlapPoint (touchPos) && moveAllowed) {
    if (GameObject.Find (dgName) && GameObject.FindGameObjectWithTag (dgTag) && moveAllowed) {
    dragging = true;
    rb.isKinematic = false;


    if (transform.tag == "hblock" || transform.tag == "PlayerTile" || transform.tag == "hblock3" || transform.tag == "PlayerTilex6" || transform.tag == "hblock3x6" || transform.tag == "hblockx6") {

    rb.MovePosition (new Vector2 (touchPos.x - (deltax), transform.position.y));
    } else if (transform.tag == "vblock" || transform.tag == "vblock3" || transform.tag == "vblock3x6" || transform.tag == "vblockx6") {
    rb.MovePosition (new Vector2 (transform.position.x, touchPos.y - (deltay)));
    }
    }
    }
    break;