Question by
panoramabeats · Jan 01, 2017 at 04:17 AM ·
physicsdrag and dropdropbox
Looking for Drag and Drop Zone (snapping) scripts for Physics objects, not UI
There are spheres that I want to snap to a specific point or another circular zone.
Does anyone know of any good scripts out there relating to the dragging object and the snapping drop zone?
Right now my spheres are draggable using the OnMouseClickDown style in this script, which also includes behaviors to display a rotating image when dragging. (Works for mobile finger drag as well.)
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class DragBehaviors : MonoBehaviour {
private bool dragging = false;
private float distance;
public GameObject Crosshair;
void OnMouseDown() {
distance = Vector3.Distance(transform.position, Camera.main.transform.position);
dragging = true;
}
void OnMouseUp() {
dragging = false;
}
// Update is called once per frame
void Update () {
//deleted scripting below : command from GetMouseButton affects all similar gameobjects, 'if dragging' only to affect GameObject.
//if (Input.GetMouseButton(0)) {
//Debug.Log ("Pressed left click.");
//Crosshair.SetActive (true);
// }
//else
// {
// Crosshair.SetActive(false);
//}
if (dragging) {
Crosshair.SetActive (true);
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y));
// ** has the -7f part, deleted for testing ** Ray ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -7f));
Vector3 rayPoint = ray.GetPoint(distance);
transform.position = rayPoint;
}
else
{
Crosshair.SetActive(false);
}
}
}
Comment
@panoramabeats Did you manage to write the code? If so do you $$anonymous$$d sharing it?