Hi everyone, im trying to make a script that when i click on a mouse with the mouse, the user will be able do drag it around while he holds the button.

Here is my piece of code:

function Update () {
    if (Input.GetMouseButton(0)) {
        var hit : RaycastHit;
        var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);  
        if (Physics.Raycast(ray, hit)) {
            mousePos = Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
            hit.transform.position = Camera.main.ScreenToWorldPoint(mousePos);
        }
    }
}

'm having some problems, fist problem:

I'm setting a fixed Z to the mouse position and i want the object to stands in its original z axis. Any insights about that? I've tried some other things but it didnt work properly.

Second problem: I want the first click to doesnt move the object, the first just to select and after the click you move the mouse and then the object comes along. This way, any click the objects moves a little.

Thank you,

First problem: I'm setting a fixed Z to the mouse position and i want the object to stands in its original z axis. Any insights about that? I've tried some other things but it didnt work properly.

// ... (snip) ...
if (Physics.Raycast(ray, hit))           
    hit.transform.position = ray.origin + ray.direction * 10;
// ... (snip) ...

Second problem: I want the first click to doesnt move the object, the first just to select and after the click you move the mouse and then the object comes along. This way, any click the objects moves a little.

I didn't know if you want to drag while holding down the mouse button or if you want to toggle dragging with a mouse click. I provide a useToggleDrag to switch between the two styles.

var grabbed : Transform;
var grabDistance : float = 10.0f;

var useToggleDrag : boolean; // Didn't know which style you prefer. 

function Update () {
    if (useToggleDrag)
        UpdateToggleDrag();
    else
        UpdateHoldDrag();
}

// Toggles drag with mouse click
function UpdateToggleDrag () {
    if (Input.GetMouseButtonDown(0)) 
        Grab();
    else if (grabbed)
        Drag();
}

// Drags when user holds down button
function UpdateHoldDrag () {
    if (Input.GetMouseButton(0)) 
        if (grabbed)
            Drag();
        else 
            Grab();
    else
        grabbed = null;
}

function Grab() {
    if (grabbed) 
       grabbed = null;
    else {
        var hit : RaycastHit;
        var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, hit))          
            grabbed = hit.transform;
    }
}

function Drag() {
    var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    var position : Vector3 = transform.position + transform.forward * grabDistance;
    var plane : Plane = new Plane(-transform.forward, position);
    var distance : float;
    if (plane.Raycast(ray, distance)) {
        grabbed.position = ray.origin + ray.direction * distance;
        grabbed.rotation = transform.rotation;
    }
}

// This drag wasn't like the OP wanted.
function OldDrag() {
    var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    grabbed.position = ray.origin + ray.direction * grabDistance;
}

I've discovered something that keeps the original Z on the object.

I've noticed that on the mousePos.z i have tu put the difference between my camera.z and my object.z and it works

Like this:

`
var desiredZ: float = Mathf.Abs(Utils.Round((Camera.main.transform.position.z - hit.transform.position.z),2));
mousePos = Vector3(Input.mousePosition.x, Input.mousePosition.y, desiredZ);
` hit.transform.position = Camera.main.ScreenToWorldPoint(mousePos); It works, but does anyone have a more ellegant solution?

One more thing, everytime i click, it already moves a little bit the object, i just want it to moves after the click. Any ideas?

if (Physics.Raycast(ray, hit)) {
            newPosition = Vector3(Input.mousePosition.x, Input.mousePosition.y, hit.transform.position.z);
            hit.transform.position = Camera.main.ScreenToWorldPoint(newPosition);
}

The little “jump” that you keep experiencing is due to the grabDistance. what you really want to do is immediately do another raycast to simulate what the next frame would see with the Raycast layer of the grabbed object turned off. Then you can use the difference from the grabbed object’s transform and that immediate second raycast as your offset for dragging.

The following is “smooth as silk” and also allows you to grab hierarchical objects – well, single-layered ones at least :slight_smile:

var grabbed : Transform;
var grabDistance : float = 10.0f;
var grabLayerMask : int;
var grabOffset : Vector3; //delta between transform transform position and hit point
var useToggleDrag : boolean; // Didn't know which style you prefer. 

