How to Open Chest, Loot, Pick Up Items, Speak - UNITY 3D RPG INTERACTION SYSTEM using Input System

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my name is joshik and in this video I show you how to make an interaction system using the new input system from Unity with this system we will be able to see the interactable names then we can interact with them like opening or closing a chest you can pick up items and you can also speak with an NPC so basically you can include any interaction you want by the way I'm using in this tutorial the finite State machine character controller and the combat system for my previous videos you can download them in the description they are not necessary for the tutorial but you can easily integrate them for example like killing an enemy and then take them for take all their loot as you can see here I'm picking up a spot in the sheet from the skeleton so I'm here in the editor as you can see I can move around and I have already objects here like just items weapons arrows and NPC but I cannot interact with them I also don't see the name of them so that's what we have to code here but first make sure that when you have the new input system installed that you go under input system player controls you have to assign a new action or Define a new action interact and it's from action type button keyboard e for The Binding so when I press e then I will interact with something so let's start with our first script it's the interactor for the the player the player is here the interactor and he interacts with an interactable so we Define these two scripts right from the beginning right click create C sharp script and the actor let's also create a new folder there it is it's a bit of a mess interaction system the actor clicking that in this and here also she shops script into Activa all right let's start with the interactor so this is our code that's a lot to cover so let's start right from the beginning that we of course are checking with an either raycast or I'm using here sphere cast wise Vehicles because I think that's only checking only with one Ray is not so precise so the sphere cast is actually working like in much bigger Ray more like a cylinder so we have here of course our origin it goes from the camera then it goes Direction forward from the camera and we have an interaction radio so this is like how thick is the the beam or the cylinder to set the one and how far does it go that is here the max interacting distance as you can see here then we are checking uh which objects we are um actually here hitting with a layer mask in the start function we can see that we already want to interact what to check with interactables enemies or NPCs of course you can change that as it fits in your project then our interaction or interact action is from our interact action here that it's the the key button e and when we perform it so when we click the E button then we are calling the interact function so that's why we have to assign them here okay then next we can see okay when we are hitting something that is intact with enemy or NPC what we are doing where we first check does it have an interactable component so that's the next script so first thing you really have all this logic of an intact on them there we are setting it to our Target let's not Define now but we will get to that soon and if it's not also if you're not hitting anything sorry then we are of course setting our intactical Target again to null so we have to Target of what does this Target on or Target of actually do well it will show us the text we'll show us the text that we can see what we are now hitting with our sphere casting with what can we interact now the interact function that we are assigning to our action in the action action is defined here we checking first that we actually have an interactable if not then we say well there's nothing to interact and if we have this if we can see the text that we can detect with something then we will check the distance are we close enough right that's every distance we're seeing is it actually close enough for the interaction distance like I change this distance depending on the intactable and if that's the case well done we are cutting the interact function finally from our interactable targets so that means that whatever the interaction is it's called in this function here then I'm drawing some gizmos to see something in the editor for debugging and don't forget if you are always assigning a method to a performed action here in the um input system then you always have to unassign it so that's because every time we initializing this object then we are assigning it so it can be the case that we assign it multiple times and that will cause some bugs so we always uh when this object gets destroyed then unassign it now back in our editor we get a bunch of error messages but let's ignore them at the moment and we open up our interactive script now that's the code for interactables of course our interactable has a name then here's the interaction distance from before that's where we checked okay are we close enough and actually can we interact with them this is the case for let's say we want to see a name for the enemy for example we want to see that name but we don't want to interact actually with them when we press the button e so we can toggle this to false this Boolean we have here an Inked app interactable name text so that's the UI element that we will Define later and the canvas here we will search for the canvas and then later it will get displayed here are the the corresponding functions for Target on that shows us the text and the topic of that actually hides the text again the interact function form before that that we trigger when we are close enough and pressing button e Etc that it's here again checked is it actually intactable so that's why this Boolean here is important when it isn't interactable actually then we call the interaction and as you can see that's a protect virtual function so virtually means we can overwrite them if we inherit from this script in an uh in a different class and protect means it's like a private but we can only access it when we inhabit from this this method again it's on on druggismos that we can see in the editor for debugging like okay how we are hitting it or not and so on and of course on when we destroy this inductible like when we pick it up and we want to destroy it then we also have to Target off again otherwise we will see RSD interactable name text it will always be shown so we have to Target off so back in the editor we can see another error message here so that we are missing the actual name text so of course when we have here our UI elements I have a one for the screen space canvas so screen space is that's what you can see always here right on on the right side and top I can show you it's like in which state we are for my state machine and have a different one here a world space canvas and this workspace canvas then will be as name it said will be rendered in the world itself so make sure that you have a render mode set the word space for our name text not not to screen space like it is here it's important also the event camera you have to you have to choose that your main camera so let's create then a new uh empty object here create an empty object and we call this one then interactable and here we create our interactable name text so we are not opening right away and under the interactable I will create again an empty game object and calling it the interactable UI foreign text mesh Pro and text UI here we can change the font size to 30 enable overflow character and let's create a small outline underline yeah and let's open up this script where is it here under our injectable name text here's the script for the intactical name text so as you can see of course we need our text element from the text mesh Pro and then we get that from get company in children we also need our camera that's of course for to see the position of our text and from right beginning when we start the function we hide the text so what does height text mean it just sets the text to to nothing and when we show the text then of course we have to check which type of injectable is it so like if it is the pickup item then we of course changed here the name for from the text to the name of the interactable and we also give that this e button to pick up right so to say okay we picked it up when it is a chest and we check it it open is it closed and then we can close the button or open Button gets thrown it's the same for the loot or for speak with the NPC and if it's not nothing of this kind and if we just give the name like for example the enemy like the skeleton Warrior then last function is the set interactable name position so when we put in just as an input here we get our interactable then it actually goes and try to get a box collider or for more human like for NPCs or for the skeleton has actually a capsule collider and we try to get this Collider and then we are doing some simple maths to actually get the position from uh for the uh for the name text right we get to the transform.position vector up and then we get the from the box collider the the Y bound size and then yeah that's just to get nice position for it so it's normally it works really good with the collider if the inductible has actually no collider then we print the error message look what I found so that we know when we are in debug mode or in the editor I try to look for some errors that is not working every interactable should have and a Collider and so I also work with our interaction system okay back in the editor I will then create this scripts here so we don't get this error message anymore so the first one is to pick up item the next is the intactable chest then the intactable loot interactable NPC and it should be it bigger writing NPC loot chest so we still get this error message here for the inductible chest that's why because we are checking here if it's open and so there's actually no logic in it so I guess we can start with this one it's also here on the left side our first interactable the interactable chest let's open it up so this is the code for the interactable chest of course it has an animator so that we see the animation from it we have we can also lock it up so if we want and like to open it with the key and so on but it's not I'm not using it in these tutorial so maybe later the only important thing is the is open and it's just some basic scripts here so the important part is the interaction we have like first calling the from the uh of our virtual function the base function into action and then we are overwriting it with this part here so the second one is if it's locked and just from sake of this tutorial we're ignoring this one we are just looking at the first if the second if here sorry so if it's open if it's not open then we open it another print opening the chest and if we are if not then we are closing it so we can trigger this one uh in both Direction now what's happening when we open it also there's here to show Loot and so on this is actually for some for different tutorial everything actual loot system items and so on so first the interaction and then the item yeah when we open the chest and we're just triggering in the animator open chest and resetting is open to and it is close or we're just saying it's it's false and the same for the closed chest we are triggering close chest in the animator and then also setting is open again to the false value let's get back to our editor now back to our editor we add a interactable chest script to our chest here and we give it to name chest islog chest ID is open as I said it's for a different tutorial not for now and don't forget we have the player here so let's also add the interactive script to our player we have to make the interaction distance radius you can leave it like that so as let's play it and see if it actually works and yes it's working but there's still some bug let's see in the scene manager this is how the gizmos works so here's the camera so we have here our sphere cast and this sphere is colliding with this interaction radius here okay so let me check what is the bug here it's not actually centered and yeah when we are here in text mesh report UI we have to Center our alignment and actually so that it looks better also to start from the bottom let's start it again and that looks much better here we can tweak also with a different size as to which whenever we rotate around it actually is changing the direction that works and when we press e then it will open the chest and we press e again it will close the chest nice it's working so let's do also the scripts for the other interactivates like for the next one should be the pickup item let's open it up here's the script for the pickup item as I said again you have already some lines here for the item system so what do we need to adjust for interaction system is an item name also if you set the interactable name to the our item name to the inductory name and when we interaction what uh what do we do actually here we are not adding it to any inventory because we don't have an inventory now what yet we are destroying the game object and we are just printing a message so here you can see already what will be in some follow-up tutorial so let's go back to our editor in our editor we go to let's say to our uh potion here and we add the pickup item give it some name like potion again this will change automatically when we have the item data we don't have it now so this will not work also for item name here we can also right like the potion because normally this should go immediately this should change without that we are typing it in here let's start and this also works we see here open the chest and here pick up the potion so next we will do the NPC I open up my intactable NPC script now that's the code for the intactable NPC it's very very simple we are just having animator we're getting the company from the animator and then we are triggering this interaction function where we just say and print message like hello unfortunately I don't have a dialect system yet and then we are triggering the new wave trigger in the animator is also the point where you can start your direct system like when you just have an event like okay open our window or do something that whatever your direct system does maybe in a future with video I will also cover a dialect system so that will be the point where I would start right when you interact with your NPC and then the direct system of a trading window something will open up after this interaction back in the editor we go to our it's in view we have here our former citizen on the NPCs and we just interactable NPCs adding it giving a name um whatever I don't remember the name I just got it now NPC warning to NPC and when you press e we are actually speaking with them or speaking is like just creating animation and then opening up the direct system so how does this actually looks in the animator it's also some very simple player idle animation that gets played and then for parameters we have condition for the wave trigger that we are triggering in both Direction so it is very similar to the opening chest we have a trigger for open chest closed chest and then we first go from the new state that is actually not playing any animation then we are going that is if it's open to this one open chest and if not then we are going here to close and open and so on so very simple animation there now there's only one inductible missing and that's the skeleton which is a bit more complicated but not that difficult if you actually follow my combat system tutorial you should have this enemy script or this anime model here with the script attached to it and the skeleton Rector and the trick is now that this enemy oil itself is just an interactable and make sure that you here have this is intactable deselected so that we cannot press e on it when we are near well we can press e but it will just give us an error message you cannot interact with them otherwise it will be okay what is the interact function and interact with them even if it's not doing something you're calling it so make sure to disability is interactable function so that you cannot trigger the interact function the inductible name is here like also just enemy let me what a skeleton what is name skeleton Warrior so skeleton wire and that's it for the Real Enemy but when it dies then we spawn this reactor here so let's open up this rectal and this Vector here when it falls down then it actually is an interactable loot so that's the one that we have to change here and so let's open that script so that's the script for the intactable loot as you can see here we are actually doing not that much we just when we are pressing e then we're giving a text okay unfortunately you can't take my loot yet again the same like for the chest when we have real items a re-inventory system and you can also open up a windowy window here for now we are just destroying this this component and we also disable the collider like we can loot the enemy just one time and after we took his loot then this will get disabled so if it has a box collider we are setting the Box credit false and when the has a capsular collider then we're also setting it to false otherwise you're given our message and this one should not should not come otherwise there is something really wrong with our code here you can have a sneak peek to uh next to Jerry where we have events we are trying to get items in the editor again we have inductible name we can have the same one here let's get it Warrior now this one is an interactive printer you can press e and we can loot this enemy now this one's a really simple trick to get also a sword and his shield when we open this skirt and rectal here in the prefab mode and we can see here we have a shield and Sport just attached to it so all we have to do is just say okay this one is also and pickup all right so it's also pickup item and we call it yeah sword or skeleton sport as you want oh no this one is The Shield sorry sheet actually I just changing the item name should be enough but for now I'm doing both also this one that's the spot also pickup item all right well we don't know why it's not working for now let's be something really simple to check but let's just play and see if it's working yes we see the skeleton Warrior name is after me and when I put him down drops we can see skeleton wire loot so press e to loot again difference to this on your speak or where was it my potion here pick up here open so that's working as intended and I can press e now I just put my sword away press e working and when I have hover over this watch I can see I can pick up the spot and the sheet awesome everything is working as attended so that's it for this video I hope you learned something new and as always if there any questions feel free to write them down in the comments I try to respond as quickly as possible to them in the next video I will show you an item in inventory system to actually use this interaction system in a weird way okay see you on the next video bye foreign [Music] to this channel where it takes another eight months for the next video
Info
Channel: Jojik
Views: 5,000
Rating: undefined out of 5
Keywords:
Id: 6DyHULHqbP8
Channel Id: undefined
Length: 27min 34sec (1654 seconds)
Published: Thu Jan 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.