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.

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