Wall-Jumping
Wall-Jumping is a technique used in Scratch to make sprites bounce off walls. This allows a character in a project, for instance, to scale up massive heights of walls rather than jumping on individualized sections like stairs. It is commonly used in platformers.
Method
This is just one of the ways for wall jumping to work.
Three variables are needed for this method to operate properly:
- L-vel — The velocity of the player going left
- R-vel — The velocity of the player going right
- Y-vel — The velocity of the player on the y axis
Afterwards, a player sprite should be made. This is the sprite that will perform all motions and be physically wall-jumping. An example of a player sprite is shown below:
The colours on each side of the square are the sensors that will use to "sense" which side the wall and floor are relative to the character. Using more complicated coding, it wouldn't require colours.
Two separate scripts are needed then, one for moving and one for sensing walls:
when gf clicked set [R-vel v] to [0] set [L-vel v] to [0] forever if <key (right arrow v) pressed?> then change [R-vel v] by (1) end if <key (left arrow v) pressed?> then change [L-vel v] by (1) end if <key (up arrow v) pressed?> then if <color [#0000FF] is touching [#000000]?> then set [Y-vel v] to [20] broadcast (Jump v) end end if <not <color [#0000FF] is touching [#000000]?>> then change y by (-5) end if <color [#FF0000] is touching [#000000]?> then set [Y-vel v] to [0] set [R-vel v] to [0] if <not < <color [#0000FF] is touching [#000000]?> or <key (up arrow v) pressed?> > > then wait (0.5) secs if <key (up arrow v) pressed?> then set [Y-vel v] to [20] broadcast (Jump v) end set [L-vel v] to [10] end end if <color [#00FF00] is touching [#000000]?> then set [Y-vel v] to [0] set [L-vel v] to [0] if <not < <color [#0000FF] is touching [#000000]?> or <key (up arrow v) pressed?> > > then wait (0.5) secs if <key (up arrow v) pressed?> then set [Y-vel v] to [20] broadcast (Jump v) end set [R-vel v] to [10] end end change x by (R-vel) change x by ((0) - (L-vel)) change [R-vel v] by ((R-vel) * (-0.2)) change [L-vel v] by ((L-vel) * (-0.2))
And one for jumping:
when I receive [Jump v] repeat (10) if <color [#FFFF00] is touching [#000000]?> then set [Y-vel v] to [0] stop [this script v] end change y by (Y-vel) change [Y-vel v] by (-1) end set [Y-vel v] to [0]
There is another way of doing it. One variable is needed
- velocity — The velocity of the player's horizontal movement.
when gf clicked forever if <key (left arrow v) pressed?> then change [velocity v] by (-1) end if <key (right arrow v) pressed?> then change [velocity v] by (1) end if <not <touching (ground v)?>> then change y by (-10) end if <key (up arrow v) pressed?> then change y by (60) end change y by (1) change x by (velocity) if <touching (ground v)?> then change x by ((0)-(velocity)) if <key (up arrow v) pressed?> then change y by (60) change x by ((0)-(velocity)) end change y by (-1) set [velocity v] to ((velocity)*(0.75)) end
Method 2
An easier way uses very simple coding. The sprite can be any color, and no velocity script is needed. Gray should be the color of the walls.
when gf clicked set [wall jump v] to (0) forever if <<<not<touching color [#000000]?>>and<touching color[#444444]?>>and<<key (space v) pressed?>or<key (up arrow v) pressed?>>> then if <not<(wall jump)=(1)>> then set [wall jump v] to (1) repeat (10) change y by (15) end wait until <color [#0000FF] is touching [#000000]?> set [wall jump v] to (0) end
A separate code is needed to make movement.
when gf clicked forever if <key (left arrow v) pressed?> then change x by (-4) end if <key (right arrow v) pressed?> then change x by (4) end if <<key (space v) pressed?>and<color [#0000FF] is touching [#000000]?>> then repeat (10) change y by (15) end end if <not<color[#0000FF] is touching [#000000]?>> then change y by (-7) end if <color [#FFFF00] is touching [#000000]?> then change y by (-8)
Examples
- Wall jump engine (the script that was used for this article)