Interactions with Unity Events - New Input System

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back everyone in this video we're going to create an interaction system inside Unity using unity's new input system so we're going to go over raycast we're going to go over interactions Unity events so we can expand upon the system a lot easier as well as interfaces classes and some object-oriented stuff as well there's the same interaction system I used inside the game kit that I made link to that is in the description if you want to check that out if you keep coming back to my videos make sure you like And subscribe and join the Discord we're building a really great Community there as well the code to download everything inside this tutorial will be available on patreon link to everything will be again in the description let's get straight into it so I'm inside Unity I'm on the sample 3D third person character template that Unity provides you can open this project from Unity hop it's completely free and the reason I'm using this template is just so we can cut out the whole character controller stuff and focus entirely on interaction so let's get straight into the code you want to create a scripts folder inside your assets folder I already have that because it's part of the template we're going to open up the script golden inside scripts we're going to create a new folder and let's call this interactions inside the instructions folder we're going to create three scripts let's just start with the one and we will create the other two later on so we want to create a c-sharp script and let's call this pull the interaction we're going to attach this script to our player let's open that up in our editor by default unit is going to generate this stuff and get rid of all of this and we can get rid of all of this cool side here we're gonna need three functions we need a private void awake we need a private void on enable and we need a private void on disable and actually we need one more function and that's the actual interact function so we'll have private void on interact now these functions should be pretty self-explanatory um but we'll go over them real quickly so the on enable on disable is important so that we can subscribe and unsubscribe from events just like you should subscribe to this channel new Unity info system works pretty much like delegates does um in C sharp we have to have a private player input and we'll just call this player input and that's so that we can actually access the input now this by itself doesn't do anything we need to actually store a value onto it that's why we do player input equals get components player input now a lot of you are probably saying why can't I just say get player input every time get component is very very slow well it's not that slow it still executes quite fast in real time but when we're dealing with programming it's very very slow and we don't want to use it that much we use it once at the start we don't have to use it ever again and then we can move on to subscribing and unsubscribing from events instead inside our enable functions now what we need to do is have our own interact function be called every single time we press the interact key from our player input to do that we have a really fancy line from for the new input system and it looks like this we have player input dot actions we choose the action in our case the action is going to be called interact make sure you remember this is spelled case also matters we're going to use this later on inside our input asset and then we can access the performed events and we want to use plus equals to add a listener and we're adding the on interact method that's going to give us an error get rid of two parentheses and you can do alt not alt sorry control will stop and that will bring us this little menu make sure you select change signature of method that's going to Auto generate these parameters for you and make sure you add a semicolon as well if it's not all control if it's not control full stop sometimes it's Alt Enter or maybe if you just right click it it changes per IDE otherwise feel free to just type it out it's not that much either and we want to copy that line and put the same thing inside on disable except plus equals is now minus equals because we're removing the listener and this function is going to do our raycasts for us and for now let's just test that the input does work so we'll say bound DOT log interacts we'll get rid of this line in a second that doesn't matter let's jump straight back into Unity for now and set up the input action asset as well now if you don't have an input action asset you want to create one of those first in my case I already have one and so I'm just going to open that up and here we want to add an action action is called interacts just like how you spelled it in your code we want to add a binding so click this empty box here press listen and you can just press e on your keyboard and select e keyboard tick keyboard and mouse let's add another binding and let's bind it for this binder for Gamepad and let's do button West and you can take Xbox controller and PS4 controller button West is usually your x button on Xbox make sure you press save asset and you can close this screen after that cool now you can select your player in my case this is our player and I'm just going to go add component interaction right there and now if I open up the console and press play every single time I press e on my keyboard okay so I was getting an error I changed on interact to do interact I literally changed the method name and that seemed to have fixed my error I think that's probably just on my side I don't know what was going on there's probably Unity um thing going on in the background but you probably wouldn't have got that error if you did maybe try restarting Unity um or try renaming your function apparently that works if I press e we can see that we have interact coming up in our console which is good we have the key binds working so the next step would be to get the raycasts working so we can write the raycast script and then we can dive into actually making the Box do interaction stuff um the raycast script is not too difficult I know raycasting can be a little bit confusing but we'll take it step by step so to do the raycast we're going to write physics Dot raycasts if you're doing this in 2D it would be physics2d Dot raycast and we can see here from the uh it just disappeared but the first parameter is the root position of our raycast the starting point so we choose a starting point we choose a direction and we shoot array infinitely towards that direction and we have a bunch of other parameters we can customize or so that we get the right output so the root position is going to be our transform.position but we don't want to do this because transform is similar to saying get component so we want to have a reference saved to transform so we'll have private transform underscore transform and inside awake we can just have underscore transform equals transform just like that and then this can be underscore transform.position but what you might find with your character especially in my case is the root position is at the player's feet which is not ideal because the raycasts will not actually hit the cube there we want it to be maybe around the player's waistline so let's add a bit of offset to the starting position so we can add Vector 3 . up and that's gonna offset the position of the raycast by one unit in the up Direction now if we don't want it to be one exact unit we can multiply this by some decimal number like 0.3 which will lower a little bit more this is diving into more of the vector math side of things it's not too complicated but if you're not understanding this I highly recommend you read up on vector and 3D math stuff and we also want to add a forward offset just so we're starting it from a little bit further than the players uh initial position and that's just so we can avoid colliding with the player as well so we can do that just by saying plus transform dot forwards and again we can add some sort of decimal number multiplication here so that we're not going one unit too far so let's just multiply this by 0.2 now the next parameter is the direction in our case it's just transform.forward and then we want to have a reference to whatever we hit so we can write out VAR hits the next parameter is the length of the ray so we'll just make it 1.5 and then we have the final parameter which is the layer mask in our case we're going to create a layer mask in a second so we'll do we'll have a reference up here let's have serialized Fields private layer mask and we'll just call this Interac table layer just like that and here we can pass in interactable layer cool so this function physics dot raycast returns a Boolean if we hit anything so if we didn't hit anything there's no point doing the rest of the function so we can have if not physics dot raycast or that fun stuff then we can have return cool so what this does is we do a physics raycast if we hit something output the hits otherwise if we didn't hit something if not then return and what return does it exits this function so the rest of this code doesn't get executed so we can for example move this down here because because this is only going to log interact if we actually hit an interactable but we don't know if we hit an interactable all we know from Hit is that we hit something and this is when we have to jump across now to the interactable side instead of the player side so instead so inside the interactions folder I'm going to create another script and this is create a class interface and let's call this Interac able object now interactable object is going to be a mono Behavior class so that we can attach it to a game object um but it's also going to inherit an interface and it will create the interface now as well so here we'll call it I interactable there's two eyes there because it's an interface um a credit class just so I can show you guys how we convert this to an interface instead of public class it's public interface and that's it now why do we need an interface and why do we need the objects and what's the difference I'm going to put some links inside the description to where you can find out more about interfaces classes records structs all that sort of stuff because there's a whole bunch of stuff in programming which you probably don't usually cover using Unity now our interface is going to basically be a blueprint to what an intractable object should do or should have so the first thing every interactable object should have is a function to do the interaction so we'll we will have a public void called interact and this is a fancy function because you'll see that we don't actually have curly brackets here it's essentially an abstract function um even though we don't put abstract here we can there's no need because it's an interface anyway the whole thing is abstract if I did this a class this would actually be an error because it's not abstract so it must declare a body if we put abstract here that would fix it but to do an abstract method we have to do an abstract class now the next thing our interface needs is something to store what we should do when we interact and that's our Unity events let's create a public Unity event and take note that it actually added this namespace using Unity engine.events if it doesn't add that make sure you add that otherwise you'll have an error on the unity event this is going to be a property so we'll have gets we need to give it a name of course and let's call this on interact and now we have to make this gets protected protected sets protected is different from private in that private means only this interface or this class can use that thing protected means this interface and this class and its children and use this thing to use the interface we can say mono behavior and I interactable that's going to give us a error saying yo you're doing something terribly wrong so we can just click anywhere on this red line and do control dots again or Alt Enter and then you can do Implement missing members make sure all the boxes are ticked and press ok that's going to generate this code for you if it doesn't generate the code feel free to type it out yourself it's not that much so in short our interact method should just call the on interact method so we can do a Lambda expression to this and say on interact dot invoke and that's gonna make this thing do stuff so that we can edit this we want to make this a serialized field so we'll just say serialize field private Unity event on interact and that means it's going to show up in the inspector so that we can customize it jump back into Unity there's a few things we should do so that we can keep up with our code the first thing I want to do is Select our interactable Cube and we have to put this on the interactable layer I've already done it but to do that you can go on the layer drop down press add layer and on layer 10 or the next available layout just type interactable the name doesn't matter too much and then on your player here we have the drop down for interactable layer make sure that you have ticked interactable you don't need to tick anything else in fact don't tick anything else because this is what guides the raycast to hitting stuff so what this means is that our raycast will not collide with anything except objects which have the interactable layer on them the next thing we should do is click on our Cube and go add components and I want to add the interactable object as components and you'll see that this gives us this really cool interface on our inspector this might look quite similar to what you see with unity's user interface and buttons and that's exactly what it actually is it's a Unity event so we can press this plus arrow to add to the list and for example what we might want to do is make the Box disappear so let's select the mesh renderer function mesh renderer and let's have bull enabled us so now when we interact with the cube the Box will disappear we can't disable the Kalina because that means that we can't turn it back on but the benefit of this is that you can add anything to this list in our case just so you can visualize it I'm making the Box disappear but there's a whole bunch of stuff you can do here you can add other objects to it so if you want to interact with this and make another object disappear you can do that if you want to have the play Disappear you can do that you can do anything really now everything on our code side is actually pretty much setup the only thing left to do is obtain this function from whatever way interacting with from the raycast that's it and that's inside our do interact method down here so what we want to do is check if the object we hit on the interactable layer has the interactable object script if it doesn't ignore it if it does or that method so we're going to have another Escape case here we're going to write if not it's dot transform dots will have trigit components and we're going to Output the interactable objects and we'll call it interactable and then return so this might be some fancy syntax you might not have seen before essentially what we're doing is we're going to check if we can get the component try get component is a method which returns a Boolean if we can get the component it's going to return true if we can't that means the component is not on that object it's going to return false if we can get the components it's going to return true and the output that components and we're going to call the output component interactable it's the exact same thing that we did here with variet and so the only thing left to do now is say interactable and interactable again is the component we obtained from that object Dot interact you'll see the method down here and that's ladies and gentlemen is it's that's literally all you have to do here we go let's walk up to this Cube press key and the box is gone obviously we can still stand on it because the collider is still there but that's it really your interaction system is done now you're probably wondering okay but what if I want to bring the box back right because making a box disappear is pretty easy um but how do I make it come back essentially what we want to do now is create a switch so that we can turn the box on and off which is a variation of interactions now there's a few ways you can do this you could create another interface which inherits from this interface you could create another class which inherits from this interface which inherits you know there's a whole different direction you could take this in our case to make this as straightforward as possible I'm going to rename interactable object to switch interactable so now we're going to treat this interactable object as if it's a switch and that's it now that doesn't make sense in the case down here because we're going to out switch interactable what if we have a different type of interactable so we're going to rename this to I interactable and that means anything which inherits the eye interactable interface we're going to regard it as something that we can interact with so now if you wanted to have a switch interactable you have a switch interactable if you want to duplicate this class and call it button interactable for example which does the same thing but slightly different you can do that as well and you can essentially build upon this in many different directions to create any different variations of interactions so to make this a switch we have to have some pretty straightforward variables we'll have a serialized fields private Unity events and we'll call this stop interact so stop interact is going to be called when we're stopping to interact on interact when we initially interact and to determine which one we use we have a private ball and we'll call the ball is on now our interact method is not as straightforward so we have to modify this a tiny bit the first thing we should do is check if it is currently on so if is on we should then do stop interact because it's currently on so we should stop interacting with it otherwise then we should just do on interact Dot inverc and we can get rid of these we don't need them and then the last thing we should do is toggle is on so is on equals the opposite of is on and we can just do that like this and that is it so now when we jump back into Unity we have another similar sort of drop down list here for the Stop interact method um so essentially what we'll do is copy the exact same thing we can choose the mesh renderer and go Bool enabled and tickets now the other thing you could do is actually change the material change column there's a whole bunch of stuff you could hard code as well um but in our case this should get the job done so if I walk up to this Cube I can press e box disappears press e box comes back and that is it we have interactions set up um so you'll see it's not actually that hard to get interactions working in unity especially to create an expandable system like this it's very very helpful now you could build upon this tutorial in any way that you want really if you want tool tips appear you can do that so if you walk up to the cube maybe the cube highlights and it says press e to interact or something you can do a whole bunch of stuff but that just about does it for this video guys I will catch you guys in the next one again the code for the tutorial will be on patreon link to that will be in the description and join the Discord if you haven't already that being said I'll catch you guys in the next one
Info
Channel: ErenCode
Views: 4,405
Rating: undefined out of 5
Keywords: how to make a game, unity tutorial, how to make a game in unity, game development for beginners, how to make game, how to make your own game, how to make an app, unity tutorial for beginners, devlog unity, unity interaction system, how to make interactions in unity, how to, unity drag and drop, how to make an interaction system in unity, unity interactions, game development, unity 3d, game dev, new unity input system, input system, unity events
Id: ZNiEbRL85Vc
Channel Id: undefined
Length: 21min 36sec (1296 seconds)
Published: Thu Aug 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.