Dash and After Image - 2D Platformer Player Controller - Part 8 [Unity 2019.2.0f1]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys welcome to Barden my name is Heinrich and today we're gonna take a look at creating a super simple - ability that also has a ghosting or afterimage effect Before we jump into unity let's take a look at how we're going to do this when the - button is pushed we'll check to see if we can - and if we can we will set the characters velocity to something high and keep it there for the - duration or until an object gets in the way the afterimage will be created by instantiating game objects on the characters position with the same sprite as the character we will then decrease the Alpha of the afterimage over time to make sure this is done efficiently we will use what is called object pooling while object pooling might be a bit overkill for this use case I feel like this is the perfect opportunity to explore what object pooling is and why it is useful the issue of this comes when we instantiate and destroy game objects doing this takes up a lot of resources and can really slow down our game especially if you're working on a game for a mobile device when we destroy game objects unity needs to take care of clearing the memory again so garbage collection comes into play to get around all of this instead of creating and destroying game objects we will just create a bunch at the start of the game and keep them disabled to the side in a pool until we need them so now as our character moves across the screen when we want to place an afterimage game object we just take one from the pool and place it where we need it then when the time runs out and we need to get rid of it instead of destroying it we will just disable it and put it back in the pool thus we only ever create the objects once and reuse them as we need okay now let's actually jump into unity we'll start by creating the afterimage prefab create a new game object and call it after image reset its transform and then hit add component and add a sprite renderer now let's head into the script folder and create the script for the afterimage prefab so right click and then click on create C sharp script let's just call it player after image sprite then click on the after image game object and drag on the script we just created we can now open up the script go ahead and delete all the pre generated code we're going to need a reference to the player game object to be able to get its position and rotation so let's start off by declaring a private transform and we'll call it player next we need a reference to the sprite renderer on this afterimage game object so let's create a private sprite renderer and we'll just call it SR we also need a reference to the player game object sprite renderer so that we can get the current sprite so let's declare another private sprite renderer and let's just call it player SR we want to decrease the Alpha of the sprite over time which means we need to make changes to the color setting so let's create a private color and just call it color we also want to say how long this game object should be active for and we also want to keep track of how long it has been active so let's declare a private float active time and just set it to 0.1 by default and then we'll declare a private float I'm activated we also want to keep track of what the Alpha currently is so let's create a private float and call it alpha and we also want to set the Alpha to something when we enable the game object so let's create a private float alpha set we can just set this one to 0.8 by default next we want to decrease the alpha over time so I'm going to create a private float alpha multiplier and just set it to 0.85 by default the smaller this number is the faster the sprite will fade if we want to be able to change active time and alpha set in the inspector all we need to do is in square brackets right serialised field above each variable either than do this but you can also write this above alpha multiplier if you want to mess up how quickly your sprite fades now we're done with the variables let's look at the functions we're going to take advantage of the on enable function which is just like the start or a wake function except it gets called every time we enable the game object in here we will start by getting a reference to the sprite renderer component of this game object so we'll say sr equals get component of type sprite renderer next we will get our reference to the player by saying player equals game object dot find object with tag and the tag is player and then we'll say dot transform to get the transform component make sure you're using the right find game object now just quickly jump back into the editor and go to the player game object and set the tag to player back in the code we can get the reference to our player game objects sprite renderer by saying player SR equals player get component of type sprite renderer next we need to set our alpha equal to alpha set now let's get the correct sprite we'll say s r dot sprite equal player SR sprite we can then set the position our game object to our players position who have transformed our position equals player dot position we can then do the same for the rotation with transform dot rotation equals player dot rotation and then finally for our on enable function we can say time activated equals time dot time now it can work with her update function we'll start off by decreasing the Alpha of alpha x equals alpha multiplier we can then create our new color with color equals new color using the parameters 1 1 1 and alpha next we can set this color to our sprite with s our color equals color and finally we just need to check if this after image has been activated for long enough and so we can say if timer time is greater than or equal to time activated plus active time when this is true we can just add this object back to the pool we'll come back to this line of code later when we've created the pool script so for now we're done with this script let's jump back into the editor in the scripts folder let's create a new script and call it player after image pool before we look at the code I just want to give a shout out to Jason Wyman he created an awesome video talking about object pooling and this is the code I got from that video you can use the card in the top right corner if you want to go watch that video now I highly recommend you do anyways open up the script and just delete the pre generated code again we can start off by writing private game object after image prefab this will be used to store a reference to the prefab we'll be using for the after image go ahead and also make this a serialize field so that we can add the reference in the inspector next we can create a private cue of type game object and we'll call it available objects and set it equal to a new cue of type game object this will be used to store all the objects that we have made that are not currently active next we'll create a super basic singleton that we can use to access this script from our other scripts then we'll look at our private void awake function and inside the function we will say instance equals this to set the reference now let's write the first function will say private void and we'll call it grow pool the purpose of this function is to create more game objects for the pool in this function we'll just create 10 game objects at a time so we'll say for INT equals zero I is less than ten I plus plus and inside the for-loop we can declare a var and call it instance to add a var is a data type which basically tells the compiler to figure out what it should be when it compiles so when we declare it and set it equal to a game object the compiler will know that it should be a game object so var instance to add equals instantiate after image prefab now just for organization we will say instance to add dot transform dot set parent transform this will make the game object we create a child of the game object this script is attached to finally we will call another function will write in a second called add to pool and we will pass this instance to that function next we'll say private void add to pool and this function will take in a game object and we'll just call it instance in the function we will say instance dot setactive false and then we will add the game object to the queue with available objects dot on queue instance and actually this function is supposed to be public so that we can call it from our other scripts via the singleton our next function will be used to get an object from the pool we will call this function from our other scripts instead of instantiate so we can say public game object because this function will return a game object and we'll call it get from pool inside will say if available objects dot count equals zero grow pool this means that if we're trying to get an afterimage object to spawn and they are non available or make some more next we'll save our instance equals available objects DQ this will take an object from the queue next we'll say instance dot set active true when this happens the on enable function will get called in our player after image sprite script then finally we will return the instance will go back to the awake function and just call grow pool once so that some game objects are ready when it's needed now let's go back to the editor create a new folder and call it prefabs go into the folder and drag the afterimage game object we made earlier from the hierarchy to the folder this turns that game object into a prefab now click on the prefab and in the inspector under additional settings change the sorting layer to and the order in layer 2 negative one now let's create a new empty game object and call it player after image pool and reset the transform add the player after his pool script to the object and then dragged the afterimage prefab into the slot next we can set up our dash button so click on edit project settings and go to the input tab increase the size by 1 and go to the last button changed the name to dash and set the positive button to left shift or whatever button you want to use for - and that's it before we head into our playercontroller script let's jump back into the player after image sprite script and finish the update function by typing in the if statement player after image pool instance dot add to pool game object we write this instead of destroying the game object ok now I can finally head into a player controller script and make the dash happen we'll start off by declaring all the variables we're going to need first we'll create a private boolean and call it is dashing this will keep track of if the character is currently dashing or not next we'll say public float - time this is how long the dash should take then we have a public float - speed this is how fast the character should move when it's dashing then we have a public float distance between images this is how far apart the afterimage game object should be placed when dashing then our last public float is our dash cool down this is how long we have to wait before we can dash again now let's go to our private float section and declare a private float - time left this will keep track of how much longer the dash should be happening then we will say private float last image exposition this will keep track of the last x-coordinate we'll replaced an after image then we have a private float last - this will keep track of the last time you start at a dash and will be used to check for the cooldown we'll just set it equal to negative 100 by default so that we can - as soon as the game starts we can now head into our check input function and at the bottom we'll check if the dash button is being pushed by saying if input get button-down - inside the if statement or call a function will write just now called attempt to dash now if we want to only be able to - after cool down will only call this function if time but time is greater than or equal - last - plus - cool down now will write the private void attempted - function inside the function will start by setting is dashing equal to true then we will set - time left equal to - time then we can set last - equal to time time now let's drop our first afterimage to get the game object from the pool will say player after image pool instance get from pool then we can save the exposition of this image with last image X position equals transform to position X now we'll create a private void function called check - this function will be responsible for setting the - velocity and checking if we should be dashing or if we should stop so we'll start off by saying if is dashing to check if we should currently be dashing or not and then inside the if statement we will set can move equal to false since we don't want to be able to control the character while we - then we can set can flip equal to false as well so that we can not flip the character mid - next we'll set the character's velocity to the - velocity with RB dot velocity equals new vector2 of - speed x facing direction for x and RB dot velocity dot y for the y if you don't want the character falling while dashing you can also set the Y to zero next we'll say - time left - equals time dot Delta time now we need to check if enough distance has passed for us to place another after image so we can say if math F dot absolute value transformed position X - last image X position is greater than distance between images inside the if statement will say player after image pool dot instance they'll get from pool and last image X position equals transformed position X now actually all of this is supposed to be in another if statement and that is if - time left is greater than zero so just cut and paste it in there after this if statement we have if - time left is less than or equal to 0 or is touching wall this means that we should no longer be dashing if the time has run out or if we touch a wall then we can say can move equals true and can flip equals true now we just need to go and call check - from our update function and that's it now we can - by pushing the shift-key just like this I hope you found this tutorial helpful and learn something new if you have any suggestions feel free to leave them in the comments down below I have also created a discord server for the channel I'll try to be on it for a bit every day if you need help or if you just want to chat with me or other people in the community about game design the link will be in the description and on my channel banner anyways thanks for watching and have a lovely day
Info
Channel: Bardent
Views: 27,487
Rating: undefined out of 5
Keywords: Unity., tutorial, player, 2D, platformer, walljumping, wall sliding, jumping, Unity, Animation, ground, check, physics2d, castcirlce, variable, jump, height, Wall, Jumping, Hell, Yeah, Movement, improvement, user, friendly, code, 2019, 2019.2.0f1, Ledge, climb, dead, cells, system, easy, beginner, animation, Dash, Ghosting, After, Image, After Image
Id: ylsWcc4IP3E
Channel Id: undefined
Length: 13min 42sec (822 seconds)
Published: Mon Dec 16 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.