- Home /
Very simple water bouyancy script not working?
I'm trying to implement very very simple water bouyancy. My script checks whether the object is under the water level, and then applies a force upwards if so, or not at all if it is not.
Here's my script:
if (transform.position.y < WaterLevel) {
underwater = true;
//if so, push the body back up on the y axis
rigidbody.AddForce(Vector3.up * -Physics.gravity.y);
//if we're already above the water level...
} else if (transform.position.y >= WaterLevel) {
underwater = false;
//let gravity do the rest
rigidbody.AddForce(0,0,0);
}
underwater
is a boolean used for debugging, and WaterLevel
is a float that is set at 0, for where the water plane is.
My problem is that with gravity enabled on my bouyant object, the object goes underwater, the boolean returns true, but no force is being applied back upwards again. This exerpt of code is in a FixedUpdate
function. I do not understand how this very simple statement is not working correctly.
Can you help me please?
Answer by ScroodgeM · Aug 07, 2012 at 09:20 PM
Force 10
Vector3.up * -Physics.gravity.y
can do nothing visible to object with mass 1000+....
use
rigidbody.AddForce(Vector3.up * -Physics.gravity.y, ForceMode.VelocityChange);
instead