FIRST PERSON INTERACTION in Unity!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
whether you're picking up items in an RPG or pulling leverage and pushing buttons on a puzzle game interacting with the environment is often an important part of first-person games and in today's video we'll be covering how to do just that in unity as we get started if you'd like to download the project from where I start in this video you can get that on my GitHub link for which will be down below however if you just want to skip the video entirely I'll have the completed project available for download on my patreon page with that out of the way let's begin start with I've got a simple scene set up with some objects from a free asset pack that I created link for which will be down in the description I've also set up a simple first person controller that allows you to walk jump and look around on top of that there is also a UI canvas for the HUD that has a small blank image in the middle as a sort of Crosshair the last thing I have that we'll be using is this quick outline asset from the asset store it's free and it's super quick and easy to use links for all the assets I use will be down in the description all right now to get started the first thing I'm going to do is create a script called interactable this will be a short script that will go on all the objects you want to interact with then we'll just want to open it up and edit it we won't need to update so we can get rid of that and for variables we'll want a private reference to the outline component and a public string containing a message to be displayed when hovering the object lastly to keep it somewhat generic we'll have a public Unity event called on interaction to do this we'll need to say using Unity engine.event if you're unfamiliar with unity events what this will allow us to do is call a bunch of different functions that we can set in the inspector similar to how UI buttons work now down to the start function we'll want to get the outlink opponent and then set it to be not enabled by default we'll actually pull this out into its own function called disable outline that will return void it will also create an enable outline function that just sets the outline enabled true we'll need these later in a different script so we'll make these public now we just have one more thing we need to do and that is create a public void interact function and in here we just need to say on interaction.invoke this will call everything that we set in the inspector and that's it for this script we can save and head back to Unity alright back in unity I'm going to start with the money object here here we want to add two components to the outline and then the interactable script we just created then before we forget we'll want to head up to the tags and make a new tag called interactable and we'll want to assign that to this object this will be used later in our player interaction script now in the interactable component I'll add the message something like pick up money then down in the on interaction area we'll add an item and in here I'll drag the money game object and then from the drop down select gameobject.setactive and we'll make sure that's unchecked this is just because we don't have any actual inventory so we'll just disable it for now also we want to make sure that there is a collider on the object so that it will be detected by array casts later now that we're done with the interactable setup we need to move on to allowing the player to actually interact with these things to do this I'll create a new script called player interaction and drag this onto the player object now open this up for editing alright here to start off we don't need the start method so we can get rid of that and now for variables we'll create a public float called player reach this will determine how far items can be from the player before they can be interacted with I'll default this to something like three next we'll need to set up a private interactable called current interactable we'll use this for storing Which object the player is currently looking at alright now done on update we'll read an input I'm just going to use key code f and then we also want to make sure that the current interactable exists because in here this is where we're actually going to call the interact so then we'll say currentinteractable dot interact now we need to actually set the current interactable somewhere to do this we'll create a void function called check interaction this will call every frame before we read input now how we're going to check for the interaction is by casting our 8 from the center of the camera and checking if that hits if it's an interactable Want to Set current interactable and enable a deadline if not we'll do the opposite to do this we'll start with raycast hit hit then we'll make array equal to new Ray with camera.main transform position and Camera main transform forward as arguments this will shoot a line straight from the center of the camera now if we say if physics.raycast and use the array we created and output to hit and we'll limit that to player reach next inside the statement we'll check if hit has the same tag as that interactable one that we created earlier and then inside there we'll get a reference to this new interactable called new interactable this will equal hit not collider.kick component interactable now lastly we'll check if the new interactable is enabled and this is where we'll finally set the current interactable I'm going to put this into a new function just to keep the code more readable and so that it's easier to change later we'll call this set new current interactable and we'll feed it the new interactable now down in this function we'll set current interactable equal to new interactable then we'll call currentinteractable Dot enable outline while we're down here we should create a corresponding void function called disable current interactable and here we'll check if current interactable exists then we'll say current interactable dot disable outline and set it equal to null now backup and check interaction there are a few places we'll want to call disable interaction first after the raycast if statement we'll say else disable interaction this is if nothing is Within Reach next after checking if the tag is interactable we'll want to say else disable interaction this is if the item we're looking at is not an interactable object lastly after checking if interaction is enabled we'll want to save else disable interaction this is if the interactable we're looking at is not currently enabled now there is one last condition we'll want to check this will be for disabling the outline if in one frame we're looking at an interactable but in the next frame we're looking at a different interactable this would happen if two objects are overlapping or are just kind of close together currently the outline on the first object would not be disabled where we'll put this is right under where we declared new interactable we'll say if current interactable is not null and the new interactable is not the current interactable then we want to say currentinteractable.disable outline now save the script and head back to Unity if we hit play we should be able to walk up to the money we changed earlier and yep it gets outline and then when we hit f it should disappear Perfect all right now all we need to do is display the message first we'll go on to the HUD and create a UI text then we'll want to drop it down just a little bit so it's just below the Crosshair then we'll want to center it and change the font size perfect now we just have to control it what I normally like to do for HUD UI is create a Singleton script that we can call from anywhere that will change the UI so we'll do that create a script called HUD controller and we'll drag it onto the HUD canvas and then we'll edit the script and here we won't need update or start so we'll get rid of those and then to make it a Singleton we'll make a public static HUD controller called instance then we'll use awake and set instance equal to this now we should have a reference to this from anywhere to be safe we could do some error checking to make sure there's only one HUD controller but we won't worry about that for now we want a reference to the text we just created I'm using text mesh Pro so I'll make sure we import using text mesh Pro next I'll make a serialized field for a TMP text called interaction text now we'll need two functions enable interaction text and disable interaction text Bolter will turn void but enable will take in a string called text in enabled we want to set interaction text Dot text equal to text plus some string for your input I use f here we could probably grab this from the input manager but for now I'll just hard code it in next we'll want to make sure that the interaction text game object is set to active next we'll go down to disable interaction text and all we'll want to do here is set the interaction text game object to inactive great we're done here now but before we go back to Unity let's go back to the player interaction script and add calls to these in the appropriate places the downer and disable current interactable will say HUD controller.instance.disable interaction right above the if statement then down and set new current interactable we'll call enable interaction text right at the end doing the argument of current interactable dot message perfect now save and head back to Unity and we don't want to forget to drag the interaction text we created earlier into the appropriate slot on the HUD controller perfect now if we hit play everything should work as expected yep and it looks like the text shows up perfect awesome now that the system is set up you can use it any way you want if you're going to use it for inventory items and Pickups I'd suggest making a new subclass that handles that instead of using these events but it's up to you now I'd like to show a quick example of something that this could be used for I created this simple lever script that has an animator and two Unity events one for up and one for down been in the scene I set the interactionable on the lever to call the toggle lever function and from there it calls the events I set up which lower this little force field so I can get the money cool and easy right well that's it for this video If you learned something give the video a like if you hated it give it a dislike if you really liked it subscribe for more tutorials I'm planning on making more RPG related tutorials in the future so stay tuned for those foreign
Info
Channel: Kabungus
Views: 8,065
Rating: undefined out of 5
Keywords: Unity, FPS, Interaction, Tutorial, Kabungus, Items, Talk, First person, unity tutorial
Id: b7Yf6BFx6js
Channel Id: undefined
Length: 9min 39sec (579 seconds)
Published: Sat Jun 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.