Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class DraggableRigidbodyController : MonoBehaviour
- {
- public List<string> draggableTags;
- [Range(0.5f,0.99f)]
- public float smoothness = 0.9f;
- public float lift = 2f;
- Rigidbody curObj = null;
- public bool makeKinematic = true;
- Plane movePlane;
- void TryClick ()
- {
- Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if (Physics.Raycast(camRay,out hit))
- {
- if (hit.rigidbody == null)
- return;
- if (draggableTags.Contains(hit.rigidbody.tag))
- {
- curObj = hit.rigidbody;
- if (makeKinematic)
- curObj.isKinematic = true;
- movePlane = new Plane(Vector3.up,curObj.transform.position);
- }
- }
- }
- void FreeObject ()
- {
- if (makeKinematic)
- curObj.isKinematic = false;
- curObj = null;
- }
- void Update ()
- {
- if (Input.GetMouseButtonDown(0) && curObj == null)
- {
- TryClick();
- }
- if (Input.GetMouseButtonUp(0) && curObj != null)
- {
- FreeObject ();
- }
- if (curObj == null)
- return;
- Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
- float hitDist;
- if (movePlane.Raycast(camRay,out hitDist))
- {
- Vector3 newPos = camRay.GetPoint(hitDist)+Vector3.up*lift;
- curObj.MovePosition(Vector3.Lerp(curObj.transform.position, newPos,1f - smoothness));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment