Discuss Scratch

jmetaxas29
Scratcher
83 posts

Slope detection in platformer

I made a platformer and I made slopes. It works, but the player slowes down a LOT on slopes and I didn't even make it slow down a pixel.

Could someone help me or do I just need to start following tutorials like a normal person (no offence to people who don't follow tutorials)

Link: https://scratch.mit.edu/projects/562477530
DD-8861
Scratcher
100+ posts

Slope detection in platformer

The problem here is that you have a “stop this script” block inside your movement loop, that stops you from moving when you are touching ‘level’.

Because the slopes are also part of the level, it stops your movement when you run up a slope too. But you probably only want it to stop the motion when you touch a wall.

Your current code looks like this:

define Change x by: (x)
repeat ([abs v] of (x))
change x by (...)
if <touching [level v]?> then
set [slope v] to [0]
repeat until <<(slope) = [8]> or <not <touching [level v] ?>>>
change [slope v] by (1)
change y by (1)
end
if <(slope) = [8]> then
... // Inside this 'if statement' is what happens when you touch a wall
end

set [slope v] to [0] //Now these two blocks happen anytime you touch level.
stop [this script v] //not only for touching walls, but also slopes. So touching a slope stops your movement.
end
end

To fix this, make it only stops your movement if you touch a wall. Simply move the ‘stop this script’ to be inside the if statement:

define Change x by: (x)
repeat ([abs v] of (x))
change x by (...)
if <touching [level v]?> then
set [slope v] to [0]
repeat until <<(slope) = [8]> or <not <touching [level v] ?>>>
change [slope v] by (1)
change y by (1)
end
if <(slope) = [8]> then
... // Inside this 'if statement' is what happens when you touch a wall
set [slope v] to [0]
stop [this script v] // Now it only stops when touching a wall, so you can run up slopes at normal speed
end
end
end
jmetaxas29
Scratcher
83 posts

Slope detection in platformer

DD-8861 wrote:

The problem here is that you have a “stop this script” block inside your movement loop, that stops you from moving when you are touching ‘level’.

Because the slopes are also part of the level, it stops your movement when you run up a slope too. But you probably only want it to stop the motion when you touch a wall.

Your current code looks like this:

define Change x by: (x)
repeat ([abs v] of (x))
change x by (...)
if <touching [level v]?> then
set [slope v] to [0]
repeat until <<(slope) = [8]> or <not <touching [level v] ?>>>
change [slope v] by (1)
change y by (1)
end
if <(slope) = [8]> then
... // Inside this 'if statement' is what happens when you touch a wall
end

set [slope v] to [0] //Now these two blocks happen anytime you touch level.
stop [this script v] //not only for touching walls, but also slopes. So touching a slope stops your movement.
end
end

To fix this, make it only stops your movement if you touch a wall. Simply move the ‘stop this script’ to be inside the if statement:

define Change x by: (x)
repeat ([abs v] of (x))
change x by (...)
if <touching [level v]?> then
set [slope v] to [0]
repeat until <<(slope) = [8]> or <not <touching [level v] ?>>>
change [slope v] by (1)
change y by (1)
end
if <(slope) = [8]> then
... // Inside this 'if statement' is what happens when you touch a wall
set [slope v] to [0]
stop [this script v] // Now it only stops when touching a wall, so you can run up slopes at normal speed
end
end
end
Thanks a LOT I will give credit to you in the game (when its released)

Powered by DjangoBB