When your character gets into deep enough water he will start to swim on the surface of the water. What if you wanted him to swim underthe water? There are a few methods for this using mass or gravity, however the character cannot maintain a level in the water using those methods, and will slowly sink or rise. This method uses the y position of the character in the water. To do this you need to know the starting height for normal swimming and the maximum depth (minimum y position) before they pass through the sea floor. You can do this by finding out the y position of your player when swimming by displaying it on screen. You need to set your surface number variable to that number and use that as your maximum on the clamp. Display the height variable on screen and press the controls until you find the lowest point you want your character to swim and use that as the minimum on the clamp. If you do not set a minimum the player can pass through the sea bed and under your map. If you do not set a maximum the player will float out of the water and into the air.
This kode can also be added to vehicles.
If you have NPC that you want to swim in the water, just add
WHEN DO [position][y][=]
The number being the depth in the water you would like them to swim at. Use the swimming fish brain, or wandering villager brain. You do not need to add with swimming to the move command, but I would remove the interact/talk command as it looks a bit odd when you are underwater.
The player cannot jump from the water as we are using the A button to control depth – however it is not realistic for humans to leap like dolphins. Make sure there is a shallow exit for the player to climb out of the water.
See this tutorial for limited air when swimming underwater .
Set up a boolean that tells us when our character is swimming, and another that sets up the dive boolean (you will need this if you implement the limited air option), and a variable that says what depth the surface of the water is.
WHEN [swimming] DO [boolvar:swim][=][true]
…WHEN [else]DO [boolvar:swim][=][false]
WHEN [once] DO [boolvar:dive][=][false]
…WHEN DO [numvar:surface][=][7.8]
Sets the maximum and minimum height our player can achieve. These numbers will be dependent on how deep you want the player to swim, and the how high the water level is.
WHEN [swim][or][dive] DO
…WHEN DO [numvar:height][=][clamp][max][numvar:surface][min][numvar:height]
…WHEN [started to] DO [height][=][numvar:surface]
These are the controls. If you are bumping terrain you cannot sink, this way you dont pass into any sloping terrain that is higher than the bottom of your sea. 1
…WHEN [Y] DO [height][increment by][0.05]
…WHEN [not][bump terrain] DO
…/…WHEN [A] DO [height][decrement by][0.05]
This controls the player’s position
WHEN DO [position][y][=][height]
WHEN [position][y][<][numvar:surface] DO [dive][=][true] WHEN [position][y][>][numvar:surface] DO [dive][=][false]
If you are using the A and Y buttons out of the water (jump and shoot) then put them as childlines to
- Thanks to Saxona Ian. Note: If you have a tunnel you want your player to swim through then you will have to change the upper limit so that you cannot bump the tunnel above you.Set up a trigger zone at the entrance of your tunnel.When your player enters change your clamped variablesWHEN DO [numvar:height][=][clamp][max][numvar:surface][min][numvar:height]
instead of numvar:surface put the maximum height they can swim in the tunnel and set the minimum if your tunnel floor is higher than the normal floor.At the exit of the tunnel put another trigger zone and set them back to the original settings.