x


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 &gt;= 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?

more ▼

asked Aug 07 '12 at 09:16 PM

Fishman92 gravatar image

Fishman92
2.4k 101 113 128

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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

more ▼

answered Aug 07 '12 at 09:20 PM

ScroodgeM gravatar image

ScroodgeM
7.6k 2 6

Perfect. Thankyou!

Aug 07 '12 at 09:27 PM Fishman92
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1791
x461
x300
x244
x2

asked: Aug 07 '12 at 09:16 PM

Seen: 340 times

Last Updated: Aug 07 '12 at 09:27 PM