#Buoyancy system
1 messages · Page 1 of 1 (latest)
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
Detect the collision with water and keep the object from falling
if you have waves you might want to check the surface normal and lerp the object to that position
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
in this case its jsut a flat plane
it is not reccomended to directly modify the linearVelocity
how so?
It will override any other velocity you have on the object
and it doesn't care about the weight
ive never noticed this behavior. what other velocities will it override?
all linear velocities
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
if you add the previous velocity to your calculation you can sometimes get away with it, but again it is not recommended
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
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.