Devblog 6 - Swimming Among Bricks

I have added a new block, the water one. It does not expand progressively, but it is "liquid" for the player. To make it possible, you have to play with physics, since when you are underwater the movement is slower and you can float.

Posted by on

I have added a new block, the water one. It does not expand progressively, but it is "liquid" for the player.
To make it possible, you have to play with physics. Since when you are underwater, the movement is slower and you can float.
We need a block that does not collide with the player, while at the same time it does collide with the player so we know that it is inside him.
How do we resolve this inconsistency? Using different physical layers.
In Unity, physics work on multiple layers and we can uncheck which ones collide with each other:

The player is in the Default layer, which we have made not to collide with the objects that are in the water layer. With this we can go through / be in the water.
Now it is time to detect that we are in it. To do this we will create an object within the player with the same shape, but we will alter the collider and put it on another layer.
In unity you can make a collider collide or not with other colliders and, through events, you can know when another object is passing through it.
Example of a normal collider going to trigger mode:

Having the collider in trigger mode, we will review each object that the player penetrates to see if it is a water-type brick and we will write it down in a list. It is important to have the checklist and not simply sign whether or not you are in the water, because the water is made up of multiple blocks, which means that player will be "in and out" of water blocks while actually still in water.

We can get an idea of ​​whether player is completely submerged or only has his feet, based on the number of blocks that we have evidence that he is "in" at every moment.
Now that we can go through the water and know that we are inside it, we only have to alter the physics / gravity of the player.
When in the water, gravity will be reduced to 1/4 and you can also hold down the SPACE key to float up. We will also vary the gravity based on whether or not you are pressing the SPACE key so that it can go up easily, but if you stop pressing it, it will go to the bottom.

With this we will obtain a behavior similar to Minecraft, where if you keep the SPACE pressed in the water, you go up until only the little feet are left and you fall down a little constantly.

Using a shader we could get to have a more animated surface, like:

But it is not my field.