Walk on Water

Why swim when you can walk? @showdialog

Don’t want to get wet? No problem! Use the fill block to allow your player to walk on 🌊 water! 🌊

Walk on Water

Fill block

Let’s use the Fill block to fill the area under our players’ 🦶 feet. The Fill block will fill an area from corner to corner with a given block. Learn more here.

From the ||blocks:Blocks|| category, drag the ||blocks:fill with|| block out to the workspace and drop into the ||loops:forever|| block. Click on the ||blocks:fill with|| drop-down menu to change from a 🌱 Grass Block to a block of 🧊 Ice.

loops.forever(function () {
    blocks.fill(
    ICE,
    pos(0, 0, 0),
    pos(0, 0, 0),
    FillOperation.Replace
    )
})

Use Player Position

Now we need to fill the ice under our player’s feet. Let’s use the Player Position blocks for this.

Open the ||positions:Positions|| Toolbox drawer and drag out 2 ||positions:right(0), above(0), in front(0)|| Player Position blocks. Drop one in the from field and the other in the to field of the ||blocks:fill with|| block replacing the existing position blocks.

loops.forever(function () {
    blocks.fill(
    ICE,
    posCamera(0, 0, 0),
    posCamera(0, 0, 0),
    FillOperation.Replace
    )
})

Set the location

In the first ||positions:right(0), above(0), in front(0)|| from player position block, set all three coordinates to -1. This specifies one block ← left, one block ↓ below, and one block ↙ behind the player.

In the second ||positions:right(0), above(0), in front(0)|| to player position block, set the coordinates to 1, -1, and 1. This specifies one block → right, one block ↓ below, and one block ↗ in front of the player.

This creates a square of blocks under the player.

loops.forever(function () {
    blocks.fill(
    ICE,
    posCamera(-1, -1, -1),
    posCamera(1, -1, 1),
    FillOperation.Replace
    )
})

Run your code

Press the 🟩 green 🟩 play button to run your code. What happens?

Use While loop

You may have noticed that you are creating ice blocks under your player even if your player isn’t in water. Let’s fix that. Use the While loop to continually check for water.

From the ||loops:Loops|| category, drag the ||loops:while|| block into the ||loops:forever|| block and around the ||blocks:fill with|| block.

loops.forever(function () {
    while (false) {
        blocks.fill(
        ICE,
        posCamera(-1, -1, -1),
        posCamera(1, -1, 1),
        FillOperation.Replace
        )
    }
})

Test for block

Now open the ||blocks:Blocks|| Toolbox drawer, and drag out a ||blocks:test for|| block and drop it into the ||loops:while|| block, replacing false.

loops.forever(function () {
    while (blocks.testForBlock(GRASS, pos(0, 0, 0))) {
        blocks.fill(
        ICE,
        posCamera(-1, -1, -1),
        posCamera(1, -1, 1),
        FillOperation.Replace
        )
    }
})

Test for water below your player

In the ||blocks:test for|| block, use the drop-down menu to change from 🌱 Grass block to 🌊 Water. Then set the middle (↕ up/down) relative coordinate to -1 to test for water beneath your player.

loops.forever(function () {
    while (blocks.testForBlock(WATER, pos(0, -1, 0))) {
        blocks.fill(
        ICE,
        posCamera(-1, -1, -1),
        posCamera(1, -1, 1),
        FillOperation.Replace
        )
    }
})

Run your code

Press the 🟩 green 🟩 play button to run your code. Walk around the world. Are you able to walk on water equally as well as on land?

⭐ Great job ⭐ giving your player super powers!

loops.forever(function () {

})