Dragging object with a mouse from one point to another point, Have a big problem

Hi I have a massive problem with a piece of code that has the ability for the user to move an object by dragging it from a starting point to an end point.

The problem is that the code I have got has and does worked and I have implemented it into a different scene and done all the changes that are needed for it to work with the objects, but it won't work properly as the dragging has become very glitchy when trying to drag a object from the start point to the end point and back again. Plus the code is as it was when it previously worked.

If anyone who has a solution to this I would be very grateful for any help, as I have a deadline for Wednesday 24th November '10

Here is the code:

var useX : boolean;
var useY : boolean;
var useZ : boolean;
private var mouseOn : boolean = false;

var start : Transform;
var end : Transform;

function Update () {

    var mouseX : float = Input.GetAxis("Mouse X");

    if(useX && mouseOn){
        transform.position.x -= mouseX;
        transform.position.x = Mathf.Clamp(transform.position.x, start.position.x, end.position.x);

    }else if(useY && mouseOn){
        transform.position.y -= mouseX;
        transform.position.y = Mathf.Clamp(transform.position.y, start.position.y, end.position.y);

    }else if(useZ && mouseOn){
        transform.position.z -= mouseX;
        transform.position.z = Mathf.Clamp(transform.position.z,   start.position.z, end.position.z);       
    }

}

function OnMouseDown(){

    mouseOn = true; 

}

function OnMouseUp(){

    mouseOn=false;  

}

//Written by Will Goldstone 17/11/2010    

You say glitchy, but you don't actually qualify what is being done incorrectly.

Here's a shorter version that does the same thing as your script:

var useX : boolean = true;
var useY : boolean = true;
var useZ : boolean = true;

var start : Transform;
var end : Transform;

function OnMouseDrag() {
    if(!start || !end) return;
    var mouseX : float = Input.GetAxis("Mouse X");
    if(useX)
        transform.position.x = Mathf.Clamp(transform.position.x+mouseX,
                                           start.position.x, end.position.x);
    else if(useY)
        transform.position.y = Mathf.Clamp(transform.position.y+mouseX, 
                                           start.position.y, end.position.y);
    else if(useZ)
        transform.position.z = Mathf.Clamp(transform.position.z+mouseX,
                                           start.position.z, end.position.z);
}

Note that your script as written will:

  • Only move on world axes. If you move your camera around, the direction will not be the same relative to the camera.
  • Move by the number of pixels the mouse moved world units. This will not mirror your mouse position. To get your movement, you would either need to convert the direction and amount into world units. or convert the object's position into pixels and back.
  • Only move on one axis at a time.