Hello People,
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, that the game object flies out of the screen as soon as i try to drag it, instead of following the mouse,
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(BoxCollider))]
public class MovePoint : MonoBehaviour
{
void OnMouseDrag()
{
Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
point.y = gameObject.transform.position.y;
gameObject.transform.Translate(point.x,0,0);
Screen.showCursor = false;
}
void OnMouseUp()
{
Screen.showCursor = true;
}
}
Help would be much appreciated.