Making Sensors in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends today i'm doing a tutorial in unity on how you might implement sensors into your game now if you're not already familiar what a sensor is is a mechanism by which an object is able to determine if there is another object near it and this is a very useful concept for being able to make it so that collectible objects will move towards player when they get nearby triggering when to open a door when a player gets near it or really just any of a myriad of different things that you might want to be doing so for the purposes of our demonstration here and what we're going to be uh coding today we have this very simple scene here where we have a little sphere is going to be our player that is going to roll around and we have these floating cubes are going to be sort of like um warning lights that are going to turn red when the player gets near them uh now a demonstration is worth its weight in words so let's just play the scene and see what it looks like so we can already see that as soon as we spawn this light is turned red because the player is close to it and these other ones are green now as the player starts rolling forwards this other light is going to turn red the one behind will turn green and so on and so forth and they they do respond to stimuli as you get nearer or further away from them now before we commit to writing any code it's important to have sort of a plan of attack or design so that we can keep our thoughts organized while we're creating i like to begin with our player because that's what the person sitting behind the keyboard is going to be interacting with and i find it important to make sure that this is correct before we do anything else now our player is actually very simple in this demonstration they're capable of moving both forwards and backwards as a bonus i'm also going to have a player destroyed after a few seconds because this is going to help demonstrate an issue that you might encounter when creating your own game with these sensors now our alarms are a little bit more complicated when the player gets near your sensor the alarm is going to turn red and when the player moves away from the sensor the alarm is going to turn green now if the player doesn't exist for instance they've been destroyed since they no longer exist it makes sense that the alarm is going to be green architecture behind our proximity alarm is a little bit more complicated than our player as well so our main object is this proximity alarm game object which is going to have this detect player script attached to it this script is going to be responsible for changing the color of the alarm as a response to whether or not the player is near it according to the sensor as a child of the proximity alarm we also have another game object which is an area of effect this area of effect is going to have the sensor script attached to it and that script is where the magic will happen in determining if a player is near it it's able to do this thanks to a collider that we're going to set onto the area of effect this collider will have to be in a triggerable state because it's not going to have any physics attached to it or else you'd get some really strange behavior as a result alrighty so now that we have our design out of the way we might as well get started on actually writing some code and putting our game together so i have a brand new scene here and i already have a couple of folders set up in the style that unity seems to be preferred here and well let's just get to it so first off i'm going to create um some some terrain basically for us to interact with i'm going to call this my grass and if i view it from the game it's a sort of awful beige color which honestly does actually look like my lawn right now but you know i would like to have a nice greener color so i'm going to create a few materials here so here's my first material i'm going to call this one grass grass is going to be green because that makes me happier whoops and i can go ahead and take my grass and i can drag this material right into my mesh renderer and it's going to give us this nicer darker color that is just much more lovely than that beige while i'm in the habit of creating materials i might as well create a couple more for our proximity alarm which is going to be changing colors as our player gets near it so i'm going to start with a a red one now red is going to be pretty easy it's just going to be red and i'm going to go ahead and create another material here this one's going to be green and this is just going to be a nice bright neon green okay so with that out of the way we might as well get started by creating a player so our player is going to be a sphere and i am going to make it um a little bit smaller than the default here in every direction let's put it a little bit closer to our camera here that looks pretty good kind of a hard time moving the camera around that looks pretty good to me uh oh although our camera isn't exactly where i would like it so let's start by bringing this up a little bit and tilting it down to take a look here and gosh this doesn't look very centered to me let's just bring that over perfect okay that looks much better okay so now that we have our player is positioned um i guess we should do a little bit of our odds and ends here so i'm going to start by naming this object player because that makes sense to me i'm also going to give it the player tag and this tag is important because of what our sensor is going to be using to determine if an object that is near it is actually the one that it's looking for and not just some random mother object without all done i should start by attaching a script to my player i'm going to create a new c sharp script this is going to be the one that is going to allow it to move so i'm naming it player movement and now there is like a million different um ways that you can make your player move and it's not really the focus of this tutorial so i'm just going to make it quick here so i'm going to create a rigid body i'm also going to serialize the field which is going to be air speed now what serializing the field does is it makes it visible inside the inspector in case you weren't already aware it's very similar to making a public variable except it still keeps the private status on it so that means that no other scripts can interact with this player movement speed it just makes it a little bit more secure it makes your code a little bit cleaner so to start our scene off we want to get a rigid body and we want to actually get it from our player and saying that it occurs to me that i have not actually attached a rigid body to our player yet so i will go ahead grab that now body just the default settings is probably fine here um and while i'm backing the inspector anyway i might as well go grab this player movement script and attach it like so uh so we have a rigid body now we have our speed perfect so that's everything that we should need i want to get some input from the player so i'm going to capture that inside of this float here i'm calling it forward input and i'm going to get that by getting an axis from the input manager this one is called vertical because i'm going to use the forward to back keys in order to make it move this is going to give me a number between negative 1 and 1 which i can use to tell if i'm moving forwards or backwards so now that i have all that i can take my rigid body and i can apply a force to it i'm going to apply the force in the forward direction i'm going to multiply by my speed and then i'm going to multiply it by the direction the player is actually pushing in all right so we save all that life is good and we can move back to our inspector this should be load shortly and i put in my speed i'm gonna give i think 10 is probably okay i'm going to run the scene and yeah that looks pretty good he's able to move forwards and backwards and that's that's basically as far as we need to go with the player uh so we can start on the proximity alarm these ones i'm going to have as cubes i'm going to call this the proximity alarm and i'm going to change it scale 55.25.25 is probably a good size i can't actually see it it came up wow that's that's pretty low all right so yeah that should probably be fine uh now because i'm going to be creating a few of these in the scene i'm going to go ahead and make it a prefab so i'm going to just dump that in here and this is my proximity alarm now this alarm is going to need a couple of scripts attached to it so first off i'm going to open up the prefab and now i'm going to create a new script which i am going to call let's say i'm going to need two according to my design i'm going to have a detect player and i'm going to create another one and this is going to be called my sensor i might as well start setting that up too so i'm going to create a new empty here and i'm going to call this my area of effect now the area of effect has has nothing attached to it right now which is perfect that's exactly what i want so i am going to give it i think i'm going to use a capsule collider but really you can use any collider for this and i'm trying to remember what it uses my height before i think well it doesn't really matter how high it is as long as it touches the ground and i think maybe an eight that that might be good let's take a look here yeah so that is the area that if the player runs into it they're going to end up triggering the sensor so i'm going to put this one a little closer to player just to start so the player starts inside of the zone of the fluids here perfect okay so go back into my prefab i have my area of effect it has my collider on it and it is very important that i trigger this is trigger box so as long as that box is checked there's no physics that applies to this collider if that box isn't checked and our player runs into this object um it's going to actually basically bounce off of it it's going to act as if it is a solid object and that's that's not really what we want here i can go ahead and add my sensor component as well so i might as well jump into that script because it makes a lot of sense for me to start here and let's see so there's a couple variables that i'm going to need to keep track of here to start with i am going to need to get my collider so i'm going to have my collider i'm going to call this my lowercase sensor and i'm going to start by getting my sensor i'm just going to get components sensor nope that's not correct i want to get my collider okay now because we clicked that that trigger box that is trigger inside of the inspector we're going to be able to use a couple of additional unity functions that we have on modern behaviors and those are going to be called on trigger enter as well as on trigger exit and visual studio is pretty nice to me it just automatically completes those for me so they're all ready to go for us uh there is one more thing that i'm going to want here so this one is going to be a public boolean that is going to be is detected that is detecting this is going to be whether or not it is able of to whether or not a player is near it essentially so i'm going to start with the on trigger enter code here so when our when any object enters into the triggerable area enters into the collider and it's going to give us that object that entered its collider so we can do something with it so what i'm going to do right now is i'm going to check to see if other dot compare tag is equal to oh i knew i forgot something i need to know what kind of tag we're looking for so i could go ahead and just say player in here and that would be totally fine something else that you might want to do so that you can use these sensors in more places though is you could make a serialized field here um which is going to be a string and i'm going to call that tag and we can actually just say right in our inspector nope did not mean to delete that you can say right in our inspector what tag we want to look for so you just say tag right there so what this is checking is to see if the tag that we have set is equal to the tag on the object that has entered our collider and if that's the case then that means that the thing that we're looking for is here so we can say is detecting is true uh let's see so we also want to do the the opposite behavior in here so we can say other dot compare tag um to tag we can say is detecting equals false okay and that's basically all that we need to do in our sensor that's that is the lion's share of the work right there so i'm going to go back into my inspector i've saved my script so it's going to reload here i'm going to call this player so i'm just going to go find the player object when something happens and unfortunately i don't actually have anything that i can really demonstrate right here because no behavior has actually been attached to the sensor it is just checking to see if something is near it but i can go into my proximity alarm i can attach the detect player script while i'm in here come on get in there there we go i can go ahead and edit this now so first and foremost i know that i'm going to have two different colors that i might want to apply to myself so i'm going to serialize a couple of fields and they are going to be materials and i'm going to call this one alarm and i'm going to serialize a field it's another material and i'm going to call this one all clear which save those i'll go into my inspector again and these are available to me now i'll go into my materials oops that's the wrong one proximity alarm so for all clear i want to be green and when i'm raising alarm i want to be red that makes a lot of sense to me go back into our script and i want to be able to change the color so in order to do that if i take a look at our where we have our materials they're attached to our mesh renderer right there and i could change the color of it right now by just dropping it in there so that means i can change its color by going into the script by saying let's see i need to get my brush renderer and i'm just going to call this one renderer and i'm going to get component oh heck or mesh renderer oh and i do want to save that inside my renderer there we go okay uh let's see i also need my sensor too that's a that's a pretty important part of all of us so i'm going to call this sensor and sensor right now is a child of me so i can use the get component in children method similar to how you use get component look for a sensor here there we go and that should pick it up and then on update i am going to grab if sensor dot is detecting which is our public variable so because of the public we're able to use it inside of the script then i'm going to get my render and i am going to set its material to be equal to our alarm because the player is now inside of it if the sensor is not detecting then i can go ahead and get my renderer status material is going to be equal to all clear okay so with that i think we have everything we need to actually start our basic demonstration so let's go into our scene so we are red right now i'm going to move away from this object and look it turned green that's that's exactly what i wanted okay that's good in order to sort of help kind of prove that this is working what i like to do is just grab a few more of these guys and put them around in the scene somewhere okay and just see that this works so i start close to this guy these guys are green and gosh that sure looks a lot like the demonstration we did at the start and you might think that we're done at this point and in a technical sense we are because in this current state these sensors work perfectly fine uh however there there is a potential issue that we might run into where our player no longer exists and it technically does not exit the uh the collider when it disappears and is destroyed uh so it never triggers um our sensors uh on trigger exit event so it'll always be reading as detecting and i'm going to demonstrate this pretty pretty simply for you here by creating another script and attaching it to our player i'm going to call this uh destroy self soon whoops it's going to be a pretty simple script uh you know i don't even really need that timer in there and i'm just gonna say on start i'm going to destroy my game object after let's say five seconds is probably a reasonable number that is a very simple script we grab our player we can attach the destroy self soon script to it and we can see just for demonstration sake i'll try to get close to these two i guess i only got close to one uh and we see that the player has disappeared but this alarm is still red so that means that something has gone wrong there's there's no player here so we want that to turn green so that's a problem that you might encounter when you're using these sensors uh especially if for instance you're making some kind of a shooter and your player is like destroyed they just got shot your player no longer exists uh and that's that's an issue so what we can do is we can go into our sensor and one potential solution is we grab a game object here and call this object defined and we can say when we enter our trigger we can say object defined is equal to other the object that triggered the sensor game object uh when we exit uh we're no longer looking for this object so we can say object defined equals uh let's say null it just it no longer exists and we have to do one more thing in our update which is to check to see if the object is destroyed now unity will let you do this by saying if object defined is equal to null and in our case we're just going to say is detecting equals false and technically there's a little bit of duplicate logic in ontrigger exit and an update because we're setting object define to be null and well it'll be null inside of the update here as well and i don't really view this as a problem because this code is very readable too uh so there there isn't a whole lot of confusion on what can happen here it's it's a pretty expected behavior let's see so with that done i think we're covered now i think if we go and we run this demonstration we should see our player will destroy itself after a few seconds and our lights turn green again because the player no longer exists so there you go that's that's basically the whole thing right here uh we have our player i'm just going to run the scene again we have the player is able to move back and forth these lights are going to change color as the player gets nearer and further away from them and they're going to return to their default state when the player is destroyed all right so by this point you're basically a master of the sensor right now so you're able to determine if a player is near another object of some kind however if you're anything at all like me you learn better from actually tinkering with your project and actually putting your hands on and building something rather than simply watching a video so what i would like to do is challenge you in order to improve on this demonstration in some way and if you don't have an idea of how you might want to do that one suggestion that i do have here is i might as well just play the scene is i've added another color in here so that if we're somewhat close to our sensor it's going to turn yellow so we we do change different colors here as we move along so it still has all the original behaviors it just has this extra one that if you're oh there we go goodbye so if we are somewhat near the sensor it turns yellow is basically the new thing that has been added to this demonstration anyhow thank you so much for watching what i'm going to do for you is i'm going to put the source code for this project in the description for the video just below and if you did like this video and you found it helpful do me a tremendous favor if you would just like and subscribe and if you do use that source code give that a like as well thank you again so much for watching and we'll see you in the next one
Info
Channel: Bourgeoisie Birdie
Views: 591
Rating: undefined out of 5
Keywords: Unity, Sensor, tutorial
Id: DYsIFSAGGcU
Channel Id: undefined
Length: 22min 23sec (1343 seconds)
Published: Sun Apr 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.