Snap to grid after click and drag?

I’m using two scripts from the Unify Community wiki - one is to drag and drop objects, and the other snaps objects to a grid. Is there a way I can connect the variables of these scripts so that when you release the mouse, the object you’ve been dragging snaps to the grid?

DragObject script:

var normalCollisionCount = 1;
var moveLimit = .5;
var collisionMoveFactor = .01;
var addHeightWhenClicked = 0.0;
var freezeRotationOnDrag = true;
var cam : Camera;
private var myRigidbody : Rigidbody;
private var myTransform : Transform;
private var canMove = false;
private var yPos : float;
private var gravitySetting : boolean;
private var freezeRotationSetting : boolean;
private var sqrMoveLimit : float;
private var collisionCount = 0;
private var camTransform : Transform;

function Start () {
    myRigidbody = rigidbody;
    myTransform = transform;
    if (!cam) {
        cam = Camera.main;
    }
    if (!cam) {
        Debug.LogError("Can't find camera tagged MainCamera");
        return;
    }
    camTransform = cam.transform;
    sqrMoveLimit = moveLimit * moveLimit;   // Since we're using sqrMagnitude, which is faster than magnitude
}

function OnMouseDown () {
    canMove = true;
    myTransform.Translate(Vector3.up*addHeightWhenClicked);
    gravitySetting = myRigidbody.useGravity;
    freezeRotationSetting = myRigidbody.freezeRotation;
    myRigidbody.useGravity = false;
    myRigidbody.freezeRotation = freezeRotationOnDrag;
    yPos = myTransform.position.y;
}

function OnMouseUp () {
    canMove = false;
    myRigidbody.useGravity = gravitySetting;
    myRigidbody.freezeRotation = freezeRotationSetting;
    if (!myRigidbody.useGravity) {
        myTransform.position.y = yPos-addHeightWhenClicked;
    }
}

function OnCollisionEnter () {
    collisionCount++;
}

function OnCollisionExit () {
    collisionCount--;
}

function FixedUpdate () {
    if (!canMove) return;
   
    myRigidbody.velocity = Vector3.zero;
    myRigidbody.angularVelocity = Vector3.zero;
    myTransform.position.y = yPos;
    var mousePos = Input.mousePosition;
    var move = cam.ScreenToWorldPoint(Vector3(mousePos.x, mousePos.y, camTransform.position.y - myTransform.position.y)) - myTransform.position;
    move.y = 0.0;
    if (collisionCount > normalCollisionCount) {
        move = move.normalized*collisionMoveFactor;
    }
    else if (move.sqrMagnitude > sqrMoveLimit) {
        move = move.normalized*moveLimit;
    }
   
    myRigidbody.MovePosition(myRigidbody.position + move);
}

@script RequireComponent(Rigidbody)

SnapToGrid script:

@MenuItem ("GameObject/Snap to Grid %g")
static function MenuSnapToGrid()
{
    var transforms : Transform[] = Selection.GetTransforms(SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable);

    var gridx : float = 1.0;
    var gridy : float = 1.0;
    var gridz : float = 1.0;

    for (var transform : Transform in transforms)
    {
        var newPosition : Vector3 = transform.position;
        newPosition.x = Mathf.Round(newPosition.x / gridx) * gridx;
        newPosition.y = Mathf.Round(newPosition.y / gridy) * gridy;
        newPosition.z = Mathf.Round(newPosition.z / gridz) * gridz;
        transform.position = newPosition;
    }
}

This is just a suggestion as I don’t have Unity currently open. I am trying to achieve the same thing but haven’t tried this yet.

What if you make use of your “canMove” boolean. Use that and (eventually) create one more variable (which is triggered as true whenever you press a button – designated for use after you position the object wherever you want --) to create an:

//Check if the variables are false and true

if (!canMove && newBoolean) {

MenuSnapToGrid(); //Run the MenuSnapToGrid function – set it as the last function or whatever

}

Try this and keep me posted as I’m very interested in the subject!

Hope this helps!

Change your ‘OnMouseUp’ function and add the gridx, gridy, and gridz variables to the script.

private var snapToGridX : float = 1.0;
private var snapToGridY : float = 1.0;
private var snapToGridZ : float = 1.0;
    
function OnMouseUp () {
	canMove = false;
	myRigidbody.useGravity = gravitySetting;
	myRigidbody.freezeRotation = freezeRotationSetting;
	if (!myRigidbody.useGravity) {
		myTransform.position.y = yPos-addHeightWhenClicked;
	}
	
	var newPosition : Vector3 = myTransform.position;
	newPosition.x = Mathf.Round(newPosition.x / gridx) * gridx;
	newPosition.y = Mathf.Round(newPosition.y / gridy) * gridy;
	newPosition.z = Mathf.Round(newPosition.z / gridz) * gridz;
	myTransform.position = newPosition;
        
}