Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class Pickupable : MonoBehaviour
- {
- public int visibleDistance;
- public Image PickupIcon;
- public Image Crosshair;
- public GameObject IsHoldingLocation;
- public GameObject ObjectHolding;
- public bool IsHolding = false;
- public Camera cam;
- // Start is called before the first frame update
- void Start()
- {
- IsHolding = false;
- }
- // Update is called once per frame
- void Update()
- {
- if (IsHoldingLocation.transform.childCount > 0)
- {
- ObjectHolding = IsHoldingLocation.transform.GetChild(0).gameObject;
- IsHolding = true;
- }
- Ray ray = cam.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit, visibleDistance))
- {
- if (hit.transform.gameObject.layer == 10) //10 = pickupable layer
- {
- PickupIcon.enabled = true;
- Crosshair.enabled = false;
- }
- }
- else
- {
- PickupIcon.enabled = false;
- Crosshair.enabled = true;
- }
- if (Input.GetKey(KeyCode.E))
- if (ObjectHolding == null)
- {
- if (Physics.Raycast(ray, out hit, visibleDistance))
- {
- if (hit.transform.gameObject.layer == 10) //10 = pickupable layer
- {
- hit.transform.parent = IsHoldingLocation.transform;
- hit.transform.gameObject.GetComponent<Rigidbody>().isKinematic = true;
- }
- {
- }
- if (IsHolding = true)
- if (Input.GetKeyDown(KeyCode.E))
- {
- Drop();
- }
- }
- void Drop()
- {
- IsHolding = false;
- ObjectHolding.transform.gameObject.GetComponent<Rigidbody>().isKinematic = false;
- ObjectHolding.transform.parent = null;
- ObjectHolding = null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement