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 DetectInteractable : UnityEngine.MonoBehaviour
- {
- public Camera cam;
- public float distanceToSee;
- public string objectHit;
- public bool interactableObject = false;
- public Transform parentToSearch;
- public static bool detected = false;
- public Scaling scaling;
- private RaycastHit whatObjectHit;
- private bool clickForDescription = false;
- private int layerMask = 1 << 8;
- private GameObject objectWasHit;
- private void Update()
- {
- if (Input.GetMouseButtonDown(0) && !scaling.scaleUp)
- {
- clickForDescription = true;
- if (whatObjectHit.collider != null)
- ExecuteActions(whatObjectHit.collider.gameObject);
- }
- Debug.DrawRay(cam.transform.position, cam.transform.forward * distanceToSee, Color.magenta);
- if (Physics.Raycast(cam.transform.position, cam.transform.forward, out whatObjectHit, distanceToSee, layerMask))
- {
- objectWasHit = whatObjectHit.collider.gameObject;
- detected = true;
- objectHit = whatObjectHit.collider.gameObject.name;
- interactableObject = true;
- print("Hit ! " + whatObjectHit.collider.gameObject.name);
- }
- else
- {
- detected = false;
- //clickForDescription = false;
- print("Not Hit !");
- }
- }
- private void ExecuteActions(GameObject go)
- {
- var ia = go.GetComponent<ItemAction>();
- ia.ItemMove();
- }
- private void OnGUI()
- {
- if (clickForDescription == true)
- {
- ProcessOnGUI(parentToSearch);
- clickForDescription = false;
- }
- }
- void ProcessOnGUI(Transform parent, int level = 0)
- {
- foreach (Transform child in parent)
- {
- if (child.GetComponent<ItemInformation>() != null)
- {
- ItemInformation iteminformation = child.GetComponent<ItemInformation>();
- if (child.name == objectHit)
- {
- var centeredStyle = GUI.skin.GetStyle("Label");
- centeredStyle.alignment = TextAnchor.UpperCenter;
- GUI.Box(new Rect(
- Screen.width / 2 - 50 + 20 * level, // <== INDENTATION
- Screen.height / 2 - 25, 100, 50),
- iteminformation.description, centeredStyle);
- }
- }
- // Process next deeper level
- ProcessOnGUI(child, level + 1);
- }
- }
- public class ViewableObject : UnityEngine.MonoBehaviour
- {
- public string displayText;
- public bool isInteractable;
- }
- }
- if (Physics.Raycast(cam.transform.position, cam.transform.forward, out whatObjectHit, distanceToSee, layerMask))
- objectWasHit = whatObjectHit.collider.gameObject;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement