Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class Pickupable : MonoBehaviour
  7. {
  8.  
  9.     public int visibleDistance;
  10.  
  11.     public Image PickupIcon;
  12.     public Image Crosshair;
  13.     public GameObject IsHoldingLocation;
  14.  
  15.     public GameObject ObjectHolding;
  16.  
  17.     public bool IsHolding = false;
  18.  
  19.     public Camera cam;
  20.     // Start is called before the first frame update
  21.     void Start()
  22.     {
  23.         IsHolding = false;
  24.     }
  25.  
  26.     // Update is called once per frame
  27.     void Update()
  28.     {
  29.         if (IsHoldingLocation.transform.childCount > 0)
  30.         {
  31.             ObjectHolding = IsHoldingLocation.transform.GetChild(0).gameObject;
  32.             IsHolding = true;
  33.         }
  34.  
  35.  
  36.         Ray ray = cam.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
  37.         RaycastHit hit;
  38.  
  39.  
  40.         if (Physics.Raycast(ray, out hit, visibleDistance))
  41.         {
  42.             if (hit.transform.gameObject.layer == 10)  //10 = pickupable layer
  43.             {
  44.                 PickupIcon.enabled = true;
  45.                 Crosshair.enabled = false;
  46.             }
  47.         }
  48.  
  49.         else
  50.         {
  51.             PickupIcon.enabled = false;
  52.             Crosshair.enabled = true;
  53.         }
  54.  
  55.  
  56.  
  57.  
  58.         if (Input.GetKey(KeyCode.E))
  59.  
  60.             if (ObjectHolding == null)
  61.  
  62.             {
  63.  
  64.                 if (Physics.Raycast(ray, out hit, visibleDistance))
  65.  
  66.  
  67.                 {
  68.                     if (hit.transform.gameObject.layer == 10)  //10 = pickupable layer
  69.  
  70.                     {
  71.  
  72.  
  73.                         hit.transform.parent = IsHoldingLocation.transform;
  74.  
  75.                         hit.transform.gameObject.GetComponent<Rigidbody>().isKinematic = true;
  76.  
  77.                     }
  78.  
  79.                     {
  80.  
  81.                     }
  82.  
  83.                     if (IsHolding = true)
  84.  
  85.                         if (Input.GetKeyDown(KeyCode.E))
  86.  
  87.                         {
  88.                             Drop();
  89.                         }
  90.  
  91.  
  92.                 }
  93.                 void Drop()
  94.                 {
  95.                     IsHolding = false;
  96.                     ObjectHolding.transform.gameObject.GetComponent<Rigidbody>().isKinematic = false;
  97.  
  98.                     ObjectHolding.transform.parent = null;
  99.                     ObjectHolding = null;
  100.                 }
  101.             }
  102.  
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement