I'm currently making a game in Unity where I want the player to have the ability to drag objects troughout the screen based on the touch position. I created a script in order to do this and from what I know it doens't have any mistakes and should allow me to drag the object that the script is attatched to across the screen. However when I try to execute the code nothing happens. when touched the object remains static and doesn't move at all. I even tried to switch it the Input.GetTouch(0).position to Input.mousePosition to see that the problem lied with my phone but this doenst work either. Does anyone know how I might be able to solve this?
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class MoveBall : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public static GameObject WhiteBall;
Vector3 startPosition;
#region IBeginDragHandler implementation
public void OnBeginDrag(PointerEventData eventData)
{
WhiteBall = gameObject;
startPosition = transform.position;
}
#endregion
#region IDragHandler implementation
public void OnDrag(PointerEventData eventData)
{
transform.position = Input.GetTouch(0).position;
}
#endregion
#region IEndDragHandler implementation
public void OnEndDrag(PointerEventData eventData)
{
WhiteBall = null;
transform.position = startPosition;
}
#endregion
}