PolygonBeamScript.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. namespace PolygonArsenal
  5. {
  6. public class PolygonBeamScript : MonoBehaviour {
  7. [Header("Prefabs")]
  8. public GameObject[] beamLineRendererPrefab;
  9. public GameObject[] beamStartPrefab;
  10. public GameObject[] beamEndPrefab;
  11. private int currentBeam = 0;
  12. private GameObject beamStart;
  13. private GameObject beamEnd;
  14. private GameObject beam;
  15. private LineRenderer line;
  16. [Header("Adjustable Variables")]
  17. public float beamEndOffset = 1f; //How far from the raycast hit point the end effect is positioned
  18. public float textureScrollSpeed = 8f; //How fast the texture scrolls along the beam
  19. public float textureLengthScale = 3; //Length of the beam texture
  20. [Header("Put Sliders here (Optional)")]
  21. public Slider endOffSetSlider; //Use UpdateEndOffset function on slider
  22. public Slider scrollSpeedSlider; //Use UpdateScrollSpeed function on slider
  23. [Header("Put UI Text object here to show beam name")]
  24. public Text textBeamName;
  25. // Use this for initialization
  26. void Start()
  27. {
  28. if (textBeamName)
  29. textBeamName.text = beamLineRendererPrefab[currentBeam].name;
  30. if (endOffSetSlider)
  31. endOffSetSlider.value = beamEndOffset;
  32. if (scrollSpeedSlider)
  33. scrollSpeedSlider.value = textureScrollSpeed;
  34. }
  35. // Update is called once per frame
  36. void Update()
  37. {
  38. if (Input.GetKeyDown(KeyCode.Escape))
  39. Application.Quit();
  40. if (Input.GetMouseButtonDown(0))
  41. {
  42. beamStart = Instantiate(beamStartPrefab[currentBeam], new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
  43. beamEnd = Instantiate(beamEndPrefab[currentBeam], new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
  44. beam = Instantiate(beamLineRendererPrefab[currentBeam], new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
  45. line = beam.GetComponent<LineRenderer>();
  46. }
  47. if (Input.GetMouseButtonUp(0))
  48. {
  49. Destroy(beamStart);
  50. Destroy(beamEnd);
  51. Destroy(beam);
  52. }
  53. if (Input.GetMouseButton(0))
  54. {
  55. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  56. RaycastHit hit;
  57. if (Physics.Raycast(ray.origin, ray.direction, out hit))
  58. {
  59. Vector3 tdir = hit.point - transform.position;
  60. ShootBeamInDir(transform.position, tdir);
  61. }
  62. }
  63. if (Input.GetKeyDown(KeyCode.RightArrow)) //4 next if commands are just hotkeys for cycling beams
  64. {
  65. nextBeam();
  66. }
  67. if (Input.GetKeyDown(KeyCode.D))
  68. {
  69. nextBeam();
  70. }
  71. if (Input.GetKeyDown(KeyCode.A))
  72. {
  73. previousBeam();
  74. }
  75. else if (Input.GetKeyDown(KeyCode.LeftArrow))
  76. {
  77. previousBeam();
  78. }
  79. }
  80. public void nextBeam() // Next beam
  81. {
  82. if (currentBeam < beamLineRendererPrefab.Length - 1)
  83. currentBeam++;
  84. else
  85. currentBeam = 0;
  86. if (textBeamName)
  87. textBeamName.text = beamLineRendererPrefab[currentBeam].name;
  88. }
  89. public void previousBeam() // Previous beam
  90. {
  91. if (currentBeam > - 0)
  92. currentBeam--;
  93. else
  94. currentBeam = beamLineRendererPrefab.Length - 1;
  95. if (textBeamName)
  96. textBeamName.text = beamLineRendererPrefab[currentBeam].name;
  97. }
  98. public void UpdateEndOffset()
  99. {
  100. beamEndOffset = endOffSetSlider.value;
  101. }
  102. public void UpdateScrollSpeed()
  103. {
  104. textureScrollSpeed = scrollSpeedSlider.value;
  105. }
  106. void ShootBeamInDir(Vector3 start, Vector3 dir)
  107. {
  108. line.positionCount = 2;
  109. line.SetPosition(0, start);
  110. beamStart.transform.position = start;
  111. Vector3 end = Vector3.zero;
  112. RaycastHit hit;
  113. if (Physics.Raycast(start, dir, out hit))
  114. end = hit.point - (dir.normalized * beamEndOffset);
  115. else
  116. end = transform.position + (dir * 100);
  117. beamEnd.transform.position = end;
  118. line.SetPosition(1, end);
  119. beamStart.transform.LookAt(beamEnd.transform.position);
  120. beamEnd.transform.LookAt(beamStart.transform.position);
  121. float distance = Vector3.Distance(start, end);
  122. line.sharedMaterial.mainTextureScale = new Vector2(distance / textureLengthScale, 1);
  123. line.sharedMaterial.mainTextureOffset -= new Vector2(Time.deltaTime * textureScrollSpeed, 0);
  124. }
  125. }
  126. }