HOW TO KILL AND RESPAWN PLAYER-Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
by the end of this video your player will be able to collide with an object and it will kill him and respawn him hey guys Blakey here and welcome back to another video today I've got another unity tutorial for you guys today I'm going to be working on a system which kills the player on trigger enter and ontriggerenter is when you enter an object that has a Collider on with the trigger set to true and I'm going to show you guys exactly what I mean in this nice and easy tutorial that should not take long to learn and master so the way we're going to do it when it collides with this big red object that we've got called kill player it will collide via and it will respawn the scene so instead of teleporting the player it will just restart the whole scene and this is quite useful to use if you've got certain levers or platforms that are moving and you want everything reset every time the player dies which is seen in most games rather than just teleporting the player back to the start of the level if certain objects in the scene needs to be reset so without further ado let's get straight into the video so before we start scripting this we're gonna go ahead and go to our kill player which is just purely a red cube that's all it is and we're gonna go ahead and add a component and that is gonna be a box Collider 2d so this is for a 2d game obviously so if he's using a 3d game you choose box Collider without the 2d and if we go ahead and edit the Collider which is this button right here we don't need to change anything because it's already exactly where we want it to be which is pretty good and the last thing we've got to do is go ahead and press is trigger and we're gonna go and tick that big box and finally before we go ahead and start coding to save you doing this later go ahead to your player which I assume you already have a movement script for if you don't go ahead and check out a video I have made on it previously and all you're gonna do is go to this tag button up here right at the top of your inspector and click play out if you don't have this player tag go ahead and press add tag and you can add one with this little button here ok that is everything we're going to need to do if you see I go ahead and press play it will just over pass this layer and it will not hit it or collide fit that's because our box Collider is set to trigger if it wasn't it would collide and we just hit it but nothing would happen and obviously we want to make it respawn so go ahead and press create c-sharp script and we're going to clear call this kill player just like that go ahead and open this up double-click and it will open visual studios or monodevelop depending on what you're using and you can see we've got a very simple script here so the first thing we are going to do because we're working with scenes we need to add a extra method at the top so we're going to be using using unity engine dot scene management because we are managing scenes go ahead and save that up and now we're going to go to our start function and we're going to leave it completely because nothing is going to be called at the start of the scene because we're going to be using this as a separate void for ontriggerenter so the first thing we're going to do is declare a public integer so go ahead and type in public int and we're gonna call this next scene actually we are going to call this re spawn if I can spell we go go ahead and put a semicolon and we're gonna declare this in unity so the public integer will be whatever it is in your build whatever the scene number isn't your build end it index and I'm going to show you guys what I mean by that so go ahead and ignore the void update and the void start we don't need either of those you can get rid of them if it makes it easier and what we are going to type in is void on trigger in tart 2d just like that and we don't need it to be private so go ahead get rid of that private and we're gonna could change the collider 2d collision you see we're going to change that to other because it's not the subject we're using when we collide with a different object not if we collide if they collide with us so I'll show you guys what I mean so if we type in now if other dot compare tag is player because remember we named our player the plate we gave our player the player tag so that so this line basically means if our if the cube collides with anything that has a tag player then do this function and we're going to use scene manager dot load scene and we're just gonna call the scene that we called right up here so REE spawn we can go ahead and save this so I'm gonna run you through this sort of see we declared a public integer so whatever we call this in our unity it will load that scene because we called it down here so we're gonna add this script to our cube or death cube so from the cube size if it collides with anything that has the tag player it will load the scene that we call which is basically going to be the same scene that went in so we're gonna just call the same scene that were in and it will just respawn to that scene and give the impression of a death so if we go straight back over to unity and we're going to drag our kill player script right over to our kill player and now you're gonna see we're gonna have a little respawn install we have it set to zero and that is actually what we want because this is the first scene for you guys it may be different if you have a main menu script other scenes and stuff like that so if you go up to your file in the top left go ahead and press build settings you can see we've got if you sit and see so it's not everything apart from the scenes and build so we're currently using a sample scene when this that's what they call it but this is our first scene and you can see it says that at the top so this is the scene and you've you've got a number here if we had another scene just below it they'd call it one because everything that is one starts at zero and unity and that's just how coding and programming works with numbers so this scene starts at zero so whatever that number is we want to have as our respawn so obviously that is why ours is just set to zero already so we don't need to change anything so now if you see we go ahead and jump straight into our player I've seen respawns which is a pretty nice feature the minute we hit it we do collide now this can be altered for many ways such as a camera shake and etc so I'll thank you very much for watching this video and I'll see you in the next one bye
Info
Channel: bblakeyyy
Views: 14,837
Rating: undefined out of 5
Keywords: unity respawn player, unity respawn 2d, unity respawn delay, unity respawn system, unity spawn, unity spawn enemies, unity spawning objects, unity spawner, unity spawn object, unity respawn after death, spawn on collision unity, unity checkpoint respawn, unity death and respawn, unity spawn enemy waves, unity spawn every few seconds, unity spawn effect, unity spawn enemy off screen, unity spawning, unity3d respawn
Id: H69PfxOr6bk
Channel Id: undefined
Length: 6min 43sec (403 seconds)
Published: Thu Nov 21 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.