BallLauncher.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using System.Collections;
  5. /// <summary>
  6. /// Creates multiple balls with physics materials and launch them.
  7. /// </summary>
  8. public class BallLauncher : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// Prefab object to be launched.
  12. /// </summary>
  13. [Tooltip("Prefab object to be launched.")]
  14. public GameObject projectile;
  15. /// <summary>
  16. /// Array of all previously-instantiated projectiles, so we can reuse them and prevent too many from appearing (similar to a queue).
  17. /// </summary>
  18. [Tooltip("Array of all previously-instantiated projectiles, so we can reuse them and prevent too many from appearing (similar to a queue).")]
  19. private GameObject[] projectiles;
  20. /// <summary>
  21. /// Launch intensity for the init velocity of the ball.
  22. /// </summary>
  23. [Tooltip("Launch intensity for the init velocity of the ball.")]
  24. public int LaunchIntensity = 750;
  25. /// <summary>
  26. /// Maximum number of spheres that can be instantiated at once.
  27. /// </summary>
  28. private const int SPHERE_NB = 100;
  29. /// <summary>
  30. /// Distance max between the user and the sphere before the sphere is disabled.
  31. /// </summary>
  32. private const int DISTANCE_MAX = 50;
  33. /// <summary>
  34. /// Maximum time a sphere can be alive before it gets disabled.
  35. /// </summary>
  36. private const int TIME_MAX = 30;
  37. /// <summary>
  38. /// Colors of the balls.
  39. /// </summary>
  40. private Color[] ballcolors;
  41. /// <summary>
  42. /// How long each sphere has been around.
  43. /// </summary>
  44. private float[] times;
  45. /// <summary>
  46. /// ID of the next sphere to launch.
  47. /// </summary>
  48. private int countsphere = 0;
  49. /// <summary>
  50. /// Cooldown period between launching balls.
  51. /// </summary>
  52. private float timeballmax = 0.05f;
  53. /// <summary>
  54. /// The actual timer incremented each frame after firing a ball, then resetting once timeballmax is hit.
  55. /// </summary>
  56. private float timerball = 0.0f;
  57. /// <summary>
  58. /// Offset of the launcher from the transform.
  59. /// </summary>
  60. private Vector3 offset = new Vector3(0.1f, -0.1f, 0.0f);
  61. /// <summary>
  62. /// The launcher
  63. /// </summary>
  64. private GameObject launcher;
  65. // Use this for initialization
  66. void Start()
  67. {
  68. launcher = new GameObject("Launcher");
  69. launcher.hideFlags = HideFlags.HideInHierarchy;
  70. launcher.transform.parent = transform;
  71. launcher.transform.localPosition = offset;
  72. ballcolors = new Color[10];
  73. for (int i = 0; i < 10; i++)
  74. {
  75. ballcolors[i] = Color.HSVToRGB(0.1f * i, 0.8f, 1.0f);
  76. }
  77. projectiles = new GameObject[SPHERE_NB];
  78. times = new float[SPHERE_NB];
  79. int count = 0;
  80. for (int i = 0; i < SPHERE_NB; i++)
  81. {
  82. projectiles[i] = Instantiate(projectile, launcher.transform.position, launcher.transform.rotation);
  83. projectiles[i].transform.localScale = new Vector3(0.10f, 0.10f, 0.10f);
  84. projectiles[i].GetComponent<MeshRenderer>().material.color = ballcolors[count];
  85. Light l = projectiles[i].AddComponent<Light>();
  86. l.color = ballcolors[count];
  87. l.intensity = 2;
  88. l.range = 1.0f;
  89. projectiles[i].AddComponent<ZEDLight>();
  90. count++;
  91. if (count == 10)
  92. count = 0;
  93. projectiles[i].SetActive(false);
  94. projectiles[i].hideFlags = HideFlags.HideInHierarchy;
  95. times[i] = 0;
  96. }
  97. }
  98. static float EaseIn(float t, float b, float c, float d)
  99. {
  100. return -c * (Mathf.Sqrt(1 - (t /= d) * t) - 1) + b;
  101. }
  102. // Update is called once per frame
  103. void Update()
  104. {
  105. if (Input.GetKey(KeyCode.Space) || Input.GetButton("Fire1"))
  106. {
  107. if(timerball > timeballmax)
  108. {
  109. timerball = 0.0f;
  110. if(!projectiles[countsphere % SPHERE_NB].activeInHierarchy)
  111. {
  112. projectiles[countsphere % SPHERE_NB].SetActive(true);
  113. }
  114. projectiles[countsphere % SPHERE_NB].transform.rotation = launcher.transform.rotation;
  115. projectiles[countsphere % SPHERE_NB].transform.position = launcher.transform.position;
  116. float offsetAngleX = 0.0f;
  117. float offsetAngleY = 0.0f;
  118. launcher.transform.localRotation = Quaternion.Euler(-offsetAngleY * Mathf.Rad2Deg, -offsetAngleX * Mathf.Rad2Deg, 0);
  119. projectiles[countsphere % SPHERE_NB].GetComponent<BallTrigger>().ResetValues();
  120. Rigidbody rigidBody = projectiles[countsphere % SPHERE_NB].GetComponent<Rigidbody>();
  121. rigidBody.velocity = Vector3.zero;
  122. rigidBody.isKinematic = false;
  123. rigidBody.useGravity = true;
  124. rigidBody.AddForce(launcher.transform.forward * LaunchIntensity);
  125. times[countsphere % SPHERE_NB] = 0;
  126. countsphere++;
  127. }
  128. timerball += Time.deltaTime;
  129. }
  130. for (int i = 0; i < SPHERE_NB; i++)
  131. {
  132. if (projectiles[i].activeSelf)
  133. {
  134. if (Vector3.Distance(projectiles[i].transform.position, Vector3.zero) > DISTANCE_MAX || times[i] > TIME_MAX)
  135. {
  136. projectiles[i].SetActive(false);
  137. times[i] = 0;
  138. }
  139. else
  140. {
  141. times[i] += Time.deltaTime;
  142. }
  143. }
  144. }
  145. }
  146. }