Wall Jump in Unity - 2D Character Controller

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you can find the code for this video in my GitHub page Link in the description select the part to Branch if you want to write the code for this tutorial yourself or depart free otherwise the first thing that we are going to do is to add some platforms to the scene so that we can test the new abilities but before doing that let's put in order the year Key by creating some empty game objects named after the different categories of objects that we have in the scene one for the cameras another 4D platforms and the last one for the characters now just drag the game objects into the appropriate category we can finally create the new platforms start by duplicating the platform fast game object and rename it to wallet change the X position to -10.5 Y to 0 and the Z rotation to 90. duplicate the wall left and rename it to war right change the X position to -6.5 y to 1.5 and the axis scale to 7. now for the last wall duplicate the wall right and rename it to wallely change the X position to minus 0.5 y to 2 and the X scale to 6. the last thing that we need to do is to create two platforms that will act as the ground on top start by duplicating the platform fast and adding to its name top left change the X position to -16 and Y to 4.5 then duplicate the top left platform rename it to topped right and change the X position to 5. move the character X position 2 minus 11.5 and the Y position to 5.5 to place the character on top of the platforms finally drag the newly created platforms inside the platforms category it's time to get into the code go into scripts checks and let's rename the ground script for more generic name like Collision data retriever open the script and change the class name to match the file name then we are going to make the normal variable public rename it to contact normal and make it a field variable so that we can use it in other scripts and won't show up in the inspector same as the on-ground variable we are going to make a field for the wall detection in the evaluate Collision method we are going to check if the contact normal is above a certain threshold but this time we are going to take the X component of the contact normal and use the absolute value of it to get rid of the sign then in the on collision exit we reset the value and finally we make the evaluate Collision method public since we are going to use it in other Scripts back in the unity editor go in the capabilities folder and create a new C sharp script named world interactor this script will be in charge of detecting which interactions should be played when in contact with a wall open the script and let's put the code inside the usual namespace you can change the name of the 9 speed using this project with one of your choice then let's start dividing each wall interaction in its own category we will start from the wall slide interaction create a new float variable that will represent the maximum speed allowed when sliding off a wall the Collision data retriever so that we can actually tell when we are on a wall the characters rigid body so that we can change its speed a vector true for the velocity and a bull so that we can cache the on wall variable of the Collision data retriever then in the start method we are going to get the Collision data receiver and rigid body 2D component now create a fixed update method this is where we are going to make all the physics calculations let's start by cashing out the velocity from the rigid body then we are going to do the same for the on wall variable to make things tidy we are going to create a region named Wall slide this is just a convenient way to wrap out the code that falls within a certain category inside the modular script inside this region we are going to check if we are on a wall and if so if we are going down by checking that the Y velocity is under the threshold we created before if that's the case we set the Y velocity to the wall slide max speed at the end we are going to assign back the cached velocity to the rigid body and that's it for the walls light back in the wall interactor script we are going to create a public bull filled variable so that we can access it from other Scripts create a new header for the wall jump and Below we are going to create a vector true variable that will represent the kind of wall jump that we want to perform the first one will be the wall climb jump and the second one will be the wall bounce job then we are going to add the controller so that we can retrieve inputs our bull to Cache the on ground variable of the Collision data Retriever and another ball to detect when to perform a jump then since we want to know in which side of the wall we are we are going to create a float for the wall Direction X in the start method retrieve the controller component and then in the update we are going to set the jump input only if we are on a wall and not touching the ground in the fixed update we can cache the on ground variable and the X component of the contact normal in the wall Direction X variable this last one will have a value of 1 if we are on the left side and -1 for the right side then create a region for the wall jump and check if a jump was requested if so we compare the negative normal which will be a vector pointing to the wall instead at outwards like it usually is with the movement input if they are the same it means that we are pointing to the wall and we will set the velocity to be equal to the wall climb Vector multiplied by the wall Direction X to get the actual direction we want the jump to be performed then we will set the wall jumping to true and reset the desired jump variable the same logic will be applied for the second wall jump but this time we will only check that no movement input is pressed if so we set the velocity to be the same as the wall bounce vector and set the other variables like before finally we need to make sure that the wall jumping variable is reset if we are on the wall and the velocity X is equal to zero meaning no movement input or wall jump was performed or if we are just on the ground as you can see now if you run the game you'll be able to perform different wall jumps depending on which movement input you are performing while wall jumping but there is a problem we need to address which is if we wall jump the Y component of the Velocity still increases while on the wall making this weird slide upward movement to solve this problem we are going to use the on collision entered 2D method use the evaluate Collision method we made public and check if we are on a wall not touching the ground and we are performing a wall jump then reset the velocity Vector back to zero with this track if you now Run the game you'll see that the rear sliding upward movement is sold the last jump that you are going to add is the wall leap jump and that will be used to go across distant walls much like the others we are going to create a vector true for it named wall jump leap then we'll add an else statement to include the last input case which is the input that points out of the wall and we assign the wall jump Leaf Vector this time like with the other variables used before but there is a problem here if we try to perform a wool jump leap sometimes we press the movement input before the jump one and so we fall from the wall before actually having the chance to wall jump we can solve this issue by blocking the player's input for a small period of time before falling off the wall go in the move script and add a float which will represent the amount of time before we fall from the wall and another float variable which will be used as the counter for it then we will create an object for the world interactor so that we can retrieve when on a wall and make sure that the Collision data retriever object's name is changed accordingly you might still add the old one in other Scripts once you have done that create a region named wall stick and inside it check if we are on a wall not touching the ground and not wall jumping which means that you are sliding off a wall then we check if there is still time left in the counter and if so we set the x velocity to zero blocking horizontal movement then we check for movements that point out of the wall and decrease the timer while when there is no moment we reset the counter this way the movement will be blocked until the counter reaches zero and reset when on the wall but not performing movements that will get the character out of the wall finally to actually make sure that the counter is set we add an else statement so that the counter is always set back to its maximum value to put definition touch let's copy the required component attribute from the move class to the wall interactor one in this video I wanted to add a bonus section to reword those who have followed so far into the video and in this segment we are going to cover the ability to control enemies duplicate the players character name it enemy and change its color when you play now you'll see that you have control over both characters but let's say that you only want to control the enemy so you want to switch from controlling the player to the enemy so we try to set the player's controller to noun so that we can control just the enemy but these gives us a problem you can see that in the console we now have a null reference exception and that is to be expected since we have to set the cartridge controller to known to solve this problem you might use an if statement that just checks for null controllers but we can do better we can use the null object pattern which is in simple terms an object that produces no actions in our case this object will be a controller that does nothing we can name such controller empty control inherit from the input controller class and implement the methods by returning values that will produce no actions like faults for the jump hold and jump and zero for the movement remember to create these creatable objects for it and that's it with this simple trick you can now swap controllers without having to worry about incurring internal reference exceptions every time that you want to control another character you just set the empty controller to it
Info
Channel: Shinjingi
Views: 8,590
Rating: undefined out of 5
Keywords: unity, unity3d, beginner, easy, how, to, howto, learn, course, series, tutorial, tutorials, game, development, develop, games, move, movement, moving, character, controller, character controller, control, player
Id: L7ePgYgqii4
Channel Id: undefined
Length: 13min 41sec (821 seconds)
Published: Fri Oct 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.