How To Interact With Objects [Unity Tutorial]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome back to the channel today we're going to be taking a look at interacting with the environment and specifically objects within that environment so if we just take a quick look at the project that I have set up here we can see that I've got a very basic character control nothing too spectacular a few colliders and we have these bins or trash cans for our American viewers and when we go through one that's interactable we have a little icon that pops up that shows us we can interact with it and if we press our interact key we can see something happens and the aim of this tutorial is to make this functionality reusable throughout our entire project by making it as generic as possible and this is going to be a one of two parts the second part we're going to go on to interacting with doors and traversing around our map using this functionality so we'll do this one first and then we'll go into the player transitions between all within a scene in the next tutorial so I'm gonna go ahead and delete all the functionality that's needed to do this and we'll start recreating it from scratch okay so I've gone and undone all my hard work so if we go and play this game now the only thing that I've left is my character control as you see when I go past the bins I don't get anything and I can't interact with them whatsoever just before we start I do want to give a quick shout-out to the creator of the asset pack that I'm using in this tutorial that is mutual pixels you can find them on Instagram and H if you want to download this pack for yourself it fantastic pixel artists make sure you go and check him out so we've got to understand the kind of things that we need to make objects interactable now the way that we're going to do this we're going to use rare casting and put very simply what rare casting does whenever you call the rare cast it will fire basically align an invisible line through the direction that you want - and if it hits anything it'll return that object back to you so we can then interrogate that object and see if it's one that we're interested in but to actually get that hit collision we need to make sure that our objects have a box Collider in this or any type of Collider for that matter but in this instance we're going to be using box Collider 2d so we're going to make sure were there anything that's interactable automatically has a box Collider now I'll just give you a bit of fair warning here this tutorial may be a little bit too advanced for some people I'm gonna try and go through and describe everything as well as I can and try and guide you in the right way but there are a few elements in this that have their own individual tutorials on my channel so I'll put links to those in the description if you're interested in finding out a little bit more about things like inheritance abstract classes and extension methods so let's start by setting up a character control Y we already have this set so as you can see all this is doing is it's accepting a horizontal value and we're moving our character based on that I'm setting the animator parameters at the same time nothing spectacular and again I do have a tutorial on 2d movement if you're interested in that so what are some of the methods that we're gonna need in here well we have our interactable icon the little finger that you saw whenever we hovered over an interactive object so we're gonna need a way to Alton this from outside of this script so we'll just start by creating these while these are going to be very simple so these are going to be public void not capitals public void open for pan interactable icon and we're also going to want a public void clause interactable icon as well and all these are going to do they're going to get our interactable object and set it to true or false respectively so that's interactable icon set active and to warden it will set it to true and we'll copy this line a call down into our claws and set it to false when we want to close it simple stuff we're gonna want one more method and again this is gonna be a private void because we're not going to be accessing this outside of this script and we'll just call this check interaction so we want to call check interaction whenever we press our interact key so we'll go ahead and do a really basic import check so inside of our update will check if import get key down and we'll use key call dot e whenever we press eat we'll call our check interaction method so for now we'll leave our character controller we have the basic setup will go on to creating the scripts for our interactable objects so first of all we want to create a class called interactable now this is going to be an inheritable class so every one of our interactable objects will have this class so this is where it's going to start getting a little bit more advanced because interactable is going to need to be an abstract class because we're going to implement an abstract method now very simply what an abstract method is is sort of a placeholder and any class that inherits from interactable will need to implement that method we'll throw in a public abstract void and we'll call this interact and the reason we want this is because every interactable object may have a different outcome so in this we're going to be opening and closing those bins so that's just swapping a sprite in note in the next tutorial that interact method will need to allow us to transport ourselves around the map when we're using a doll so we don't actually want to put any cord inside of that method in this class another few things that we're going to do we're gonna want to add in some trigger enter and trigger exits that'll be on trigger enter 2d on trigger exit 2d and all these are going to do these are going to be responsible for opening and closing our interactable finger icon whenever we enter an interactive all space so we'll check if the collision and we'll compare that tag and if that tag is player will get collision dot get component will get a character control and we'll open the interactable icon and then exactly the same when we leave but this time we're gonna close the interactable icon now like I said before every interactable object is going to need some form of Collider so we can make sure whether anything that we inherit from interactable with we can make sure that that object does in fact have a 2d collider and to do that above the class declaration we can use the require component attribute and the component that we want is of types of type of and we'll pass in box Collider 2d and one final thing in this class is we want to make sure that this box Collider is always set to is trigger because we use in Earth trigger commands another collision commands so the way that we can do that we can use the model behavior method reset private Lloyd reset and inside of reset we can use get component box Collider 2d and we can set is trigger equal to true now we can do this in reset because we know for a fact that our object is going to have a box Collider so that's this class complete we can get rid of these two system namespaces we won't be needing those but now we have an inheritable interactable class so let's put this to use in the case of I've been all we want to do is swap the sprite oh we want to open the bend them and close the bin so we can go ahead and create a class that'll do just this and we can call it a burnable now again just like in interactable there's a component that we're definitely going to need and that this time is the sprite renderer and we need that because we're obviously going to be swapping in and out of sprites so we can add in the required component type off sprite renderer so now we know for a fact that we're always going to have a sprite renderer next we're going to need to public sprites and that's going to be for the open Spragg publish bride plus we're also going to want to get a reference to the sprite render itself so that'll be a private sprite renderer from atlas Cola SR and we also want to keep a local copy of whether or not this object is open so again a private pool and we'll just call that is open so we're now start method we can grab the sprite renderer just like we always do with getcomponent we'll get the sprite renderer and then we'll set our sprite renderer sprite equal to our closed sprite that we've passed in so now we want to make anything that's open herbal interactable so we do that by instead of inheriting from monobehaviour will inherit from interactable and this will give us an error and this is saying that we don't implement the abstract member interact which is this abstract member right here so we can go ahead and let unity autofill this all it does is puts a public override called exactly the same as the method name in the abstract class and it'll put in some boilerplate code which will just throw a system error because we're not currently doing anything with it so we can remove that area because we're going to fill this in there and all we want to do if we interact with one of these automobile objects is will check if we're already old soil check is open if we are we want to close it it'll set sprite renderer dots sprite equal to our cloth spray or else if we're not open so we're closed we want to open it so we'll do the exact opposite set the sprite renderer sprite equal to open and then we want a toggle that is open boolean to whatever the opposite was and again that's this class complete get rid of any unused namespaces so now that we can check for interactable objects we can go back to our character controller class and we can start checking for interactions so like I said at the start of the video we're going to be using bread casting now we're not going to use the standard rear cast line we're going to be using something called box casting which works exactly are similar to standard rear casting but instead of putting a two line it'll put a box a 2d box around our target object and then they'll check for any colliders inside of that box and return the hit value for that so we're going to want to take in an array of objects because we may be over the top of multiple objects that have colliders so what we can do we can set a ray cast hit 2d and we'll make that an array and we'll call it hits we'll set that equal to physics 2d dot box cast all and that box cast all needs for parameters the first being its origin point so we want that to be a player's position so we can set that to transform position we also want a vector to of the size of our rear casted box so I've gone ahead and up at the top here I have a private vector two and the box size and I've set this box to be point one wide by one tall so that way no matter where the object is if it's on the ground or slightly above our player will be able to capture it by having a one on the Y and similarly we only wanted quite narrow into our player so we're not finding objects that are way out of our reach so we can put in as our size the box size vector - next we need an angle in which this box is angled but we don't want it to be we just want it to be a standard box so we'll put that in a zero next we need the direction in which we want this box to be casting so we'll use vector 2.0 next we want to make sure that we've actually got something in our hits array so we don't actually get an index so deranged exception we'll check if it's dot length is greater than 0 if it is we've hit something and we can go over our hits array so now we can do a for each loop and we'll do for each rear cast hit 2d we'll call WA FC in hits and then we need to check if our object that we've hit is actually interactable so the way that we could do this is we could check if I see dart transform getcomponent interactable so if our object has the component interactable that will return true and we'll go into our block of code inside the if statement and then if it is we can do exactly the same again but this time we'll call our interact method because we know we're going to have one because we have interactable we're forced to implement our interact abstract method and inaudible that's right here and that just toggles our surprise so whenever we hit this we're going to open and close whatever we want and then just to make sure that we don't interact with every object we can return out of that if statement if you want to be able to interact with everything that you currently in range of leave that return statement so and this will call interact on every interactable object I just want to call it on the first one that we find but instead of needing to do this on any other interactable components we can in fact use extension methods and we can extend rear cast hit 2d so this isn't strictly necessary this chord will work as it is but I'm going to make you so to some extension methods either we pop back over into unity will create one more script and we'll call it extensions we'll get rid of our stat and update we don't need to inherit from monobehaviour but for our extensions class this does need to be static let's have a think about what we want we want to extend read cast hit 2d and we want to be able to get whether or not that object is interactable from it so let's create a public static boo the call this is interactable and then to make this an extension method what we need to do we need to pass in this and then the time that we want to extend so that'll be really cut it to D and we'll give it a name hit and all we want to do in this method is return whether or not hit transform getcomponent interactable and then we can also create one more extension method in it and we'll just call this one interact and again we're going to be extending read cast hit 2d but this time we don't want to return anything so we'll put void get rid of the return statement and this time we'll just call interact on our object so now if we pop back to the character controller we can put in these extension methods by removing all of X and we'll just check I see dart is interactable so it does exactly the same it just makes it look a little bit nicer and then again here we can do I see dark interact and I do go into extension methods in a lot more detail in one of my other tutorials so I'll make sure you go and check that out if you're a little bit confused as to what I did there so now we have all of our scripts set up it should just be a case of implementing them inside of our game so we'll open up the game object that I have all my assets inside and we'll find our two empty bins these two right here and as you can see all they've got is a Spragg renderer which you'd expect for an in-game sprite so we want these both to be audible so we can drag and drop our honourable script on and as you can see it's automatically generated a box Collider for us it's automatically setting to trigger and it would have automatically given us a sprite renderer if we didn't already have one so now all that's left is to populate our opening cloths prize so if we take a look we'll get our exteriors props so we want bin closed enclosed and been open in Alban and that should be it we should be able to play our game now you can see I can move around like I did before whenever I'm next to a bin that's interactable I get my little interactable finger arrow and if we press E while we're over it we can see we swap our sprite so and then we can toggle back as well so that we could make any of our sprites interactable as long as we add a very in sprite father so we could go ahead and create an alternate version of our fire hydrant that's leaking Walter and whenever we interact with that all we'd have to do is select our object drag in our opening script and drag in the two sprites for Altmann closed so as you can see this is extremely reusable and extremely easy to use once everything's in place now in the next video what I'm gonna do I'm gonna reuse this for our dolls dolls are gonna tear calls around them up into different houses into different rooms and we're going to be using the interactable gripped so if you're interested in that make sure you go and check out that video as well and I will see you again soon if you've learned something today then drop a like on the video and subscribe to the channel you can also find us over on social media for more bite-size unity in c-sharp tips I've been Mike for com3 Interactive and I'll see you again soon
Info
Channel: Comp-3 Interactive
Views: 9,962
Rating: undefined out of 5
Keywords: comp-3 interactive, comp3 interactive, comp-3interactive, comp3interactive, comp, comp3, comp-3, interactive, unity, unity3d, unity2d, 3d, 2d, materials, easy, how, to, howto, learn, course, series, tutorial, tutorials, game, development, develop, developer, android, mobile, games, programming, programmer, tip, tips, indie, coding, basic, basics, C#, video game, first, material, physics, navigating, noob, beginner, intermediate, advanced, professional, guide, introduction, full game, space invaders, interaction, interacting, interactable
Id: GaVADPZlO0o
Channel Id: undefined
Length: 20min 22sec (1222 seconds)
Published: Fri Apr 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.