I'm trying to create a 2d game and the game objects should follow the mouse on a single axis (the x axis), when the user drags the object.
Here's my code, the problem is, the game object is following the mouse everywhere i drag.
using UnityEngine; using System.Collections; using UnityEngine.EventSystems; public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { public void OnBeginDrag(PointerEventData eventData) { Debug.Log("OnBeginDrag"); } public void OnDrag(PointerEventData eventData) { Debug.Log("OnDrag"); this.transform.position = eventData.position; } public void OnEndDrag(PointerEventData eventData) { Debug.Log("OnEndDrag"); } } 
