Advertisement
Guest User

Untitled

a guest
May 26th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 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 Scaling scaling;
  14. public int spinX = 0;
  15. public int spinY = 0;
  16. public int spinZ = 0;
  17. public GameObject navi;
  18. public GameObject itemsDescriptionCanvas;
  19. public Text itemsDescriptionText;
  20.  
  21. private RaycastHit whatObjectHit;
  22. private Transform[] childrenToSearch;
  23.  
  24. private void Start()
  25. {
  26. childrenToSearch = parentToSearch.GetComponentsInChildren<Transform>();
  27. }
  28.  
  29. private void Update()
  30. {
  31. if (cam.enabled == true)
  32. {
  33. if (Input.GetMouseButtonDown(0) && !scaling.scaleUp)
  34. {
  35. if (whatObjectHit.collider != null)
  36. ExecuteActions(whatObjectHit.collider.gameObject);
  37. }
  38.  
  39. Debug.DrawRay(cam.transform.position, cam.transform.forward * distanceToSee, Color.magenta);
  40. if (Physics.Raycast(cam.transform.position, cam.transform.forward, out whatObjectHit, distanceToSee))
  41. {
  42. if (whatObjectHit.transform.tag == "Interactable")
  43. {
  44. objectHit = whatObjectHit.collider.gameObject.name;
  45. interactableObject = true;
  46. print("Hit ! " + whatObjectHit.collider.gameObject.name);
  47.  
  48. if (scaling.objectToScale.transform.localScale == scaling.minSize)
  49. {
  50. scaling.objectToScale.transform.Rotate(spinX, spinY, spinZ);
  51. }
  52. ProcessItemsDescripations();
  53. itemsDescriptionCanvas.SetActive(true);
  54. }
  55. }
  56. else
  57. {
  58. if (scaling.objectToScale.transform.localScale == scaling.minSize)
  59. {
  60. navi.transform.rotation = new Quaternion(0, 0, 0, 0);
  61. }
  62. itemsDescriptionCanvas.SetActive(false);
  63. print("Not Hit !");
  64. }
  65. }
  66. }
  67.  
  68. private void ExecuteActions(GameObject go)
  69. {
  70. var ia = go.GetComponent<ItemAction>();
  71. if (ia != null)
  72. {
  73. ia.ItemMove();
  74. }
  75. }
  76.  
  77. void ProcessItemsDescripations()
  78. {
  79. foreach (Transform child in childrenToSearch)
  80. {
  81. if (child.GetComponent<ItemInformation>() != null)
  82. {
  83. ItemInformation iteminformation = child.GetComponent<ItemInformation>();
  84. if (child.name == objectHit)
  85. {
  86. itemsDescriptionText.text = iteminformation.description;
  87. }
  88. }
  89. }
  90. }
  91.  
  92. public class ViewableObject : UnityEngine.MonoBehaviour
  93. {
  94. public string displayText;
  95. public bool isInteractable;
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement