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.

91 lines
2.1 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. [RequireComponent (typeof(Rigidbody))]
  6. public class BuoyantObject : WaterObject {
  7. [Tooltip ("How fast object will raise into the ")]
  8. public float BouancyValue = 5;
  9. [Tooltip ("Local point, at which the object floats within the water")]
  10. public Vector3 BouancyPoint = Vector3.zero;
  11. //rigidbody attached to GameObject
  12. protected Rigidbody rb;
  13. private float airDrag;
  14. #region Unity Functions
  15. void Awake() {
  16. rb = GetComponent<Rigidbody>();
  17. airDrag = rb.drag;
  18. }
  19. #endregion Unity Functions
  20. public override void OnWaterStay(WaterController water,float waveHeight) {
  21. Vector3 waterEntryPoint = transform.position;
  22. waterEntryPoint.y = water.transform.position.y + waveHeight;
  23. float distance = Vector3.Distance(transform.position + (BouancyPoint * transform.lossyScale.magnitude) , waterEntryPoint);
  24. Vector3 force = Vector3.up * BouancyValue * Mathf.Pow(distance,1.5f);
  25. if (waterEntryPoint.y < gameObject.transform.position.y + (BouancyPoint.y * transform.lossyScale.y))
  26. force = Vector3.zero;
  27. rb.AddForce(force);
  28. Vector3 dragVel = rb.velocity;
  29. dragVel.y *= 0.9f;
  30. rb.velocity = dragVel;
  31. }
  32. public override void OnWaterEnter(WaterController water) {
  33. //rb.drag = 1;
  34. }
  35. public override void OnWaterExit(WaterController water) {
  36. // rb.drag = airDrag;
  37. }
  38. public virtual IEnumerator pushObject(Vector3 point,float power, float totalTime) {
  39. Vector3 dir = transform.position - point;
  40. dir = Vector3.ProjectOnPlane(dir, Vector3.up);
  41. dir.Normalize();
  42. float elapsedTime = 0;
  43. while (elapsedTime < totalTime) {
  44. Vector3 force = dir * power * Time.deltaTime;
  45. rb.AddForce(force);
  46. yield return new WaitForEndOfFrame();
  47. elapsedTime += Time.deltaTime;
  48. }
  49. }
  50. /// <summary>
  51. ///
  52. /// </summary>
  53. public void BuoyancyCalc() {
  54. }
  55. }