Teleport Your Player Back To Safe Ground | EASY Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome today I'm going to show you how you can warp your player back to the ground after touching a damage Zone this is perfect for Platformers where you have a cliff or poison or lava or something that damages the player when they touch it and then you immediately want to warp them back to Safe ground all games have different needs so I'll show you a couple of ways we can set this up let's get started okay and here's what we're starting off with I have a player that can move and jump you can see his health over here is at five I just put it in there to make it easy and you can see that if he falls too far we take some damage there's obviously another problem where he just keeps on falling forever so we're gonna fix that damage is simply being applied when our player collides with this Edge collider here you can see it over here I have it set to trigger and we have a little fall damage script which really just looks for the player tag grabs the player health component and damages the player very very simple stuff so we are going to create a system that will allow us to warp our player back to a safe location back up on these platforms once he falls and hits this line there are many many ways that we could accomplish this so I'm going to show you in two different ways the first way is simply just to check if our player is safe on the ground somewhere and every couple of seconds just save that location so that one is completely automatic as long as our player is touching the ground and he is safely on the ground then it will store that location and then we will warp him to that location when we damage our player from Fall damage when he touches this Edge collider here that's like a nice Plug and Play type of solution where you make it once and it'll just work forever but that might not be how you want your game to work so I will show you a second way in which we can actually set up multiple checkpoints throughout the level and if we pass that checkpoint then if we fall and damage the player then we will warp back to that checkpoint location so let's get started and we will start with the automatic system that runs every couple of seconds so in my scripts folder I'm going to go into my player and create a new folder called warp to Safe ground let's go in there and let's create a new script called safe ground saver let's add that to our player character and let's open it up let's get rid of all the startup stuff so we know that we're going to want to save our players safe ground location every couple of seconds so let's create a float that will allow us to determine how often so we'll set up a private float called save frequency and we'll default that to three seconds next let's set up a public Vector 3 which is going to be our actual safe ground transform position location [Music] and actually a vector 2 will work just fine since this is a 2d platformer let's call it safe ground location and we're going to set this as a public getter and a private Setter that means we can get this Vector 2 from any other script but we're only able to change this Vector to within this script that's what it means by private set and we're going to default that to a vector 2.0 which and by the way this is exactly the same as writing either way works totally fine so every couple of seconds we are going to be updating this location and I want to do that by setting up a CO routine so let's create a variable for that private Co routine let's call it safe ground Co routine and now let's actually set up the co-routine method so we'll say private ienumerator save ground location so what do we want to do well first we want to just get some sort of timer going then we want to see if the player is actually touching the ground and if he is then we want to update this safe ground location and then we basically just want to run this co-routine again to keep it on a constant Loop so that's the basics of how this is going to work so the timer is going to be very simple let's set up a float called elapsed time we'll default that to zero let's create a while loop while the elapsed time is less than the save frequency well then we'll actually update our timer lapse time plus equals time dot Delta time yield return null so the way a CO routine works and none of this will actually run until we exit this while loop so this is going to keep running and running and running until the elapsed time is not less than say frequency so until more than three seconds is up and then it'll exit this Loop and it'll run all this other stuff down here so we're going to set up a way to determine whether the player is touching the ground or not in another script in just a moment but first let's do the rest of this Co routine so we'll skip this one and go down here and say safe ground location is equal to the transformed opposition because again we put this script on top of the player so if we are touching the ground then we are simply going to update our safe ground location to where the player is currently standing now to restart the co routine we're going to grab this reference up here and say that is equal to start code routine save ground location and we actually need to call this somewhere in order for it to actually run and we're just going to do that in the start function so we can actually copy and paste this here because it's exactly the same now currently when we start the game it's going to run this co-routine and it's going to wait three seconds before it actually updates the save ground location for the first time and that's not really the best because if we happen to fall off of the ledge before our three seconds are up then it's going to warp us to a zero zero location and depending on how your scene is set up that could be like super far away so let's initialize a starting safe position before this has even run for the first time so let's say our safe ground location is equal to our current transform dot position so as long as you have your player starting the scene somewhere safe then that is going to work out just fine so let's actually create a method for determining whether the player is touching the ground or not and we are going to set up another script for this because checking for this is outside the scope of this safe ground saver script this is a totally separate job so we will create a totally separate script to handle that so in scripts in player in warp to Safe ground let's create a new script let's call it ground check and let's drag that onto our player object as well by the way if you do not have a ground layer up here go ahead and create add layer and create a ground layer you're going to need that in just a moment so let's open up this ground check script let's get rid of all the startup stuff so we're going to be checking if the player is touching the ground using a physics boxcast and that is exactly the same as array cast except it creates array casts in the shape of a box so we're going to need to set up a raycast hit 2D variable let's call that ground hit and now let's create a method I want to call it public Bool is grounded so we're going to say our ground hit is equal to physics2d dot boxcast now before we go any further we're going to need to set up a couple of variables in order to get our origin and size I'm going to need a reference to our collider I'm going to want to float to cover our distance here and I'm going to want a layer mask to fill out this layer mask here so let's get our collider first generic collider 2D is fine let's call it cull and get a reference to that in our start function okay so with that we can say that our origin is our call Dot bounds Dot Center and now we can say that our size is our call Dot bounds dot size we don't want an angle so we can throw in a zero what direction do we want well this is a box cast and we want it to be at the player's feet pointing down so we're going to say vector2 dot down and let's create a serialized float to fill this in we'll say serialize field private flow extra height let's default that to something really small like 0.25 let's put that in here and finally for our layer mask let's create a serialized field called private layer mask called what is ground and you can throw that in here so we set this method up as a Bool not a void so we need some return types so let's say if ground hit.coider does not equal no then we will return true else you guessed it return false okay so let's go back into our inspector and set what is ground on our ground check to ground now let's go back into our safe ground saver script let's grab a reference to our ground check script so we'll say private ground check called Ground check let's get a reference to that in our start function and now down here we can finally say if ground check dot is grounded then we will update our safe ground location to our transform.position and now let's actually create a method to warp our player to the safe ground location I'm going to set this up by calling it right here in Fall damage and normally I would not set up that function in safe ground saver I don't really feel like it's the job of this script to actually warp the player but for the sake of keeping this tutorial very simple we'll just create a public method down here called warp player to Safe ground and what is that going to be that's going to be our transform dot position is equal to our safe ground location and finally to test that let's go back to full damage let's grab a reference to our safe ground saver we'll say private safe ground saver called safe ground saver so let's create a start function to actually grab this so we can say safe ground saver is equal to gameobject dot find gameobject with tag we'll say player dot get component saveground saver and then down here we can say safegroundsaver dot warplayer to Safe ground okay so I've been standing here for more than three seconds let's fall off and make sure that we warp back there awesome go over here and stand here for a moment and fall off there we go so that is Method one complete this is completely automatic and now let's see how we could do this with a checkpoint system instead I'm actually going to remove our ground check component as well as our safe ground saver component from our player instead I'm going to create a new script called safe ground checkpoint saver and let's add that to our player let's go ahead and open that up and get rid of all the startup stuff so we are going to do this by creating trigger checkpoints throughout the level so let's create an on trigger enter 2D function now just like with the ground chuck I would like to set up a serialized field type layer mask called what is checkpoint and then I want to check if what we collided with is within that layer mask now in order to check if a collision is within a layer mask we're going to need what's called a bitwise operator so I'm going to paste that in here now I am not comfortable really explaining this line to you because I do not know bitwise operators all that well what I do know is that this works but if you are out there and you understand bitwise operators a lot better than I do then please let us know in the comments down below so that we can all learn something from this tutorial but again what this is checking is this collisions game objects physics layer matches this layer mask what is checkpoint here so if it matches what are we going to do we're going to update the safe ground location so just like before let's create a safe ground location vector 2. foreign location is equal to a new Vector 2. for the X we are going to say Collision dot bounds Dot Center dot X and I'm doing that because I'm going to set up trigger box colliders in the scene so we want to grab the center of the X so that no matter how wide the box is we will warp to the center of that trigger box and now for the Y we want to say Collision dot bounds dot Min dot y Min just meaning the minimum the very lowest point on the Y for that trigger box collider and just like before I'll set up a warped player to save ground within this script as well and we'll do that by saying our transform.position is equal to our safe ground location and just like before if we start our game and we do not run into a trigger then it's just going to warp us to Vector 2.0 wherever that happens to be in our scene so let's also initialize a starting safe location here as well let's do that in our start function I'm going to say safe ground location is equal to our transform dot position okay let's go back to our fall damage script we no longer need that or that or that instead let's get a reference to our safe ground checkpoint saver okay and we'll do that by saying that is equal to gameobject dot find game object with tag player dot get component safe ground checkpoint saver and then finally we can use that to actually call the warp function saveground checkpoint saver.workplayer to Safe ground Okay so we've got a what is checkpoint layer mask here so let's go up to our layers and add a new layer and let's call it checkpoint and then we will change what is checkpoint to checkpoint now let's create a couple of actual triggers I'm just going to create an empty call it checkpoint one I'm going to reset the transform and move it over here let's add a box collider 2D to that I'm going to drag that way up there we go and now we can duplicate that and I'll put the other one over here so now we've got these two checkpoints so over here back in our script remember we set up the safe ground location to be the bounds center of the X which will be right here in the middle so we should land right here on this middle brick and the minimum on the y-axis which I set that to be right on top of my floor layer there and if we test this it would probably help if I set them to be triggers and while we're at it let's also remember to change the layer on those two to checkpoint let's test that okay so we just walked through our first checkpoint but if we fall you are going to see a problem so what the heck just happened let's slow that down and watch it again okay very slow but we just walked through the checkpoint and let's see what happens when he falls all the way down he's starting halfway through the floor there now this may or may not be a problem for you depending on your scene setup and depending on your Sprite import settings so we are simply warping the player to Safe ground by just updating his transform dot position the transform.position of an object in unity is dependent on that object's pivot point and you can tell where it is based on where these arrows are you can see on my character it is in the center and here's the actual file I use to import my character you can see that his pivot is set to Center so it is trying to warp our player's middle position right here to be the center of the X which was on this middle brick but also the Min on the Y which is really right down here and that's no good so let's go ahead and fix that let's go back to our safe ground checkpoint saver let's grab a reference to our player's Collider and a generic collider 2D is again just fine let's call it call let's grab a reference to that in our start function and now I also want to set up a private float called safe spot y offset so what I'm really trying to accomplish is to warp the player's feet his very bottom position to the floor right here which is the trigger minimum bounds not his Center here so what we're going to do is calculate that difference there and we can do that by saying save spot y offset is equal to power collider dot bounds dot size dot y divided by 2. y divided by 2 because we just want to take half of the character's height which is his Center to the bottom there and we want to take that and we want to add it to our collision.bounds.man.y down here let's try that now there we go we went through the first checkpoint and warped back there there's a second checkpoint right here and if I fall down back over here it's going to warp me over there and it's warping me perfectly on the floor honestly guys thank you so very much for choosing this video to learn how to warp your player back to the ground safely it really means the world to me if you found this tutorial helpful please let me know by giving me a like and if you have any ideas for future tutorials that you would like me to cover then please let me know in the comments down below thank you guys and I hope you have a wonderful day
Info
Channel: Sasquatch B Studios
Views: 2,452
Rating: undefined out of 5
Keywords: unity tutorial, unity 2d, unity warp player, unity teleport player to location, unity teleport player to location 2d, unity checkpoint respawn 2d, unity checkpoint, sasquatch b, sasquatch b studios, how to teleport objects in unity, teleport player in unity, teleport player and gameobjects in unity, unity teleport player, unity teleport, unity warp, unity warp 2d, unity teleport object, unity warp player to location, unity warp object, unity tutorial for beginners
Id: mYjql0uUbug
Channel Id: undefined
Length: 15min 58sec (958 seconds)
Published: Thu Dec 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.