From Test-Scratch-Wiki

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:

Wall Jumping 4.png

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 [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 [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))


Note Note: The color black can be changed depending on the color of the wall and floor sprites, but the floor and walls must be the same color.

And one for jumping:

when I receive [Jump v]
repeat (10)
if <color [#FF0000] is touching [#000000]?> then
set [Y-vel v] to [0]
stop script
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]>>
change y by (-10)
end
if <key [up arrow v] pressed>
change y by (60)
end
change y by (1)
change x by (velocity)
if <touching [ground v]>
change x by ((0)-(velocity))
if <key [up arrow v] pressed>
change y by (60)
change x by ((0)-(velocity))
end
change y by (-1)
set [velocity v] to ((velocity)*(0.75))
end

Examples

Cookies help us deliver our services. By using our services, you agree to our use of cookies.