Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Pickup : MonoBehaviour {
- public GameObject mainCamera;
- public GameObject carriedObject;
- public bool carrying;
- public float distance;
- public float smooth;
- public Vector3 velocity = Vector3.zero;
- private Ray ray;
- // Use this for initialization
- void Start () {
- }
- // Update is called once per frame
- void FixedUpdate () {
- if (carrying) {
- carry (carriedObject);
- checkDrop();
- } else {
- pickupobject ();
- }
- }
- void carry(GameObject o) {
- o.transform.position = Vector3.SmoothDamp(o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, ref velocity, Time.deltaTime * smooth);
- o.transform.rotation = Quaternion.identity;
- }
- void pickupobject() {
- if(Input.GetKeyDown (KeyCode.E)) {
- RaycastHit hit;
- ray = mainCamera.GetComponent<Camera>().ScreenPointToRay (Input.mousePosition);
- if (Physics.Raycast (ray, out hit)) {
- Pickupable p = hit.collider.GetComponent<Pickupable>();
- if (p!= null && p.enabled) {
- carrying = true;
- carriedObject = p.gameObject;
- // p.GetComponent<Rigidbody>().isKinematic = true;
- p.GetComponent<Rigidbody>().useGravity = false;
- }
- }
- }
- }
- void checkDrop() {
- if (Input.GetKeyDown (KeyCode.E)) {
- dropObject();
- }
- }
- void dropObject() {
- carrying = false;
- carriedObject.GetComponent<Rigidbody>().useGravity = true;
- carriedObject = null;
- }
- }
Advertisement
RAW Paste Data
Copied
Advertisement