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.

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