Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class DraggableRigidbody : MonoBehaviour
- {
- bool dragging = false;
- public bool makeKinematic = true;
- Plane movePlane;
- void OnMouseDown ()
- {
- if (makeKinematic)
- rigidbody.isKinematic = true;
- dragging = true;
- movePlane = new Plane(-Camera.main.transform.forward,transform.position);
- }
- void Update ()
- {
- if (!dragging || !this.enabled)
- return;
- if (Input.GetMouseButtonUp(0))
- {
- dragging = false;
- if (makeKinematic)
- rigidbody.isKinematic = false;
- }
- Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
- float hitDist;
- if (movePlane.Raycast(camRay,out hitDist))
- {
- Vector3 newPos = camRay.GetPoint(hitDist);
- rigidbody.MovePosition(Vector3.Lerp(transform.position, newPos,0.5f));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment