Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class DetectInteractable : UnityEngine.MonoBehaviour
  7. {
  8. public Camera cam;
  9. public float distanceToSee;
  10. public string objectHit;
  11. public bool interactableObject = false;
  12. public Transform parentToSearch;
  13. public static bool detected = false;
  14. public Scaling scaling;
  15.  
  16. private RaycastHit whatObjectHit;
  17. private bool clickForDescription = false;
  18. private int layerMask = 1 << 8;
  19. private GameObject objectWasHit;
  20.  
  21. private void Update()
  22. {
  23. if (Input.GetMouseButtonDown(0) && !scaling.scaleUp)
  24. {
  25. clickForDescription = true;
  26.  
  27. if (whatObjectHit.collider != null)
  28. ExecuteActions(whatObjectHit.collider.gameObject);
  29. }
  30.  
  31. Debug.DrawRay(cam.transform.position, cam.transform.forward * distanceToSee, Color.magenta);
  32. if (Physics.Raycast(cam.transform.position, cam.transform.forward, out whatObjectHit, distanceToSee, layerMask))
  33. {
  34. objectWasHit = whatObjectHit.collider.gameObject;
  35. detected = true;
  36. objectHit = whatObjectHit.collider.gameObject.name;
  37. interactableObject = true;
  38. print("Hit ! " + whatObjectHit.collider.gameObject.name);
  39. }
  40. else
  41. {
  42. detected = false;
  43. //clickForDescription = false;
  44. print("Not Hit !");
  45. }
  46. }
  47.  
  48. private void ExecuteActions(GameObject go)
  49. {
  50. var ia = go.GetComponent<ItemAction>();
  51. ia.ItemMove();
  52. }
  53.  
  54. private void OnGUI()
  55. {
  56. if (clickForDescription == true)
  57. {
  58. ProcessOnGUI(parentToSearch);
  59. clickForDescription = false;
  60. }
  61. }
  62.  
  63. void ProcessOnGUI(Transform parent, int level = 0)
  64. {
  65. foreach (Transform child in parent)
  66. {
  67. if (child.GetComponent<ItemInformation>() != null)
  68. {
  69. ItemInformation iteminformation = child.GetComponent<ItemInformation>();
  70. if (child.name == objectHit)
  71. {
  72. var centeredStyle = GUI.skin.GetStyle("Label");
  73. centeredStyle.alignment = TextAnchor.UpperCenter;
  74. GUI.Box(new Rect(
  75. Screen.width / 2 - 50 + 20 * level, // <== INDENTATION
  76. Screen.height / 2 - 25, 100, 50),
  77. iteminformation.description, centeredStyle);
  78. }
  79. }
  80. // Process next deeper level
  81. ProcessOnGUI(child, level + 1);
  82. }
  83. }
  84.  
  85. public class ViewableObject : UnityEngine.MonoBehaviour
  86. {
  87. public string displayText;
  88. public bool isInteractable;
  89. }
  90. }
  91.  
  92. if (Physics.Raycast(cam.transform.position, cam.transform.forward, out whatObjectHit, distanceToSee, layerMask))
  93.  
  94. objectWasHit = whatObjectHit.collider.gameObject;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement