I am new to Unity trying to create a simple drag function for a Uni assignment but I am getting stuck.
I want to make an object that can be dragged when the mouse is held down, without moving through the y axis, only the x and z axes (as if it can be dragged across a floor but not lifted). The trick is that the camera is at a 30 degree angle, so all three axes are used.
Could anybody help me do this?
I could show my own progress so far, but as I said, I am new to Unity and think my own coding would be more of a joke than anything, and probably the wrong way to go about it. So far I have the mouse position being read when the mouse is held down, and initially intended to do transform.position = Vector3 (blah), before realising it is probably impossible to read the mouse position and translate it into three dimensions 
Any help would be appreciated
When the object is clicked, you must create a horizontal plane passing through the object position and set the boolean flag drag to signal it’s being dragged. In Update, while drag is true create a ray from the mouse pointer with Camera.main.ScreenPointToRay(Input.mousePosition), and use Plane.Raycast and Ray.GetPoint to find the intersection of the ray with the plane created, then set the object position to this point - this way, the object only moves in a horizontal plane, never changing the Y coordinate. When the mouse button is released, set drag to false to stop dragging.
NOTE1: Attach this script to the “drageable” object.
NOTE2: Mouse button up is checked inside Update instead of using OnMouseUp - this event doesn’t occur if the mouse button is released outside this object’s collider.
var plane: Plane;
var drag: boolean = false;
function OnMouseDown(){
// create the horizontal plane
plane = new Plane(Vector.up, transform.position);
drag = true; // start dragging
}
function Update(){
// if mouse button released, end dragging
if (Input.MouseButtonUp(0)) drag = false;
if (drag){ // while drag is true, object follow the mouse pointer
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var distance: float;
if (plane.Raycast(ray, distance)){
// transform the distance returned in a 3D point
transform.position = ray.GetPoint(distance);
}
}
}
Tried Aldo Naletto’s script out. Didn’t work for me at all until I realized that the OnMouseDown function only set drag to true because it was acting separate of the Update function. It would set Drag to true, but that didn’t mean anything because the script wasn’t looking at the variable properly. Here’s my rewrite:
var plane: Plane;
var drag: boolean = false;
var position : Vector3;
function OnMouseDown(){
// create the horizontal plane
plane = new Plane(Vector2.up, transform.position);
drag = true; // start dragging
}
function OnMouseUp(){
drag = false;}
function Update(){
position = transform.localPosition;
// if mouse button released, end dragging
if (drag){ // while drag is true, object follow the mouse pointer
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var distance: float;
if (plane.Raycast(ray, distance)){
// transform the distance returned in a 3D point
transform.position = ray.GetPoint(distance);
}
}
}
while you have the object selected in your heirarchy, it will return the objects localPosition for you to view. Just a little something I like having on my own structures.