Advertisement
Guest User

Code

a guest
Jul 29th, 2017
85
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Pickup : MonoBehaviour {
  2. public GameObject mainCamera;
  3. public GameObject carriedObject;
  4. public bool carrying;
  5. public float distance;
  6. public float smooth;
  7. public Vector3 velocity = Vector3.zero;
  8. private Ray ray;
  9.  
  10. // Use this for initialization
  11. void Start () {
  12.  
  13. }
  14.  
  15. // Update is called once per frame
  16. void FixedUpdate () {
  17. if (carrying) {
  18. carry (carriedObject);
  19. checkDrop();
  20. } else {
  21. pickupobject ();
  22.  
  23. }
  24. }
  25. void carry(GameObject o) {
  26.  
  27. o.transform.position = Vector3.SmoothDamp(o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, ref velocity, Time.deltaTime * smooth);
  28. o.transform.rotation = Quaternion.identity;
  29. }
  30.  
  31. void pickupobject() {
  32. if(Input.GetKeyDown (KeyCode.E)) {
  33.  
  34.  
  35.  
  36. RaycastHit hit;
  37. ray = mainCamera.GetComponent<Camera>().ScreenPointToRay (Input.mousePosition);
  38. if (Physics.Raycast (ray, out hit)) {
  39. Pickupable p = hit.collider.GetComponent<Pickupable>();
  40. if (p!= null && p.enabled) {
  41. carrying = true;
  42. carriedObject = p.gameObject;
  43.  
  44. // p.GetComponent<Rigidbody>().isKinematic = true;
  45. p.GetComponent<Rigidbody>().useGravity = false;
  46. }
  47. }
  48.  
  49. }
  50.  
  51. }
  52. void checkDrop() {
  53. if (Input.GetKeyDown (KeyCode.E)) {
  54. dropObject();
  55.  
  56. }
  57.  
  58. }
  59. void dropObject() {
  60.  
  61. carrying = false;
  62. carriedObject.GetComponent<Rigidbody>().useGravity = true;
  63.  
  64. carriedObject = null;
  65.  
  66.  
  67.  
  68. }
  69. }
Advertisement
RAW Paste Data Copied
Advertisement