Discuss Scratch

Abdi81
New Scratcher
2 posts

need help with adding gravity and platforms

Im trying to make a Brawlhalla/Smash Bros esque game but cant figure out how to make the sprites not go through the wall and land on the platforms.


https://scratch.mit.edu/projects/1028272084
FreezingNightScreech
Scratcher
31 posts

need help with adding gravity and platforms

I will respond to your question but I will take time to type all this out, also sorry for the late response but I just looked at the posts today
NIKI-KOLCHAGOV
Scratcher
500+ posts

need help with adding gravity and platforms

Well gravity isnt that hard to make depending on how realistic you want it
Basically when the player is not touching the ground you want to move him down until it touches the floor again you also would want some more physics like potential energy
Basically the higher it is the faster it falls

Heres a simple script to try for the players sprite

when green flag clicked
forever
if <not <touching [ground v] ?>> then
change y by (((y position) - (y position of ground)) * ((9.8) * (mass of player))) //the first one is the height between the ground and the player the second one is for the gravity how strong it is and the third one is for the mass larger things fall faster and are more attracted to the ground by the gravity
end
end

You probably should change the amounts to fit your needs
And i think it should work

Last edited by NIKI-KOLCHAGOV (June 18, 2024 11:54:19)

FreezingNightScreech
Scratcher
31 posts

need help with adding gravity and platforms

Firstly, For gravity to exist, you need to have a sprite that acts as the platforms, you can do this in 2 ways, either by:

(Method 1) simply making the sprite one costume and the background will be the backdrop(to avoid continuous touching of the sprite),Ex:
when green flag clicked
forever
wait until <<not <touching [Platform] ?>> and <not <(Status) = [Stunned]>>>
repeat until <<touching [Platform] ?> or <(Status) = [Stunned]>>
change y by (-5)
end
end
I added the status=Stunned because as I remember (Not sure never played smash bros before), When you get hit and stunned you don't fall down(gravity ceases to exist :lol so that is why I added it

Or

(Method 2) You can create multiple costumes or one costume and use variable cloning(that's what I call it), Ex:
when green flag clicked
set [Platform Positioner] to [0]
repeat (3)
create clone of [Myself]
wait (0.1) secs
change [Platform Positioner] by (1)
end

when I start as a clone
if <(Platform Positioner) = [0]> then
switch costume to [Platform No.1]
go to x: (50) y: (-20)
else
if <(Platform Positioner) = [1]> then
switch costume to [Platform No.2]
go to x: (-60) y: (50)
else
switch costume to [Platform No.3]
go to x: (0) y: (-90)
end
end

and don't forget this script too
when green flag clicked
forever
wait until <<not <touching [Platform] ?>> and <not <(Status) = [Stunned]>>>
repeat until <<touching [Platform] ?> or <(Status) = [Stunned]>>
change y by (-5)
end
end

If something wrong happens with your project let me know, because I might have forgotten something, but hopefully not
FreezingNightScreech
Scratcher
31 posts

need help with adding gravity and platforms

You can also make it more realistic if you want by changing the Y direction according to the time spent in the air(this is not reliable though, I think it won't fit well with a super smash bros-like game, but you could give it a try)
when green flag clicked
forever
wait until <<not <touching [Platform] ?>> and <not <(Status) = [Stunned]>>>
repeat until <<touching [Platform] ?> or <(Status) = [Stunned]>>
wait (0.15) secs
change [Gravitational force] by (-1)
end
set [Gravitational force] to [0]
end
and use the last script in the message before this one
when green flag clicked
forever
wait until <<not <touching [Platform] ?>> and <not <(Status) = [Stunned]>>>
repeat until <<touching [Platform] ?> or <(Status) = [Stunned]>>
change y by ((-5) + (Gravitational force))
end
end

Last edited by FreezingNightScreech (June 18, 2024 12:41:04)

FreezingNightScreech
Scratcher
31 posts

need help with adding gravity and platforms

This should help you with the wall touching bug :

https://scratch.mit.edu/projects/1038960623

Also I advice you to make the walls in the platform sprite
Dagriffpatchfan
Scratcher
1000+ posts

need help with adding gravity and platforms

Groffpatchs has a helpful tutorial on this

Powered by DjangoBB