You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.1 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class WaterController : MonoBehaviour {
  5. public AnimationCurve RipplePower;
  6. private float waveOffset = 0;
  7. public float waveScale = 1;
  8. public float waterWidth = 1;
  9. public float waveSpeed = 1;
  10. public float maxWaveHeight = 2;
  11. public GameObject rippleEffect;
  12. #region Unity Functions
  13. // Use this for initialization
  14. void Start() {
  15. }
  16. // Update is called once per frame
  17. void Update() {
  18. waveOffset += 0.1f *waveSpeed * Time.deltaTime;
  19. }
  20. #endregion Unity Fucntions
  21. #region Interaction Functions
  22. /// <summary>
  23. /// Pushes any objects in water away from this point
  24. /// </summary>
  25. /// <param name="point"> point where water is createds</param>
  26. /// <param name="radius"> radius of effected object</param>
  27. /// <param name="power"> power with chich the objects are pushed</param>
  28. public void CreateWave(Vector3 point, float radius, float power) {
  29. //find all colliders within the wave distance
  30. point.y = transform.position.y;
  31. Vector3 pos = point;
  32. pos.y += 1;
  33. Quaternion rot = Quaternion.Euler(-90, 0, 0);
  34. if (rippleEffect != null)
  35. Destroy(Instantiate(rippleEffect, pos, rot), 4);
  36. Collider[] colliders = Physics.OverlapSphere(point, radius);
  37. foreach (Collider hit in colliders) {
  38. BuoyantObject hitScript = hit.gameObject.GetComponent<BuoyantObject>();
  39. if (hitScript != null) {
  40. Debug.DrawLine(point, hit.transform.position, Color.blue, 1);
  41. StartCoroutine(hitScript.pushObject(point, power, 2));
  42. }
  43. //StartCoroutine(waveController(point, radius, power, 2));
  44. }
  45. }
  46. #endregion Interaction Functions
  47. #region Helper Functions
  48. private IEnumerator waveController (Vector3 point, float radius, float power, float rippleTime) {
  49. float elapsedTime = 0.0f;
  50. while (elapsedTime < rippleTime) {
  51. float curRippleForce = RipplePower.Evaluate(elapsedTime / rippleTime);
  52. Collider[] colliders = Physics.OverlapSphere(point, radius);
  53. foreach (Collider hit in colliders) {
  54. Rigidbody rb = hit.GetComponent<Rigidbody>();
  55. Debug.DrawLine(point, hit.transform.position, Color.blue, 1);
  56. if (rb != null) {
  57. Debug.DrawLine(point, hit.transform.position, Color.blue, 1);
  58. Vector3 flatPoint = new Vector3(point.x, hit.transform.position.y, point.z);
  59. rb.AddExplosionForce(power * curRippleForce, point, radius, 0.0f);
  60. }
  61. }
  62. yield return new WaitForEndOfFrame();
  63. elapsedTime += Time.deltaTime;
  64. }
  65. }
  66. #endregion Helper Functions
  67. #region Collision Functions
  68. void OnTriggerEnter(Collider other) {
  69. //calls appropriate function if the object should interact with the water on enter
  70. WaterObject waterInteraction = other.gameObject.GetComponent<WaterObject>();
  71. if (waterInteraction != null) {
  72. waterInteraction.OnWaterEnter(this);
  73. }
  74. }
  75. void OnTriggerStay(Collider other) {
  76. //calls appropriate function if the object should interact with the water on stay
  77. WaterObject waterInteraction = other.gameObject.GetComponent<WaterObject>();
  78. if (waterInteraction != null) {
  79. Vector2 waveCoords = new Vector2(other.transform.position.x, other.transform.position.z);
  80. waveCoords = waveCoords / waterWidth * waveScale;
  81. float waveHeight = Mathf.PerlinNoise(waveCoords.x, waveCoords.y + waveOffset);
  82. waterInteraction.OnWaterStay(this,waveHeight * maxWaveHeight);
  83. }
  84. }
  85. void OnTriggerExit(Collider other) {
  86. //calls appropriate function if the object should interact with the water on exit
  87. WaterObject waterInteraction = other.gameObject.GetComponent<WaterObject>();
  88. if (waterInteraction != null) {
  89. waterInteraction.OnWaterExit(this);
  90. }
  91. }
  92. #endregion Collision Functions
  93. }