0

I have problem with UI image on canvas for android in unity, the object does not follow the exact touch coordinates it moves slightly, but not enough to actually be functional

I tried everything, including creating a new project that apparently works as intended but without image from UI only by move sprite to the scene

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DragScript : MonoBehaviour{

//offset dotkniecia objektu
float deltaX, deltaY;

//dostep do komponentu rigibody
Rigidbody2D rb;

//bool do dostepu poruszania pilki
bool moveAllowed = false;

void Start()
{
    rb = GetComponent<Rigidbody2D>();
}

void Update(){

    /// <summary>
    /// inicjowanie zdarzenia dotykowego
    /// jezeli ma miejsce zdarzenie dotykowe czyli jest > 0
    /// </summary>
    if (Input.touchCount > 0)
    {
        Touch touch = Input.GetTouch(0);

        // uzyskuje pozycje dotykowe
        Vector2 touchPos = Camera.main.ScreenToWorldPoint(touch.position);

        //przetwarzanie faz dotykowych
        switch (touch.phase)
        {
            //jezeli dotykasz ekranu
            case TouchPhase.Began:
                Debug.Log("DOTKNALES PALCEM WTFF");
                // jezeli dotkniesz elementu
                if (GetComponent<BoxCollider2D>() == Physics2D.OverlapPoint(touchPos)){

                    Debug.Log("DZIALA IF");
                    /// <summary>
                    /// pobierz offset pomiedzy pozycjami ktore dotkniesz
                    /// i srodek objektu gry
                    /// </summary>
                    deltaX = touchPos.x - transform.position.x;
                    deltaY = touchPos.y - transform.position.y;

                    /// <summary>
                    /// jezeli dotyk zaczyna sie w ciagu colidera obiektu
                    /// wtedy może się poruszać
                    /// </summary>
                    moveAllowed = true;

                    /// <summary>
                    /// ogranicza troche rigibody aby sie poruszał
                    /// bardziej płynnie i poprawnie
                    /// </summary>
                    //rb.mass = 1;
                    rb.freezeRotation = true;
                    //rb.velocity = new Vector2(0, 0);
                    rb.gravityScale = 0;
                    GetComponent<PolygonCollider2D>().sharedMaterial = null;
                }
                break;
            // Gdy przesuwasz palcem po ekranie
            case TouchPhase.Moved:
                // jezeli przesuwam obiekt i moveallowed = true
                if (GetComponent<BoxCollider2D>() == Physics2D.OverlapPoint(touchPos) && moveAllowed)
                {   
                    rb.MovePosition(new Vector2(touchPos.x - deltaX, touchPos.y - deltaY));
                }
                break;

            // puszczasz palce
            case TouchPhase.Ended:
                Debug.Log("KONIEC");
                moveAllowed = false;
                //rb.mass = 17;
                rb.freezeRotation = false;
                rb.gravityScale = 45;
                break;
        }
    }
}

}

it must move an UI image with fingers no error messages it just doesn't move correctly

  • You could try and rather use the [`IDragHandler` interfaces](https://docs.unity3d.com/2018.1/Documentation/ScriptReference/EventSystems.IDragHandler.html) – derHugo Sep 22 '19 at 15:17
  • i dont know how use it for touch – Filip Gorczyca Sep 22 '19 at 16:35
  • You just need an `EventSystem` in your scene (should have been added to your Scene together with the Canvas by default) .. then `IDragHandler` etc react to both mouse and touch inputs – derHugo Sep 22 '19 at 16:36
  • i need to implements voids from idraghandler to my code? or just change code with me? – Filip Gorczyca Sep 22 '19 at 16:46
  • in the link above there is an example of usage for all of them ... maybe try the example script for testing before implementing it in your script .. just to see if it works as expected first – derHugo Sep 22 '19 at 16:57
  • yea i just check whole script and it doesnt work,it doesnt work at all nothing :/ – Filip Gorczyca Sep 22 '19 at 17:17

1 Answers1

0

The script is attached to the wrong object in the Hierarchy. You need to attach it to the Canvas and not the image. The script uses the transform of the object it is placed on. Thus, if you place it on a child, the parent object will not follow.

To resolve this, try adding the script to the canvas.

kicksent
  • 111
  • 1
  • 3
  • 1
    MissingComponentException: There is no 'Rigidbody2D' attached to the "Canvas" game object, but a script is trying to access it. You probably need to add a Rigidbody2D to the game object "Canvas". Or your script needs to check if the component is attached before using it. DragScript.Update () (at Assets/Scripts/DragScript.cs:64) i attached it to canva i have error like that – Filip Gorczyca Sep 22 '19 at 16:26