Advertisement
Guest User

Untitled

a guest
May 27th, 2019
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 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[] childrenToSearhc;
  23.  
  24. private void Start()
  25. {
  26. childrenToSearhc = 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. // Bit shift the index of the layer (8) to get a bit mask
  40. int layerMask = 1 << 8;
  41.  
  42. // This would cast rays only against colliders in layer 8.
  43. // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
  44. layerMask = ~layerMask;
  45.  
  46. Debug.DrawRay(cam.transform.position, cam.transform.forward * distanceToSee, Color.magenta);
  47. if (Physics.Raycast(cam.transform.position, cam.transform.forward, out whatObjectHit, distanceToSee, layerMask))
  48. {
  49. objectHit = whatObjectHit.collider.gameObject.name;
  50. interactableObject = true;
  51. print("Hit ! " + whatObjectHit.collider.gameObject.name);
  52.  
  53. if (scaling.objectToScale.transform.localScale == scaling.minSize)
  54. {
  55. scaling.objectToScale.transform.Rotate(spinX, spinY, spinZ);
  56. }
  57. ProcessItemsDescripations();
  58. itemsDescriptionCanvas.SetActive(true);
  59. }
  60. else
  61. {
  62. if (scaling.objectToScale.transform.localScale == scaling.minSize)
  63. {
  64. navi.transform.rotation = new Quaternion(0, 0, 0, 0);
  65. }
  66. itemsDescriptionCanvas.SetActive(false);
  67. print("Not Hit !");
  68. }
  69. }
  70. }
  71.  
  72. private void ExecuteActions(GameObject go)
  73. {
  74. var ia = go.GetComponent<ItemAction>();
  75. if (ia != null)
  76. {
  77. ia.ItemMove();
  78. }
  79. }
  80.  
  81. void ProcessItemsDescripations()
  82. {
  83. foreach (Transform child in childrenToSearhc)
  84. {
  85. if (child.GetComponent<ItemInformation>() != null)
  86. {
  87. ItemInformation iteminformation = child.GetComponent<ItemInformation>();
  88. if (child.name == objectHit)
  89. {
  90. itemsDescriptionText.text = iteminformation.description;
  91. }
  92. }
  93. }
  94. }
  95.  
  96. public class ViewableObject : UnityEngine.MonoBehaviour
  97. {
  98. public string displayText;
  99. public bool isInteractable;
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement