Help with Water Buoyancy Physics (using a trigger)
I'm trying to make it so that objects (with rigidbodies) float when the enter the waters trigger area, it works to an extent but once they reach the top they just bob there really fast as they're constantly entering and exiting the water. What I would like is some way to slow the bob down slowly until the object is almost completely still. Here's the Buoyancy script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class buoyancy : MonoBehaviour
{
public float upwardForce = 12.72f;
bool isInWater = false;
Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "WaterObj")
{
isInWater = true;
rb.drag = 1.5f;
upwardForce = 13f;
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "WaterObj")
{
isInWater = false;
rb.drag = 0f;
upwardForce = 10.1f;
StopCoroutine("moreUpwardForce");
}
}
private void FixedUpdate()
{
if (isInWater == true)
{
Vector3 force = Vector3.up * upwardForce;
rb.AddForce(force, ForceMode.Acceleration);
}
}
}
I know that most people achieve a buoyancy effect based on the waters Y coordinate, but that won't work for my game so I need it to be done with triggers. Also, if you know of a way to make the object rotate so that it is facing upwards (ie if it was thrown into the water and started spinning it would gradually rotate until it was facing up).
Thank you very much for any help!
I found a Reddit post that says that I could use a bounding box that deter$$anonymous$$es how much of the object is under water and apply an upward force based on how much of it is submerged but I don't know how exactly I'd go about doing something like that, does anyone have any ideas?
your help is much appreciated :)