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.

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