function Update () {
    if (useToggleDrag){
        UpdateToggleDrag();
    } else {
        UpdateHoldDrag();
    }
}

// Toggles drag with mouse click
function UpdateToggleDrag () {
    if (Input.GetMouseButtonDown(0)){ 
        Grab();
    } else {
    	if (grabbed) {
        	Drag();
        }
    }
}

// Drags when user holds down button
function UpdateHoldDrag () {
    if (Input.GetMouseButton(0)) {
        if (grabbed){
            Drag();
        } else { 
            Grab();
        }
    } else {
    	if(grabbed){
    		//restore the original layermask
    		grabbed.gameObject.layer = grabLayerMask;
    	}
        grabbed = null;
    }
}

function Grab() {
    if (grabbed){ 
       grabbed = null;
    } else {
        var hit : RaycastHit;
        var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, hit)){          
            grabbed = hit.transform;
            if(grabbed.parent){
            	grabbed = grabbed.parent.transform;
            }
            //set the object to ignore raycasts
            grabLayerMask = grabbed.gameObject.layer;
            grabbed.gameObject.layer = 2;
            //now immediately do another raycast to calculate the offset
            if (Physics.Raycast(ray, hit)){
            	grabOffset = grabbed.position - hit.point;
            } else {
            	//important - clear the gab if there is nothing
            	//behind the object to drag against
            	grabbed = null;
            }
        }
    }
}

function Drag() {    
	var hit : RaycastHit;
    var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, hit)){      
    	grabbed.position = hit.point + grabOffset;
    }

},

hi gilson,i have same problem .do you solve it?could you plz show your complete code?

Another approach is to think of the problem as follows:

A ray from the camera passes through the projection plane and hits an object at some point on the object. The ratio of the distances along ray from the camera to the projection plane and from the camera to the point on the object is constant if the object moves parallel to the projection plane.

If we store the ratio when the object is clicked, we can cast a ray on each update, calculate the distance to the projection plane and multiply that by the ratio to get the new position of the point on the object at it’s desired location.

Additionally, you will need to get the vector from point on the object to the origin of the object’s transform.


There may be ways to write this simpler but here is the basic parts.

	private float ratioObjectToScreen;
	private Vector3 handleToObjectOrigin;

	void OnMouseDown () {
		Ray rayFromCameraThroughScreenPoint = Camera.main.ScreenPointToRay (Input.mousePosition);
		RaycastHit handle;
		Physics.Raycast (rayFromCameraThroughScreenPoint, out handle);
		Vector3 cameraToHandle = handle.point - Camera.main.transform.position;
		float distanceCameraToHandle = cameraToHandle.magnitude;
		Vector3 cameraToProjectionPlanePoint = Camera.main.ScreenToWorldPoint (Input.mousePosition + new Vector3 (0, 0, Camera.main.nearClipPlane)) - Camera.main.transform.position;
		float distanceCameraToProjectionPlanePoint = cameraToProjectionPlanePoint.magnitude;
		ratioObjectToScreen = distanceCameraToHandle / distanceCameraToProjectionPlanePoint;
		handleToObjectOrigin = transform.root.position - handle.point;
	}

	void OnMouseDrag () {
		Vector3 cameraToProjectionPlanePoint = Camera.main.ScreenToWorldPoint (Input.mousePosition + new Vector3 (0, 0, Camera.main.nearClipPlane)) - Camera.main.transform.position;
		float distanceCameraToProjectionPlanePoint = cameraToProjectionPlanePoint.magnitude;
		float distanceCameraToHandle = distanceCameraToProjectionPlanePoint * ratioObjectToScreen;
		Ray rayFromCameraThroughScreenPoint = new Ray (Camera.main.transform.position, cameraToProjectionPlanePoint);
		Vector3 newHandlePosition = rayFromCameraThroughScreenPoint.GetPoint (distanceCameraToHandle);
		transform.root.position = newHandlePosition + handleToObjectOrigin;
	}