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.

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