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