Sinuousity

DraggableRigidbody.cs

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