using System.Collections; using System.Collections.Generic; using UnityEngine; public class Float : MonoBehaviour { public float waterLevel = 0.0f; public float floatThreshold = 2.0f; public float waterDensity = 0.125f; public float downForce = 4.0f; float forceFactor; Vector3 floatForce; void FixedUpdate() { forceFactor = 1.0f - ((transform.position.y - waterLevel) / floatThreshold); if (forceFactor > 0.0f) { floatForce = -Physics.gravity * GetComponent().mass * (forceFactor - GetComponent().velocity.y * waterDensity); floatForce += new Vector3(0.0f, -downForce * GetComponent().mass, 0.0f); GetComponent().AddForceAtPosition(floatForce, transform.position); } } }