30 lines
673 B
C#
30 lines
673 B
C#
using UnityEngine;
|
|
|
|
public class SimpleBuoyancy : MonoBehaviour
|
|
{
|
|
public float waterLevel;
|
|
|
|
public float floatHeight;
|
|
|
|
public Vector3 buoyancyCentreOffset;
|
|
|
|
public float bounceDamp;
|
|
|
|
private Vector3 actionPoint;
|
|
|
|
private float forceFactor;
|
|
|
|
private Vector3 uplift;
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
actionPoint = base.transform.position + base.transform.TransformDirection(buoyancyCentreOffset);
|
|
forceFactor = 1f - (actionPoint.y - waterLevel) / floatHeight;
|
|
if (forceFactor > 0f)
|
|
{
|
|
uplift = -Physics.gravity * (forceFactor - GetComponent<Rigidbody>().velocity.y * bounceDamp);
|
|
GetComponent<Rigidbody>().AddForceAtPosition(uplift, actionPoint);
|
|
}
|
|
}
|
|
}
|