- Home /
The question is answered, right answer was accepted
eventdata.position issue when using OnDrag
I had a UI button dragging and dropping fine. I then set Canvas scaler to scale with screen size and it has broken the gameobjects position. It appears the position is being scaled up, because when I go to the very bottom left of the screen the object will appear but as I go up and right it scaled to about x position = 120000 and y position = 66000.
I have logged the actually values for eventData.position and it is the same as before, between 0 and 1000 roughly. I can only assume that something is changing the value after my code fires. I have played around with scaling setting but had no luck in fixing the issue.
This is the code I'm using.
public void OnDrag(PointerEventData eventData){
Debug.Log("Dragging");
this.transform.position = eventData.position;
}
Answer by gdowse · Aug 03, 2016 at 01:27 PM
Fixed this myself.
Changed 'postion' to 'localPosition' and changed some of the settings on the canvas scale.
this is exactly my problem, but using: this.transform.localPosition=eventData.position; doesn't correct the problem, the position still gets scaled from about (1,1) to (800,800) when OnDrag.
Here's what worked for me:
public void OnDrag(PointerEventData eventData)
{
transform.position += (Vector3)eventData.delta;
}
YES MAN!!! finally a clear, simple, correct solution. So many answers that completely replace all code with a ton of complicated things. This is the way to do it.