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.

145 lines
4.2 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. Debug.Log(hit.name);
  39. BuoyantObject hitScript = hit.gameObject.GetComponent<BuoyantObject>();
  40. if (hitScript != null) {
  41. Debug.DrawLine(point, hit.transform.position, Color.blue, 1);
  42. StartCoroutine(hitScript.pushObject(point, power, 2));
  43. }
  44. //StartCoroutine(waveController(point, radius, power, 2));
  45. }
  46. }
  47. #endregion Interaction Functions
  48. #region Helper Functions
  49. private IEnumerator waveController (Vector3 point, float radius, float power, float rippleTime) {
  50. float elapsedTime = 0.0f;
  51. while (elapsedTime < rippleTime) {
  52. float curRippleForce = RipplePower.Evaluate(elapsedTime / rippleTime);
  53. Collider[] colliders = Physics.OverlapSphere(point, radius);
  54. foreach (Collider hit in colliders) {
  55. Rigidbody rb = hit.GetComponent<Rigidbody>();
  56. Debug.DrawLine(point, hit.transform.position, Color.blue, 1);
  57. if (rb != null) {
  58. Debug.DrawLine(point, hit.transform.position, Color.blue, 1);
  59. Vector3 flatPoint = new Vector3(point.x, hit.transform.position.y, point.z);
  60. rb.AddExplosionForce(power * curRippleForce, point, radius, 0.0f);
  61. }
  62. }
  63. yield return new WaitForEndOfFrame();
  64. elapsedTime += Time.deltaTime;
  65. }
  66. }
  67. #endregion Helper Functions
  68. #region Collision Functions
  69. void OnTriggerEnter(Collider other) {
  70. //calls appropriate function if the object should interact with the water on enter
  71. WaterObject waterInteraction = other.gameObject.GetComponent<WaterObject>();
  72. if (waterInteraction != null) {
  73. waterInteraction.OnWaterEnter(this);
  74. }
  75. }
  76. void OnTriggerStay(Collider other) {
  77. //calls appropriate function if the object should interact with the water on stay
  78. WaterObject waterInteraction = other.gameObject.GetComponent<WaterObject>();
  79. if (waterInteraction != null) {
  80. Vector2 waveCoords = new Vector2(other.transform.position.x, other.transform.position.z);
  81. waveCoords = waveCoords / waterWidth * waveScale;
  82. float waveHeight = Mathf.PerlinNoise(waveCoords.x, waveCoords.y + waveOffset);
  83. waterInteraction.OnWaterStay(this,waveHeight * maxWaveHeight);
  84. }
  85. }
  86. void OnTriggerExit(Collider other) {
  87. //calls appropriate function if the object should interact with the water on exit
  88. WaterObject waterInteraction = other.gameObject.GetComponent<WaterObject>();
  89. if (waterInteraction != null) {
  90. waterInteraction.OnWaterExit(this);
  91. }
  92. }
  93. #endregion Collision Functions
  94. }