Unity 2D Platformer Tutorial 19 - Respawning the player

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi in this video i'm going to show you how to respawn the player in a 2d platformer game in unity i've been working on this game in unity this platformer game and i can move around the scene i can jump from one platform to another but as soon as i fall off a platform that's it i can't get back onto the platforms again and continue the game i can't respawn i can still move around left and right but the player is basically just continuously just falling through space so what i want to do is have once the player falls off a platform shortly after that i want the player to respawn back to the very first platform that started on so what we'll need to do is add some collision detection to the game so we can set up an empty object below the scene that's going to act as a full detector and when the player goes through that full full detector or hits it a collision is going to be detected and when that happens the player will respawn back to the starting point so we'll need to write some code to detect that collision but uh what we'll need to do first is set up something that can be used to detect when the player has fallen off the platforms so what i'm going to do is in the hierarchy here create an empty object so right click create empty and i'm going to call this empty object full detector okay and what i'm going to do is just position it using the move tool oops i'm going to position that below the player and i'm going to add a collider so i'll go add component physics 2d and i'm going to use a box collider 2d for this i'm going to edit this collider and i'm going to just drag the edges out so it's nice and wide like that and uh if this was a small scene and the player couldn't move too far left and right then what you could do is just drag out the collider to the width of the scene basically so no matter where the player is in the scene when they fall they're going to touch this collider and then respawn but my scene is quite wide and i'm going to keep adding more and more to the scene so what i'll do instead is with code i'll just make this collider move left and right with the player so wherever the player moves the collider is going to be following underneath the the scene there okay so we'll look at how to do that a little bit later so i don't need the collider to really be that big uh if i'm going to make it move underneath the player so about that size should do okay what i'll also need to do is tick this box here where it says is trigger and what that means is instead of the player falling and landing on this collider or hitbox it's going to be able to pass through it and then we'll be able to detect that collision we'll be able to detect when the player has passed through the collider or entered the collider okay um next thing we'll need to do is we need to tag this collider because in this scene what we're going to do what i'm going to show you how to do later is how to detect when the player has hit things like spikes so we can take off health or reduce the score or when the player has collected different objects like crystals or maybe when the player is touching a ladder or a box or a checkpoint and that kind of thing so we're going to have to detect different types of collision or collision with different types of objects and we want to distinguish this full detector from other objects that the player might be able to collide with so what we'll do is create tags so we can tag different objects and when the player collides with an object we can just check what tag that object has so if you select the fall detector you can go to tag and then from that drop-down list click add tag i'm going to create a new tag and i'm going to call this tag full detector and a tag can be applied to more than just one object as well so i'm going to save that tag and once i've added that to my list of tags i just need to go back to the full detect object and apply that so i need to select that tag so now it's tagged fall detector okay so that's all that we need to do to set up the scene now we need to add some code so i'm going to go into the player controller script which is attached to the player all right so this is the code that's controlling all the the movement of the player and animations for the player and what i'm going to do is i need to create two variables two new variables so public vector three respawn point and actually this can be a private variable so private vector3 respawn point and then a public variable public game object full detector so we'll call that one okay so this first variable here respawn point that's going to basically record the position of where the player starts off in the game so if the player dies they'll respawn back to that position so we'll use this to just store the position of where we want the player to to respawn to the second variable here is going to uh link this script to the full detector that's in the scene so that we can track it or so we can basically um change its position to follow the player on the x-axis so if the player moves left and right then the full detector will move left and right with it so we'll need those two variables okay in the start method here what we'll need to do is just specify the position of the respawn point at the start of the game so to do that say respawn point equals transform dot position and because this start method only runs at the start of the game before the very first frame update what's going to happen is the respawn point variable is going to store the position of the player which this script is attached to before the first frame so basically wherever the player is at the very start of the game that position is going to be stored in this variable and then we can use that later to when the player falls to respawn the player back to that position okay so going down to the update method what i'm going to do is add a line of code here that's going to be used to move the full detector and it basically its position will match the position of the player on the x-axis so we'll do that by saying full detector equals oh sorry full detector dot transform dot position equals new vector two okay so the position of the fall detector is going to be set to transform dot position dot x so that's the player's position on the x axis and on the y axis it's not going to change at all it's going to stay where wherever it is on the y axis okay and just end that line with a semicolon okay so the fall detector's position is going to be changed uh we're going to use a new vector 2 here this is going to be the x coordinate of the full detector and this is going to be the y coordinate of the full detector the x coordinate will be the position of what this script is attached to which is the player object okay so it's going to follow the player on the x-axis but on the y-axis it's just going to keep its current position so it's not going to move up or down with the player we wouldn't want that to happen because if the player falls then the fall detector would be falling as well below the player and the player would never actually hit the fall detector so we don't want that okay oops just don't need two semicolons there all right now to get this collision detection working we're going to need to create a new method and this method is called ontrigger enter 2d and it's used to detect a collision when one object enters another object's collider okay so to do that we'll type in below the update method void on trigger enter 2d okay and we can just double click that to auto complete that so we've got a new method here called ontrigger enter 2d and then in the parenthesis here we'll see collider 2d collision okay so what this is going to do is this this method is going to be called whenever a collision is detected whenever the player which this script is attached to has entered another collider and we're going to refer to that other collider as collision so then we can just check if that other object's tag is the full detector if it's tagged full detector and if so change the position of the player back to the respawn point so this method is going to run whenever uh the player enters the collider of another object and so now what we need to do is just check if the tag of that other object is equal to full detector so if collision dot tag equals full detector transform dot position so this is the position of the player set that to the respawn point and remember the respawn point was initialized up here at the start of the game to the initial position of the player okay so there are a few different types of methods for collision there's we can detect collision when one object enters a collider of another object or when it's staying inside the collider of another object or when it's left the collider of another object okay so there's a few different types of collision that we can detect in this case we want to detect when the player is entering the fall detector or passing through it and when that happens we'll check whether that other object that we've collided with whether it is tagged full detector and if so that means that the players hit the full detector below the scene and so we'll change its position to the respawn point that's basically how it works so you can save that script and then go back to unity okay so what we'll need to do first before we can run the game is we'll need to go to the player object and scroll down in the inspector panel to where the script is attached to the player object and that public game object variable that was called fall detector now needs to be linked to the actual fall detector so you can just click and drag in one go default detector object onto that box there all right so now this variable fall detector this variable here fall detector is now linked to this object here all right let's run the game all right now if i play off this fall off this platform you can see straight away i respawn and if i move further along the scene past the point of where that full detector originally was i still respawn okay because the the full detector is moving underneath the player uh following it on the x-axis all right so that's it um you could just improve that a little bit just by maybe changing the position on the y-axis of the full detector if the player is re-spawning too quickly so you can play around with that and the next video we'll look at how to add checkpoints so once the player has made a bit of progress in this scene rather than respawning all the way back to the start if they die they can respawn back to a checkpoint so we'll look at how to do that in the next video that's it for this video though thanks for watching
Info
Channel: Daniel Wood
Views: 843
Rating: undefined out of 5
Keywords:
Id: 6ZmRJ_-XcQ8
Channel Id: undefined
Length: 13min 46sec (826 seconds)
Published: Mon Sep 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.