[Unity] Creating a 2D Platformer (E10. variable jump height)

preview_player
Показать описание
Learn how to create a 2D platformer controller in Unity that can reliably handle slopes and moving platforms.

Or a once-off donation through PayPal:
Рекомендации по теме
Комментарии
Автор

FOR ANYONE WHO'S JUMP STOPPED.


You need to set the minJumpHeight in the editor and the maxJumpHeight since it got reset when it was renamed.
At 3:24, there's a jump in the video, so he had the same problem and during the cut he set those values.

nygamery
Автор

Thank you very much for this series. I've been looking into trying to make an indie 2 & 1/2D adventure platformer and so this tutorial series was a great example of all the basic physics setups I needed to get started.

For my project I named the "through" Tag "HollowPlat" since it represents a platform that is also hollow in some senses. Additionally I made the fall through command "Down + Jump" as many classic platformers used that method (Contra for example). I also added an additional Tag for platforms that have no horizontal collision as well, as many platformers feature such.

I guess if there was something I'd suggest for the series it would a dash command/run button.

mresturk
Автор

To add a button press required to fall through a platform, one can simply add the input:
if (playerInput.y == -1 && Input.GetButtonDown ("Fire2")) {

in this example, down + Fire2 will allow me to pass through the platform; That way we don't plummet to our deaths if we also use the "down" function to crouch.

Scratch-Moded
Автор

Here's a better way to do falling through platforms;

1. Change the bool fallingThroughPlatform to a Collider2D fallThroughPlatform.
2. Store hit.collider in instead of changing the boolean to true, when playerInput.y == -1.
3. Check if hit.collider is equal to collisions.fallThroughPlatform, instead of checking if you are falling through a platform.
4. Remove the resetFallingThroughPlatform function entirely, and the invoke to it.
5. Set to OUTSIDE of the hit.collider.tag statement, but instead the hit statement. Right in where you're setting your velocity.y

There's a pretty glaring issue this will solve, and I (believe) it's ever so slightly better on performance, as well, since there's no Invoke. But that's rather negligible. Though it will take up ever so slightly more memory, but whatever.

What Sebastian's method will do, is make you fall through ANY platform with the "Through" tag, while you have the fallingThroughPlatform boolean set to true. Meaning if you have two platforms close to each other, you'll fall through both.

To avoid this, you need to set the time in Invoke to a very small number. However; unless you have a long enough time, you won't be able to fall through platforms moving downwards quickly. If this is the function you want, then stick with what he showed you.

However; with my method, it will store the platform you actually jumped down through, and only let you fall through that. And when you touch any ground that isn't that platform, it will enable collision with that platform again.

zantoxu
Автор

This was awesome! Great tutorial as ever. Climbing, Climb off/on as more people has asked for would be great. Both for walls like "Ninja Gaiden" and ladders like "Mega Man" Roof climbing and hanging? Maybe the same thing. Later on maybe play with some "In-Water physics/effects" You're the greatest! Keep up the good work mate!

BakaRecca
Автор

Thanks so much for these tutorials, the best on YouTube.

GameCircleUK
Автор

Man your videos are so great so far, the best for beginners I've seen. But I am a coding noob and I will need help for what I want to make. It is a variation of the jump, where you can charge your jump with the set mechanic: upon pressing the W and S keys (or up and down) at the same time you jump X%, higher then normal, the longer you hold them (up to a cap of course).

Thanks for the help in advance, also excuse any mistakes I've made, inglish isn't my mother language.

PS. So far my code is up to date with the code from the this video. GREAT WORK SO FAR!

jrtyjeuetyue
Автор

Hi there!
I really like this series. I'm a programmer and my dream is to make a video game someday before I kick the bucket. This definitively got me one step closer. Thanks a bunch. I added one little detail to this lesson. Instead of falling thru the platform by just pressing down, I changed it so it falls thru if you press down + jump.

in the Player class right after the wall sliding check:


    if(input.y != -1){
        velocity.y = maxJumpVelocity;
    }
}

and in the Controller2D

if(playerInput.y == -1 && Input.GetKey(KeyCode.Space)){
    continue;


I'd like you to complete the series by adding a lesson about animated sprites.

arturorojas
Автор

As a physicist I have to say that the words velocity, distance and force are used quite randomly here :D
At 3:20 for example velocity.y is set to minJumpHeight which is contradictionary. Also your controller.Move Method would require another factor 1/2 which comes from integrating two times over time.
Otherwise awesome tutorial!
Greets Nick

nickjung
Автор

wow nice !  maybe you'll do ladder climbing (climb, jump on ladder, off the ladder) and thanks for these great tutorials

LorenMcKiro
Автор

Oh, my god, the code is successfully coupled as hell!

israelg
Автор

Sir pls do a ladder climbing (climb, jump on ladder, off the ladder) video tutorial and thanks for these great tutorials.

johncruel
Автор

@Sebastian Lague, thank you so much for the series. I have feedback on the variable jump height: with this method, when tapping the jump button it gives inconsistent jump results (sometimes the arc of the jump is lower, sometimes higher) and that leaves you with a really bad feeling. Last time I coded a min jump height I did it by checking "Jump button UP" and the player's "velocity.y was less than a threshold" in fixed update, and it worked consistently. I was wondering if there's a fix to this with this method, since I like this approach that is more intuitive to set up.

guille_sanchez
Автор

I love your codes man :D you are helping me a lot THANKS MAN :D

eziostone
Автор

for anyone who got stuck in a downwards moving platform while jumping through: just set the min jump height to something like 0.2

arfa
Автор

In the portion of the PlatformController method where you're determining the passenger movement if the platform is moving downwards, rather than have

if(hit && hit.distance != 0)

instead, I left it as
if(hit)
but later, when defining whether isStandingOnPlatform, instead of simply putting true, I had
(hit.distance == 0)?false:true

this made it so that when trying to fall through a platform that's moving down, I wouldn't abruptly lose the downward velocity that that platform was contributing and made the whole movement a lot smoother, especially for platforms with eased movement

then, in order to stop the platform from slowing me down while trying to jump up onto it from beneath it while it's moving downwards, I added a isJumping bool to the collisionInfo struct
and when PlatformController goes to move its passengers, it check && this.tag == = 0;}

goseigentwitch
Автор

9:37 Why use input.y == -1 and not input.y < 0? I believe you don't need to hold down if you use the latter.

Nikolay
Автор

How do I block some of these moves and unlock them as the player progresses in the game?

Some topics to continue the series
1) Sprite animations
2) Level design
3) Enemy behavior
4) External effects like wind, slippery surfaces, trampoline etc

ArsenalStunner
Автор

I also see what Meron Mist reported below. Moving while on a moving platform causes me to fall through. Left, right, and jumping.

budoray
Автор

+Sebastian Lague Hi, I'm doing a small test with your coding, I'm having a problem doing a crouch animation, I can do the animation but I don't know how I can ignore or lower down the ray cast to not collide with walls. Can you help me?

devWithYouAll