Skip to content

Instantly share code, notes, and snippets.

@rstecca
Last active September 29, 2021 11:32
Show Gist options
  • Save rstecca/6e583504e1e0ba7f2192d64d3036daae to your computer and use it in GitHub Desktop.
Save rstecca/6e583504e1e0ba7f2192d64d3036daae to your computer and use it in GitHub Desktop.
How to make a 3D object draggable in Unity3D
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
/*
MAKE A 3D OBJECT DRAGGABLE
Riccardo Stecca
http://www.riccardostecca.net
https://github.com/rstecca
And check out UNotes here: https://github.com/rstecca/UNotes
!!! Remember that in order to get this working you need a PhysicsRaycaster on the current camera. !!!
*/
[RequireComponent(typeof(Collider))]
public class MakeA3DObjectDraggable : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler {
Camera m_cam;
void Start () {
if (Camera.main.GetComponent<PhysicsRaycaster>() == null)
Debug.LogError("Camera doesn't have a physics raycaster.");
m_cam = Camera.main;
}
public void OnDrag(PointerEventData eventData)
{
Ray R = m_cam.ScreenPointToRay(Input.mousePosition); // Get the ray from mouse position
Vector3 PO = transform.position; // Take current position of this draggable object as Plane's Origin
Vector3 PN = -m_cam.transform.forward; // Take current negative camera's forward as Plane's Normal
float t = Vector3.Dot(PO - R.origin, PN) / Vector3.Dot(R.direction, PN); // plane vs. line intersection in algebric form. It find t as distance from the camera of the new point in the ray's direction.
Vector3 P = R.origin + R.direction * t; // Find the new point.
transform.position = P;
}
public void OnBeginDrag(PointerEventData eventData)
{
// Do stuff when dragging begins. For example suspend camera interaction.
}
public void OnEndDrag(PointerEventData eventData)
{
// Do stuff when draggin ends. For example restore camera interaction.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment