Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Basic Platformer Help
- MelonManatee
-
Scratcher
100+ posts
Basic Platformer Help
Hi, how do you make gravity in a platformer? And how do you make it so that the sprite collides with the walls?
- shikma
-
Scratcher
59 posts
Basic Platformer Help
the way to simulate gravity is:
1. create a variable. name it vy. (v for velocity). This variable holds the number of pixels to move the sprite in the Y axis in each frame.
2. on each frame: change the sprite Y position by this variable (vy).
3. on each frame: subtract vy by 1. this simulates gravity
4. on each frmae: if the sprite touches ground (a certain color, or a certain other sprite) set vy to 0.
5. if user jumped, set vy to 10. this simulates a jump.
For example:
frame 1 : sprite y is 50, vy is 0. - the sprite is not moving.
frame 2 : sprite y is 50, but vy is -1 (rule 3). the sprite moves a pixel down (rule 2).
frame 3 : sprite y is 49. vy is -2 (rule 3). the sprite moves down 2 pixels (rule 2). This simulate falling faster and faster.
when the sprite is touching some color, change the vy to 0. This simulates that the sprite is on the ground because it will not fall.
when you want to jump, set vy to 10. This simulates jumping. The sprite will raise, but since vy is subtracted by 1 in each frame, the sprite will gradually stop moving up, start moving back down until it touches ground and vy is set back to 0.
Hope I helped.
You can check how I did it in my platformer game: https://scratch.mit.edu/projects/64636198/
Good luck.
1. create a variable. name it vy. (v for velocity). This variable holds the number of pixels to move the sprite in the Y axis in each frame.
2. on each frame: change the sprite Y position by this variable (vy).
3. on each frame: subtract vy by 1. this simulates gravity
4. on each frmae: if the sprite touches ground (a certain color, or a certain other sprite) set vy to 0.
5. if user jumped, set vy to 10. this simulates a jump.
For example:
frame 1 : sprite y is 50, vy is 0. - the sprite is not moving.
frame 2 : sprite y is 50, but vy is -1 (rule 3). the sprite moves a pixel down (rule 2).
frame 3 : sprite y is 49. vy is -2 (rule 3). the sprite moves down 2 pixels (rule 2). This simulate falling faster and faster.
when the sprite is touching some color, change the vy to 0. This simulates that the sprite is on the ground because it will not fall.
when you want to jump, set vy to 10. This simulates jumping. The sprite will raise, but since vy is subtracted by 1 in each frame, the sprite will gradually stop moving up, start moving back down until it touches ground and vy is set back to 0.
Hope I helped.
You can check how I did it in my platformer game: https://scratch.mit.edu/projects/64636198/
Good luck.
- Sigton
-
Scratcher
1000+ posts
Basic Platformer Help
Another thing I should add is that you must have this in a forever loop, otherwise it will only run one frame. For wall collision you need to sense when you are touching something, and then stop the relevant velocities. So, to stop you continuously falling, you'd add the following blocks to your script:
Sigton
if <touching [whatever v]> then
change Y by ((0) - (vy))
set [vy v] to [0]
end
Sigton
- nitrox_plunge
-
Scratcher
1 post
Basic Platformer Help
Here is how i do platforming engines:
This realistically accelerates an object downwards. Here is a physics engine i made as an example: https://scratch.mit.edu/projects/63923434/
For the walls, just do this:
when green flag clicked
set [Y Velocity] to [0]
forever
change [Y Velocity] by (-8)
change y by (Y Velocity)
if <touching [Sprite Platform] ?> then
change y by ((-1) * (Y Velocity))
set [Y Velocity] to [0]
end
end
This realistically accelerates an object downwards. Here is a physics engine i made as an example: https://scratch.mit.edu/projects/63923434/
For the walls, just do this:
when green flag clicked
set [X Velocity] to [0]
forever
if <key [right] pressed?> then
change [X Velocity] by (2)
change x by (X Velocity)
if <touching [Sprite Platform] ?> then
change x by ((-1)*(X Velocity))
set [X Velocity] to [0]
end
end
Last edited by nitrox_plunge (June 26, 2015 10:06:09)
- BlueFire11111
-
Scratcher
9 posts
Basic Platformer Help
Basically, you need to do something a little like this: https://scratch.mit.edu/projects/78062760/#editor
You make a variable, that is gravity, and a variable that is yV(Y velocity). Make a Script that is like:
You make a variable, that is gravity, and a variable that is yV(Y velocity). Make a Script that is like:
when green flag clickedThen change the variables as wanted, and when you want to jump, set yV to something higher than gravity to jump.
if <not <touching color [#ff0088] ?>> then
change y by (yV)
set [(yV) v] to [ ((yV) - (gravity))]
end
- BlueFire11111
-
Scratcher
9 posts
Basic Platformer Help
Just look at the Instructions of this game:
https://scratch.mit.edu/projects/78062760/
It explains how it works.
https://scratch.mit.edu/projects/78062760/
It explains how it works.
- Discussion Forums
- » Help with Scripts
-
» Basic Platformer Help