Dragged object not under finger on touch.
Sorry if this question has already been ask, but i can't find it.
I'm working on a mobile app for a tabletop game i play with my friends, I have to move gameobjects around a playmat.
I've tried all the solutions i could find on the web, but everyone of them was working fine in the editor, but not on my android tablet. So I'm using the eventTrigger.drag and it works a little bit, my new proble is that the gameobject i touch is following my finger but he is not under it, he is offset behind/bellow.
using UnityEngine;
using UnityEngine.EventSystems;
public class OnDragExample : MonoBehaviour
{
void OnEnable()
{
//Fetch the Event Trigger component from your GameObject
EventTrigger trigger = GetComponent<EventTrigger>();
//Create a new entry for the Event Trigger
EventTrigger.Entry entry = new EventTrigger.Entry();
//Add a Drag type event to the Event Trigger
entry.eventID = EventTriggerType.Drag;
//call the OnDragDelegate function when the Event System detects dragging
entry.callback.AddListener((data) => { OnDragDelegate((PointerEventData)data); });
//Add the trigger entry
trigger.triggers.Add(entry);
}
public void OnDragDelegate(PointerEventData data)
{
Rigidbody2D rb = GetComponent<Rigidbody2D>();
//Create a ray going from the camera through the mouse position
Ray ray = Camera.main.ScreenPointToRay(data.position);
//Calculate the distance between the Camera and the GameObject, and go this distance along the ray
Vector2 rayPoint = ray.GetPoint(Vector2.Distance(rb.position, Camera.main.transform.position));
//Move the GameObject when you drag it
transform.position = rayPoint;
}
}
I`ve added the Rigidbody2D to make sure the gameobjects would act like real token and allow physics to work.
Your answer
Follow this Question
Related Questions
Drag and Drop Touch Collision 1 Answer
What to code if the laboratory equipments are correct and then proceeds to the next scene? 0 Answers
When The Screen Is Touched Jump 0 Answers
Android Touch Double Jump - Not working 0 Answers
Unity Input Touch 0 Answers