Drag and merge object

Hi.
I would like to create a script that would move objects with the mouse. At the same time, I would like the objects to be able to merge with each other.
but I don’t want them to be one object.
I want objects to detect each other.

Sorry for my bad English

Do you mean, when dragging one object,
it would collide with other objects,
and then those objects would then stick to it? (follow the object also)

That’s exactly what I mean.

so do you have the basic dragging already working? can you show the script?
are those 3D or 2D physics objects, or just transforms without physics?

I’ve created a drag and drop script and a rotation.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class dragObject : MonoBehaviour {

private Vector3 mOffset;
private float mZCoord;
float doubleClickStart;
void OnMouseUp()
{
if ((Time.time - doubleClickStart) < 0.3f)
{
this.OnDoubleClick();
doubleClickStart = -1;
}
else
{
doubleClickStart = Time.time;
}
}

void OnDoubleClick(){
gameObject.transform.Rotate( new Vector3 (0,0,90));
}
void OnMouseDown()

{

mZCoord = Camera.main.WorldToScreenPoint(

gameObject.transform.position).z;

// Store offset = gameobject world pos - mouse world pos

mOffset = gameObject.transform.position - GetMouseAsWorldPoint();

}

private Vector3 GetMouseAsWorldPoint()

{

// Pixel coordinates of mouse (x,y)

Vector3 mousePoint = Input.mousePosition;

// z coordinate of game object on screen

mousePoint.z = mZCoord;

// Convert it to world points

return Camera.main.ScreenToWorldPoint(mousePoint);

}

void OnMouseDrag()

{

transform.position = GetMouseAsWorldPoint() + mOffset;

}

}

The project is in 3d.

i’d try checking if can get collision events (this can fail, if move object fast by setting transform.position, it breaks physics)

if it collides, then parent that collided object as a child of the currently dragged object,
then it would automatically follow it. (and then unparent on release)

I would rather create a common parent, then parent all object to that common parent. Ie on collision, check if any object as a sticky parent, if not, create one, if yes parent to it, if both have delete one and parent to the other. It will simplified management of many collider and query, and potential bug down the line if you aong chain of children.

1 Like

I thought the work area would be a mesh on which objects would move in steps of constant value.