CUI_GunController.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.EventSystems;
  4. using UnityEngine.UI;
  5. namespace CurvedUI
  6. {
  7. public class CUI_GunController : MonoBehaviour
  8. {
  9. #pragma warning disable 0649
  10. [SerializeField]
  11. CurvedUISettings ControlledCanvas;
  12. [SerializeField]
  13. Transform LaserBeamTransform;
  14. #pragma warning restore 0649
  15. // Update is called once per frame
  16. void Update()
  17. {
  18. //tell canvas to use the direction of the gun as a ray controller
  19. Ray myRay = new Ray(this.transform.position, this.transform.forward);
  20. if (ControlledCanvas)
  21. CurvedUIInputModule.CustomControllerRay = myRay;
  22. //change the laser's length depending on where it hits
  23. float length = 10000;
  24. RaycastHit hit;
  25. if (Physics.Raycast(myRay, out hit, length))
  26. {
  27. //check for graphic under pointer if we hit curved canvas. We only want transforms with graphics that are drawn by canvas (depth not -1) to block the pointer.
  28. int SelectablesUnderPointer = 0;
  29. if (hit.transform.GetComponent<CurvedUIRaycaster>() != null)
  30. {
  31. SelectablesUnderPointer = hit.transform.GetComponent<CurvedUIRaycaster>().GetObjectsUnderPointer().FindAll(x => x.GetComponent<Graphic>() != null && x.GetComponent<Graphic>().depth != -1).Count;
  32. }
  33. //Debug.Log("found graphics: " + SelectablesUnderPointer);
  34. length = SelectablesUnderPointer == 0 ? 10000 : Vector3.Distance(hit.point, this.transform.position);
  35. }
  36. LaserBeamTransform.localScale = LaserBeamTransform.localScale.ModifyZ(length);
  37. //make laser beam thicker if mose is pressed
  38. if (Input.GetMouseButton(0))
  39. {
  40. LaserBeamTransform.localScale = LaserBeamTransform.localScale.ModifyX(0.75f).ModifyY(0.75f);
  41. }
  42. else {
  43. LaserBeamTransform.localScale = LaserBeamTransform.localScale.ModifyX(0.2f).ModifyY(0.2f);
  44. }
  45. }
  46. }
  47. }