The ULTIMATE 2D Character CONTROLLER in UNITY

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you want to follow along with the project you can find it on my github page link in the video description first create a new folder in the project tab named materials in this one create two other folders one named graphics and the other physics go into the graphics one right click select create and select material name the material trail choose a color your choice from the inspector then go into the physics one and right click again select create 2d physics material 2d call it frictionless and set the friction to 0 and the bounciness 2 then create another physics material and call it sticky this time set the friction to 2 and the bounciness to 0. now we can start creating the scene go to the main camera reset its values and change the y position to 5 and with that 1 to -10 then right click in the arrow key select 2d object sprites square change the x position to minus 5 the y to minus 4.5 and the x scale to 10 add the box collider 2d and the rigid body 2d set to static and the rigid body material to frictionless duplicate the game object set the x position to 5 and change the color in the rigid body material set the sticky one then rename the one with changes color platform slow and the white one to platform first create another square sprite change its name to character the x position to -5 the color to red add a rigid body to d with a frictionless material collision detection continues interpolate to interpolate freeze rotation to z then add the trailer renderer component to it with a width of 0.1 material trail and turn off the cast shadows and receive shadows you can disable or remove this component once we've found the proper values for the character controller in the project folder create a new one named scripts and inside it another one named controllers go inside it and create a new script named inputcontroller these will be our abstraction from the actual input source meaning that we can use this class to represent a generic input open the script make the class abstract and instead of mono behavior write scriptable object this will allow us to create instances of the controllers that you can then use in the inspector inside the class create a public abstract method that returns a float named retrieve move input and another one that returns a bool named retrieve jump input go back to the controller folder and create a new script named playercontroller open it and make sure that we can create an instance of it from the menu using the create asset menu attribute change mono behavior to input controller and implement the missing methods in the retrieve jump input we are going to listen for jump button down event and for retrieve move input we will listen for the horizontal axis then go back once again in the controllers folder and create a new script named ai controller in this script we will take the input from another source the script is very similar to the playercontroller one but this time the filename and menu name are changed and the return value of both methods too we are not going to make any calculation we just want to simulate an ai input it is now possible to create discretable objects in the asset menu but before that let's create a new folder named scriptable objects and inside it another one named controllers right click and select create you will see a new option in the asset menu named input controller and inside it the player ndi controller select the player controller and then again the ai one we're going to assign them once the movement component is being created and before doing that we need a component that determines if we are on the ground or in the air so create a new folder in scripts named checks and inside it a new script named ground inside it create a private bull on ground and a private float friction then we need a way to determine if the point the game object is colliding with is the ground to do that we iterate through each contact point and check if the normal is greater than 0.9 one being a flat surface we then set the result to the on ground variable to set the friction instead we retrieve the physics material through the collision set the friction to zero so that if no physics material is being used we still get a value from this method and if the material is not null we set the friction value to the one specified in the material now that we have methods to calculate if the character is on the ground and the ground friction we can use the on collision enter 2d and on collision stay 2d to evaluate the collisions and get the ground friction and the on collision exit 2d to reset those values finally to make use of the on-ground and friction variables in other components we use the two get methods to retrieve the values the last thing that we need are the actual components that will handle the movement of the character inside the scripts folder create a new one named capabilities and inside it a script named move open the script and start writing the following variables a private input controller named input this will be our generic input a private float named maxspeed with a serialized field and range attribute of 0 to 100 set to 4 then 1 for the max acceleration set to 35 and max error acceleration set to 20. then a vector 2 for the direction the character will move another for the desired velocity the character wants to achieve one for the actual velocity the character is at a rigidbody2d for the game object physics the ground component to determine if the character is on the ground and retrieve the friction a private float for the max speed change which is how fast the speed will increase another float for the acceleration and a blue on ground for the ground check change the start meter to awake and get decomponents for the rigid body and ground from the game object with the get component method in the update retrieve the direction from the generic input we created and calculate the desired velocity by using the retrieved direction and multiplying it by the maximum value between the max speed minus the friction and zero so that we always have a valid velocity that doesn't go below zero then create a fixed update method and inside it retrieve the on ground value from the ground component set the velocity to the current rigidbody velocity set the acceleration based on the on ground variable calculate max speed change by multiplying the acceleration by the time dot delta time and finally calculating the velocity in the x axis with the math f that move towards method which requires the current velocity the desired velocity we want the car to achieve and how much to increase the velocity and finally we assign the velocity to the rigid body velocity now go in the inspector and select the character add the move and ground component in the input property select the player controller in the main camera increase the size of 10 and play the game you can adjust the values depending on what you want to your card and feel a higher acceleration will make your character more responsive while a lower one will make your character feel heavy try to find the perfect balance between the two there is just one problem with this approach that we need to solve since the two platforms are made of individual colliders the character may become stuck between the two to solve this issue just add an edge collider to the character modify the y offset to align with the bottom part of your sprite in this case minus 0.5 and in the box collider change the y offset to 0.01 and the y size to 0.98 we have lifted the box collider which was the one getting stuck in the intersection between the two colliders and applied an edge collider which has no sides so it can't get stuck the last thing to do is to code the jump so go back in the project tab and under scripts capabilities create a new script named jump open it and write the following variables as in the move component we need the generic input we created and a series of variables such as the jump height the max air jumps which is the number of jumps we are able to do once in the air the downward movement multiplier this is the value the gravity scale the character will be changed to in order to increase how fast the character will fall the upward movement multiplier instead is how fast the character will reach its peak then we need a rigid body to apply the jump velocity the ground component to detect the ground a vector true for the velocity and hint for the jump phase to track how many times we've jumped a float for the default gravity scale so that we can always revert back to the original one and two booleans to track if we want to jump and if we are on the ground then change the start method to wake retrieve the body and ground components and set the default gravity scale to a value of your choice this is the value that will be applied when the character is on the ground in the update method we retrieve the jump input and store it by using the or operator so that it remains set even in new update cycle until we manually set it to false create a new method that will perform the jump with all its logic we first check if we are on the ground or we still have our jumps to perform we keep track of the jump phase and calculate the jump speed by taking into consideration the formula for the jump height which uses the gravity and the desired height we want to achieve then we check that the jump speed never goes negative because of the gravity and add it to the velocity now in the fixed update we retrieve the ground set the velocity to the current velocity of the rigid body if the cart is on the ground we reset the jump phase variable then we check if a jump is being requested if so we set the request back to false and trigger the jump now that we are in the air we apply different gravity scales depending on the actual phase of the jump if we are going up we apply the upward movement multiplier if we are going down we apply the downward movement multiplier and if we are on the ground we revert back to the default gravity scale and at the end of the calculations we apply the velocity to the rigid body velocity back in unity remember to add the jump component to the character add the player controller to the input and play the game now by adjusting the max acceleration you can see that when in the air you are able to control more or less the character depending on how high or low the air acceleration is you can easily increase the number of jumps by setting the max air jumps in inspector and control how fast or slow you go up or down with the upward and downward multipliers you can also change the input controller by playing to make the character be controlled by the ai one last thing that i want to mention is why we applied a physics material to the character since we already have them for the platforms and the answer is that if we add another platform by duplicating this low one and place it above it if you remove the frictionless material from the character rigid body when trying to jump on the platform you'll get stuck in the side whereas with the frictionless material the friction gets only applied to the movement in the x direction in the code you
Info
Channel: Shinjingi
Views: 61,516
Rating: undefined out of 5
Keywords: unity, unity3d, asset, assets, beginner, easy, how, to, howto, learn, course, series, tutorial, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, move, movement, moving, character, controller, character controller, control, your, player
Id: lcw6nuc2uaU
Channel Id: undefined
Length: 15min 21sec (921 seconds)
Published: Sat Oct 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.