Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class DraggableRigidbody : MonoBehaviour
- {
- [Range(0.0f,0.99f)]
- public float smoothness = 0.9f;
- bool dragging = false;
- public bool makeKinematic = true;
- Plane movePlane;
- void OnMouseDown ()
- {
- if (makeKinematic)
- rigidbody.isKinematic = true;
- dragging = true;
- movePlane = new Plane(Vector3.up,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)+Vector3.up*2f;
- rigidbody.MovePosition(Vector3.Lerp(transform.position, newPos,1f - smoothness));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement