drag and merge object
I want to create a script that moves objects with the mouse. When dragging one object, it would collide with other objects, and then those objects would then stick to it (follow the object also).
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;
}
}
Any idea?
Comment