Sinuousity

DraggableObject.cs

Aug 29th, 2014
1,515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.58 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class DraggableObject : MonoBehaviour
  5. {
  6.     bool dragging = false;
  7.     Plane movePlane;
  8.  
  9.     void OnMouseDown ()
  10.     {
  11.         dragging = true;
  12.         movePlane = new Plane(-Camera.main.transform.forward,transform.position);
  13.     }
  14.  
  15.     void Update ()
  16.     {
  17.         if (!dragging || !this.enabled)
  18.             return;
  19.        
  20.         if (Input.GetMouseButtonUp(0))
  21.             dragging = false;
  22.  
  23.         Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
  24.         float hitDist;
  25.         if (movePlane.Raycast(camRay,out hitDist))
  26.             transform.position = camRay.GetPoint(hitDist);
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment