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.

72 lines
1.6 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(GameObject 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.98f;
  30. rb.velocity = dragVel;
  31. }
  32. public override void OnWaterEnter(GameObject water) {
  33. //rb.drag = 1;
  34. }
  35. public override void OnWaterExit(GameObject water) {
  36. // rb.drag = airDrag;
  37. }
  38. /// <summary>
  39. ///
  40. /// </summary>
  41. public void BuoyancyCalc() {
  42. }
  43. }