1. Lost Twins 2
  2. News
  3. Devlog # 51 - Character physics in water Part 2

Devlog # 51 - Character physics in water Part 2

Hi again!
In our last devlog, we tackled the intricacies of character physics in water, exploring how buoyancy and fluid dynamics affect movement. This time, we’re taking it a step further by diving into the interactions between characters and in-game objects submerged in water. From floating boxes to sinking boulders, we’ll walk you through the mechanics behind how these objects behave when they enter the water, and how we’ve enhanced their physics to create a more immersive and dynamic experience. Let’s jump in!

These objects already have rigidbodies, colliders, and ground physics implemented, so adding buoyancy to them was the logical next step. Just like the characters, we added buoyancy forces to make these objects behave more naturally when submerged. The boulder, being denser, sinks slowly compared to how it behaves in air, while the box rises to the surface at a steady pace.

[previewyoutube][/previewyoutube]

Floating Box Physics

The floating box required a few extra tweaks to make it feel more natural. To achieve a gentle wobble when the box floats at the water’s surface, we added a slight variation to the buoyancy force. Initially, the box rises and falls more noticeably when it first reaches the surface, as the buoyancy force gradually decreases and stabilizes. As the forces of gravity and buoyancy find a balance, the box settles, but we continuously introduce slight fluctuations in the FixedUpdate() to maintain a subtle wobble effect.

Additionally, we had to account for when a character lands on top of the box. Since all our physics are custom, we applied a small downward impulse when the character jumps onto the box. This decreases the buoyancy just enough to submerge the box slightly more than when the character is not on it. To add a bit of realism, we also introduced a small torque force. The torque is based on the character’s X position relative to the box’s center, causing the box to rotate toward the character’s landing position. If the character lands too far to the side, the box rotates by up to 90 degrees, eventually submerging the player if they fall off.

To ensure the box remains upright in the water, we applied a lerp function to rotate the box back to the nearest 90-degree angle relative to the water’s surface.

[previewyoutube][/previewyoutube]

Pushing Boxes Underwater

When the character pushes a box underwater, we use the OnCollisionEnter() function to detect when the player collides with the box. If the player presses any directional key, force is applied to the box in that direction. Additionally, it switches the character’s animation from normal swimming to a “pushing box” animation to reflect this action.

Unstucking Boxes and Boulders

Sometimes, the box or boulder can get stuck in corners between a wall and a roof. When this happens, we added an extra layer of logic: when the character presses the up key while stuck in a corner, an additional force is applied in the opposite direction of the wall to free the object. This ensures the box or boulder can escape these tight spots and continue interacting with the character.

[previewyoutube][/previewyoutube]

In the next part of this water series, we’ll be covering how we implemented some of the special water-based effects for both characters and interactable objects. Stay tuned for more!

Best regards,
Playdew Team

https://store.steampowered.com/app/1752540/Lost_Twins_II/