Unity Tutorial: RigidBody Step Up Stairs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

You could have just made an invisible ramp to glide up... that is incredible though... and adorable!

👍︎︎ 2 👤︎︎ u/Relateable7 📅︎︎ Oct 27 2020 🗫︎ replies
Captions
hey guys and welcome to the video so today i decided to make a video on how to solve a key problem you will receive if you choose to create a character controller using a rigid body instead of the character controller built into unity now the character controller can do great things like being able to walk up and down stairs um whereas the rigid body cannot do that for most people the character controller will be fine but me personally and for many others sometimes either a rigid body just makes more sense for the style of game or you just prefer to work with that as it's easier to code with and it's what you're familiar with so what i'm going to show you how to do now is how to use a rigid body character and still be able to go up and down stairs now most people's solutions tend to be just going up and down ramps and now that's all well and good um but sometimes you actually need to be able to go up and down stairs so i've got three stair examples here uh i've used pro builds to make these so for example this stair over here i've made it convex so that it is basically like a ramp so with a character controller without any additions you will be able to go up and down this uh these set of stairs are regular steps and i've got some sprite stairs here which is where i'll show that just making it convex doesn't quite do what you would want so that's not an option so if you've got any kind of curve to your stairs then you're not going to be able to do the round trick and then there's these blocks and i'll show that these blocks will actually block your movement except for the small one here and that is not ideal so if i show you what i mean i've currently got the tutorial unfinished script on and i can show you that as you run into these simple blocks you just get called on them now this isn't ideal and you don't want to have to go and create a little ramp for every little object so if i show you what i'm going to have complete by the end of this tutorial just load up the completed one and you can see that as i run into it you run over it and you can run up and down stairs if we go over to these stairs here regular stairs you can go up and down these and to show you that it also still works on ramps as well you can still go up and down ramps just fine so if we go back to the tutorial script where it's broken i can now show you just how it is we do that okay so basically the general idea is we're going to use two raycast points to check if we're colliding with an object and if it's something we should be able to step over so the idea is we're going to create a raycast point at the bottom of the feet and a raycast point say near the knees to determine whether you should be able to climb this or not right so the easiest way to represent where these raycasts are going to be are just two empty game objects so i've created step ray lower and step ray upper these two game objects are where the ray casts are going to come from the upper one we will determine in script exactly where that's going to be but the lower one you kind of just want it to be right down to the bottom not quite on the floor but just above so the general idea is going to be that if we're colliding with the low array we check to see if we also collide with the up array if we do then we don't do anything we're just walking into something that's too high if we don't collide with the up array then it's something we should be able to step over so if we go back and go into our script here we can make a start so we're first going to want to create some solid variables first of all we want to capture the empty game objects we've created for our raycast points uh i've defined a step height float and a step smooth the step height is how high we're gonna want our character to be able to climb in a simple step and the step smooth is what will sort of smooth our transition up the step rather than making it look too jolty and it'll be how high we step up basically so first of all if we save that and go back as you can see when this reloads in we'll have our new variables and so i've i've got them set to serialize fields but they are private so they can't be accessed anywhere else but we still have access to them in the editor so the first thing we're going to want to do is define our step lower and step higher heights here's where you can change the values if you're going to need to okay so the next stage is to take the step ray upper game object that we have defined take its position and set its height to be the step height that we set in the inspector this will allow us to adjust exactly how high and how tall the steps can be that allow us to climb them okay so with that in place it's time for us to actually create the function that we're going to use to define the raycast points and step up on the steps so we'll do that by creating a new function right when we have this new function we're going to want to have this being called in our fixed update function so there you go that's ready to be used now basically what we're going to do is we're going to create a raycast first of all we want to check for the lower raycast as i was saying we're going to create a new raycast point it's going to stem from the game object position that we've defined it's going to move outwards uh look for an object that i can hit and the distance uh you can play around with these as much as you want but i find that 0.1 uh is a pretty good distance it doesn't go out too far so you've got to be right up against the object for you to recognize so the second stage is to put our second raycast inside that if statement now the reason we're doing that is because it limits how many raycast checks we're going to be doing so while we're running around the world and we're not colliding with anything we're not entering the if statement so we're only ever actually sending out this one raycast if that raycast collides with something then we know we're at an object and we want to check so this is where we'll create the second raycast the upper one and it basically works the same um we are taking its position from the game object sending out forward from the character's position but we do want to send it out a little bit further this will allow us to step over anything that is slanted ever so slightly away from us if they were said to be exactly the same then potentially an object could be slightly slanted away from you not enough for it to be a ramp and you should be able to walk up but enough of the upper raycast wouldn't detect it and then you might not be able to step up it so if you make sure that one sends out a little bit further than any slightly slanted object you also climb above we want to set this to not because what we basically want to check is if we are currently colliding with an object we want to make sure that the object isn't tall enough for us to stop us completely so we need to make sure that this raycast isn't hitting anything if that's the case then what we're basically going to do is we're going to take the rigid body of our character and literally just take the position and minus it by our step smooth amount so that's why we have the steps from the mouth the higher the step smooth the more your character will jump up now because this is being done every update you can set it to a really low value and it will smoothly move your character up so if we take that and save then what we can do give it a quick test and there you go as you can see we bump into the object and we move up and step onto it now it's not completely perfect at the moment for example if i go at an angle you can see that you kind of get stuck you don't quite go up things but in general we can move up and down the stairs so let's fix the angle so what i've done here is basically replicated the raycast code from our forward and i've set at a 45 degree angle and a minus 45 degree angle so we now have three ray casts going out they will check in front 45 degrees to the left 45 degrees to the right and they all work exactly the same if any of them detect any kind of object then they will set they send their upper raycasts out and see if there's anything that you can collide with at all there so if we go back we hit play then you should be able to see that now even if we're going a bit of an angle we can still go up and down the object perfectly fine um you as you can see you can go downstairs it still works perfect and also we can still go up and down ramps so that works fine as well and more importantly these little blocks to get in the way you can go up and down these and you smoothly go up and down so to show you what some of these settings do so the smoothing is how high up you go so if we were to set this to some wild value like one then you would see that basically when we go over something we jump up in the air way too much and that's being done for every physics update that you are colliding so if we set it down to 0.01 and go in the opposite direction and make it really small you'll see we'll climb up much slower because every physics update we're only jumping up a tiny amount now that's too low because it kind of slows the character down and it really feels like he's struggling to go up them it looks even worse on stairs because you hit the stair before you actually get up there but something like 0.1 works quite well and you kind of just sort of you look like you almost just step onto the top of it and you go straight over it as for the step height if i show you that i cannot climb up the two double stairs there i can't go up there but the single stair i can if i was to set this to say one then that would make the step height really tall and i would be able to step up onto the double stairs there maybe even onto the triple stairs i can't i just about make it up that but i can't make it up there if you were to make it say 100 something really ludicrous then you can see the whole platform in the background i would also be able to just climb straight up so if we go we go straight up it as you can see it can might take a little bit of time but in general you should climb right up it now this works very similar to the character controller um there's some slight differences it may not be the most optimal but it's a clean simple solution that i have personally found to be able to climb up and down the stairs and more importantly any kind of rocks or curbs you may have in your game it allows you to go up them without having to take every single object and create some kind of ramp for it you can just have a box as long as it's low enough and you set your step height to enough then you can go up and down it so uh there you go that's how to climb up and downstairs with a rigid body in unity uh thanks for watching the video uh like the video if you liked my solution um if you've got any suggestions for better ways to do it then please let people know down in the comments and if i find your solution better than mine i might even heart it and pin the comments so other people can see that one instead thank you very much guys and see you next time
Info
Channel: DawnsCrow Games
Views: 17,779
Rating: undefined out of 5
Keywords:
Id: DrFk5Q_IwG0
Channel Id: undefined
Length: 12min 27sec (747 seconds)
Published: Mon Oct 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.