//used for following the mouse after being instantiated void Update() { DragObject targetScript = target.GetComponent <DragObject>(); //make it follow the mouse if spawned by a prefab spawner if (followingMouse) { targetScript.dragged = true; Vector3 temp = Input.mousePosition; temp.z = this.transform.position.z + 10; // Set this to be the distance you want the object to be placed in front of the camera. this.transform.position = Camera.main.ScreenToWorldPoint(temp); if (!Input.GetMouseButton(0)) { targetScript.dragged = false; followingMouse = false; this.OnMouseUp(); } } //highlight if it's in a close snap zone and being dragged if (outline == null) { return; } if (Input.GetMouseButton(0) && targetScript != null && targetScript.dragged && targetScript.getNearestSnappable() != null) { targetScript.SetOutline(true); } else if (targetScript != null) { targetScript.SetOutline(false); } }
//Snap object to available snappable objects void OnMouseUp() { if (locked) { return; } targetScript = target.GetComponent <DragObject>(); if (targetScript.snappedObject != null) { targetScript.snappedObject.GetComponent <SnapTo>().holdingObject = false; } targetScript.dragged = false; spr = target.GetComponent <SpriteRenderer>(); GameObject nearestObject = targetScript.getNearestSnappable(); Sounds.SnapSound(); //on a failed snap if (nearestObject == null) { target.transform.parent = null; targetScript.snappedObject = null; if (targetScript.destroyOnFail && targetScript.returnOnFail && snappedOnce) { targetScript.SnapTo(targetScript.lastSnappedTo); } else if (targetScript.destroyOnFail) { Destroy(target); } else if (targetScript.returnOnFail) { targetScript.SnapTo(targetScript.lastSnappedTo); } return; } targetScript.SnapTo(nearestObject); }