Answer the question
In order to leave comments, you need to log in
IDragHandler Flies to Narnia. Why?
Good day. I am using Unity3D.
I created a 2D Scene where I want to hold the object and move it by pressing the left mouse button / touching the finger on the phone.
Created an object (image). I throw a script like this on it:
using UnityEngine;
using UnityEngine.EventSystems;
public class EventHandle : MonoBehaviour, IDragHandler
{
public void OnDrag(PointerEventData eventData)
{
this.transform.position = eventData.position;
}
}
Answer the question
In order to leave comments, you need to log in
The fact is that the touch occurs in a different coordinate system.
To convert the touch to "world" coordinates, you need to use the Camera.main.ScreenToWorldPoint() method
public void OnDrag(PointerEventData eventData)
{
transform.position = Camera.main.ScreenToWorldPoint(eventData.position);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question