Advertisement
Sinuousity

DraggableRigidbody.cs Lift

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