Question by
FabioPinhaoMello · Apr 07, 2018 at 05:16 PM ·
transform.positioneventsystemdrag-and-drop
transform.postion behaving strangely when dragging an object
I followed a tutorial in the internet and it didn't work for me. What is wrong with my code? The code is as follows:
public class DragHandler : MonoBehaviour, IBeginDragHandler, IDragHandler,IEndDragHandler { public static GameObject objectBeingDragged; Vector3 startPosition;
void IBeginDragHandler.OnBeginDrag (PointerEventData eventData)
{
objectBeingDragged = gameObject;
startPosition = transform.position;
}
public void OnDrag (PointerEventData eventData)
{
// First Attempt (the object is behaves strangely. Example: when I drag the object 1 inch to one direction, it moves much more than 1 inch in that direction)
transform.position = Input.mousePosition;
// Second Attempt (Nothing happens when I use the line below)
transform.position.Set(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
}
void IEndDragHandler.OnEndDrag (PointerEventData eventData)
{
objectBeingDragged = null;
transform.position = startPosition;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612160258im_/https://answers.unity.com/themes/thub/images/avi.jpg)