#Buoyancy system

1 messages · Page 1 of 1 (latest)

brittle hull
#

I am wanting to make a simple buoyancy system where cubes will float in water and the player can stand on top of them without falling into the water too easily. what's the best way to try and make this?

steel reef
#

The easiest and arguably the best way is to use a buoyancy asset or a water asset that has a buoyancy system built in. If you want to make one yourself it depends on how realistic you want it

stable axle
#

if you have waves you might want to check the surface normal and lerp the object to that position

brittle hull
#

the method ive tried so far is this

            float depth = waterLevel - transform.position.y;
            Vector3 force = rb.linearVelocity;
            force += Vector3.up * buoyancyForce * depth;
            rb.linearVelocity = force;
#

but it dosent work too well

brittle hull
stable axle
#

it is not reccomended to directly modify the linearVelocity

stable axle
#

It will override any other velocity you have on the object

#

and it doesn't care about the weight

brittle hull
stable axle
#

all linear velocities

brittle hull
#

ive directly modiefed linear velocity for my player and all rigid body objects so far

#

and i havent experenced any issues of them being overwritten

stable axle
#

if you add the previous velocity to your calculation you can sometimes get away with it, but again it is not recommended

brittle hull
#

i mean what i do is get current linear velocity, set it as a new vareable, do math on that, then set the velocity so its basiclly just adding it

#

but now i can directly modify specific aspects like the x, y, and z vectors as needed because i need to have custom implementation for gravity and all

stable axle
#

the AddForce function can handle all that as well, while also taking drag and weight into consideration, so you have more accurate and predictable behaviour

#

and simpler math

#

anyway for buoyancy, check out Unity's samples for the water system (HDRP only), it has a buoyancy script that you can adapt to your water.

clever karma
#

AddForce makes it easier to reason about, yes

#

I would only set the velocity if I was doing something completely non-physical