Picking Up And Moving Objects | Inspect Item Part 1 - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to the engine 4 tutorial today when we starting off creating an item inspection so this might be more on the simple side however today what we're doing is we're going to be going over how to pick up and put down the item and then in the next video we're going to be going over how to actually inspect it so you can move around look at it and all that good stuff again today i'm gonna be picking up and putting it down so let me show you what that looks like again might be more of a simple version to some people but it gets the job done and it still works quite well and efficiently so we press e on it we're gonna pick it up we press it again it's gonna then be put down on the floor and we can pick it up and put it down as many times as we want and we can walk around anywhere and then put it straight back down again so this is what we're setting up today and obviously this episode doesn't have to just be item inspection this can just be picking up and putting down an item if you want that as well again this is what we're making so without further ado let me delete this code and i'll show you how i've done it so the first thing we want to do is want to create an item parent for all of our inspectable items so to do that i'm going to right click get a blueprint class get an actor and i'm just going to name this one inspectable parent or item parent or anything along the lines and i'd already have one named that so i'll put inspectable item parent like so and this is technically apparent the way to make it apparent obviously is if you right click on it you can then create child blueprint class i'm not going to do that but doing it this way means you can just right click do that and then any child of that will have the same code the same elements everything that this parent has so it just makes this a little bit easier to do and a bit more efficient as well so we can do all the code in this one item and then we'll do it for all of them else but let's open it up straight away all we're going to do in here is add a component adding a static mesh i'm going to name this one item and just to test out i'm going to give it a sphere again you can give this any item you want but again this is for the parents so you don't really need one in here because when you create your own child of this it will have its own static mesh which you can set that's what i can see is rather big so let me also just scale this down to about 0.5 maybe maybe even 0.25 compile that and so again now we just have our static mesh in here what i also want to do is make sure that the bottom of this is in the center so i'm going to just drag that up there so now the bottom of our object is in the center of that and that's just going to help us pick it up and putting it down we may change that in a future episode however right now we want it to be like this so in compile save and one final step in here is to select inspectable item parent self so this one up here and we're going to search for tag we're going to give it a tag i'm going to name this tag interactable and what i'm also going to do is just select all and copy it so control a control c so that when we want to use it i can just paste it in there so i know it's spelled exactly the same and this tag here is how we know that we want to be able to interact or pick up or inspect this item so you can call it inspectable as well if that makes more sense we can close that compile save and close the item as that is all we need to do in there the rest of the code is going to be in a character blueprint so let's open that up now so i'm just going to select it here and edit third person bp or you can go to content third person bp blueprints that button character in here we want to be able to interact with it so what i'm actually going to do is go to edit project settings once it loads i'm going to go down to input down here and i'm going to create an action mapping so you can see i already have one so i'm just going to delete that and make a new one so we have the plus action mapping naming this one interact and i'm going to make this the e key so when we press e it's going to fire off this event you can set this to whatever key you like and you can name one if you like as well and don't worry if you don't have any of these other action mappings these are just from previous tutorials again name it whatever you like give it whatever key you like it's just when we press this key it will fire off this event we can close that back in our event graph we can right click and we can now search for that so if i search for interact you see we have action events interact there so now whenever we press e it's going to fire off this event here and when we fire this off we want to then use a line trace to see which object we're looking at so which one we want to interact with so i'm going to come out pressed and i'm going to get a line trace by channel but the start is going to be our camera so i'm going to get my first person camera here this does work in third person as well however it just looks a lot better in first person and i do have videos on creating a first person camera as well so we come out of the camera and get the world location that is going to go into the start this is also going to go into an addition so effector plus the vector and that is going to go into the end and this addition here is to keep it going in a straight line what i want to add to that is just the forward facing direction of our camera so the line goes forward so i'm going to come out the camera again get forward vector out of this i'm going to get a multiply so vector multiplied by a float and this float is how far we want the line to go so i want to be about 250 as that's usually quite a good value for a line trace of interact and that obviously goes to the addition which goes in the start so again this line trace is going to draw a line from our camera's current location to 250 units in front of the camera having a line like that so if i just change draw debug type to for duration and then if i hit play we can see what this looks like so when i press e we're going to have the line go in front of us like so again customize this length be whatever you like i'm going to set that back to none like so then after this i'm going to hold down b left click to get a branch the return value is a condition execution going in there so this just means if we do hit something because if we do hit something we want to see what it is to see what it is we want to come up with the out hit and break hit result opening that up so we have all these different options here because we want to use the hit actor to see which actor we are interacting with and to see if we want to be able to interact with them we can come out of his actor and get actor has tag now if i go on non and hit control v you can see that's now got my interactive in there obviously if you didn't copy it you can't do that so just type in the tag that you have there on your interactable or inspectable items again this is just make sure that we are only trying to inspect with items we want to be able to inspect and we can close that move this up like so because again we're checking to see if a we've hit something and b if we can actually interact with it says hold down b left click again with a condition as that return value execution as true of that first branch so again we're checking it if we hit something and then after this we're going to hold down s left click to get a sequence because we want to fire off two different things now those two different things you want to fire off are going to simulate us picking up the object so we're going to do then one first as we want this to be done first so i'm going to hold down o and left click to get a do once node the execution going to then one and reset i'm going to right click and add a custom event naming this reset do once you can name whatever you like but i'm just giving it a very simple name like so and what we're going to do now is we want to have it so the player is now holding the object so to do that i'm going to be using a child actor so we can go to the viewport add a component and add a child actor like so i'm just going to name this item actor like that and i'm going to drag and drop that onto my first passing camera so wherever the camera goes the object goes with it so as if the player is looking around and just to test it i'm going to make the child actor class my item parent or inspectable item parent and now you can see we have that there so i'm gonna move this over and see where i want it so if that's still a bit big for you you can go into the army term itself and scale it down in fact i think i might do that so i'm just gonna open it up select the item and scale it down to maybe point one instead i'll go back and that looks a bit better again you can see this is where the item is going to be when holding it so i think there's going to be good for me i may also need to actually move this back down again let's have a look yes i do so let's move it back there compile and i think that's gonna be good for me again it's all about just customizing it to get it looking great for you the visuals of it completely up to you so if i just clear the path there we have it back as empty which is obviously what we want what i'm also going to do here is add an arrow and this is going to be a simple reference from where we want this to be so what i'm going to do is deselect this add component add an arrow like so because this is something that we can't see in game unless also we tell it to and i'm also going to put that on the camera as well the location of this i'm just going to make the same as the child actor so i can right click on the location hit copy go to the arrow right click location and hit paste so essentially we want to move the actor into that location so in compile and save that as we're just using that as a reference for the location we want to move it to now we can go back to the event graph in here and again completed the do once we want to pick up the object so we're going to get our item actor here or basically your child actor and out of this we're going to set child actor class with that going to the completed there and the class of this wants to be whichever item we're interacting with so very simply we can come out of this hit actor here and we can just get class with a return value going into the in class there so we're seeing what class of object and item we're interacting with so that's the one we're then going to pick up and spawn i'm just going to double click this to get some root notes to keep it looking nice and tidy as well as that we also want to make sure that we're picking up in the right location so to keep it in the same location what we need to do is convert the world location into a relative location that's very simple so what we're going to do is drag out the item exit again and set relative location connecting that into there like so and the new location again is quite simple what we can do is right click and get an inverse transform location so we're turning a world location into a local location return value going to the new location there the transform wants to be our transform of our camera so we can get our fpp camera there which is your first person camera and get world transform with the return value going to the t of this node here because the t just stands for transform and you can tell because it is an orange return value which in unreal means transform the location wants to be the current world location of the item so we come out of the hit actor again and get world location or get actor location sorry get act location there like so now what this is going to do is just turn the current items location into a relative location so we can then spawn it in on our blueprint where we want it to be and then we also want to destroy the actor which is on the floor or in game so it looks like we've picked up so again we're not actually picking up that object we're just giving the illusion we have so we're spawning in a new one in the same location but now it is in the character bp because now we have it in here it's a lot easier to interact with so we can come out of this hit actor again so i'm coming at the root node and we just simply get a destroy actor connecting that into there like so and again double click this to get some root notes just to keep it looking nice and organized so i'll give you a rundown of this once again so essentially when we're interacting with something we're going to get its current location we're essentially just going to take it from the world into the character bp because then it's easier for us to mess about with and because we're putting in here and because we're moving it from the world into the character we want to also remove it from the world as well hence why we're destroying the actor there off of then zero for sequence we want to hold down d left click to get a delay connecting that to then zero and i'll put the duration as 0.1 the reason we've got this delay is just so we're giving this code here enough time to fire off because we need to do this before the rest of it so we're just doing that here and the reason i haven't just done it all in one is because we have this do once here so we want this to fire up every time we want this to fire off every other time that will make a lot more sense once we do the rest of the code after the completed we're going to hold down b and left click to get a branch this branch is going to see if we're already interacting with it so if we want to pick it up or put it down so to do that we're going to create a new variable naming this one inspecting question mark compile default value of false and we're going to get that and then out of this we're going to get a not boolean and that one is going to go into the condition of the branch there so if we are not already inspecting with it so if we're not inspecting we're going to come out true and out of true we're going to set inspecting to be true our false we're going to set it to be false because if we are not inspecting we want to then start inspecting and if we are inspecting we want to stop inspecting so i hope that makes sense for you then to pick it up and put it down we're going to be using a timeline to create that simple animation so we can come out of set inspecting to true and get an add timeline and i'm going to name this one pick up item i'm going to move this out a bit because i'm going to do some code in here in a second and set inspect into false can go into reverse of that timeline because obviously if this is going to pick it up reverse is going to put it back down so it's very simple to do another efficient way of keeping it all in one timeline out of the update of this timeline so every time it moves along the track what we're going to do is set the location of our item so we're moving it so we can drag and drop and get our item actor there and just set relative location with that going in the update like so and then the new location we want to go between values of a and b so we can right click and get a lap getting a lot vector with the return value being the new location there and a and b want to be our start and end locations so we can right click on a promote to variable name in this start block we can right click on b promote variable limits end lock so now we have the starting and ending locations for moving our item and these are what we're going to be setting here so we then know what they are the alpha wants to be on the timeline here which is why we have the timeline so let's set up that track now that is a very simple thing to do we can double click on the timeline to open it up add a float track and i'm just going to name this one item track the name doesn't matter at all and we can also set the length so the length is how long you want this animation to be i'm going to give it a very quick one of 0.5 seconds then we can right click on the graph add key to curfload time of zero value of zero right click add key time of 0.5 or just your maximum length and a value of 1. so it goes from the start to the end of the timeline perfectly like so we compile and close that now you see we have this item track here and that we can go straight into the alpha of that lerp so we're going to be going between the values of a and b or the start and end using the timeline track here which is obviously going from zero to one in the timeline and again when we reverse it we're gonna go from b to a or end to start so let's now set up setting these variables so we know where the starting end is that's very simple to do the start is just going to be the current location so what we can do is we can set start after set inspecting there and we can drag and drop in our item actor out of this we can get relative location connecting that straight to the start lock there because the starting location is the current location of the item so that works then the end location is also very simple so we can set it there and this is going to be the arrow we made which is why i said this is a reference for where we want it to go that's why we did it now back in the event graph we can get the arrow and we can also get the relative location connecting that into the set and location there what i might do is just give myself a bit more space so just move this out a little bit like so but there we go now what's going to happen is it's going to go between the start and end location or the current location to the arrows location so that will then work perfectly for us so now we can pick it up and put it back down again we compile and save so what i want to do next is set up putting it back down again so we're picking it up now we want to set up putting it down so we're going to do that off of finished of this timeline so we're going to hold down b left click to get a branch with that going to the finished of the timeline and the condition is going to be our inspecting boolean so this is because if we are inspecting it we don't want to put it back down but if we're not inspecting it and we've just finished the timeline then we are putting it back down so we're coming off a false of this branch and nothing will be off if true so offer false we're going to get a spawn actor from class because like i say we destroyed it before to pick it up now to put it back down we need to respawn it the class is going to be getting the item actor here so get item actor and then get child actor class would that go into the class of the spawn actor there so now we're getting the class of the item to spawn it back in the transform we're going to make transform so that's for the spawn transform sorry and for the location we're going to do something similar as we did before so here we got the wild location into local now we want to go from local to world so here we've got inverse transform location now we just want to get transform so we're going to come out the location and get transform location not inverse just transform and for the t or the transform of this it again wants to be our camera transform so get our camera and then get world transform with that going into the transform there like so and the location wants to be the current location of our child actor so we're going to get our item actor here and out of this we're going to get relative location not rotation sorry get relative location connecting that into the location there so it's just going to simply find the world location from this relative location so it's a very handy tool to have and that is obviously then going to go into the transform here after this what we're going to do is right click the return value promoting it to a variable and i'll name this one placed item doesn't matter too much what you name it this is just because we want to be able to access this return value and having a variable just keeps it nice and tidy and after this we also then want to despawn the actor we're holding because now we've moved it from the blueprint back into the world we now want to remove it from the character blueprint again so we can get our item exit here and then just set child actor class and we'll put nothing in the in class because if we don't put anything in there it's basically just resetting it to nothing and then after this we're going to call function reset do once which is a custom event we made all the way back here to a reset and do once which means we can then pick up another item so again this is so we're picking it up and now we're resetting it we can pick up another one because we just put it back down so that will then work perfectly for us and then after this we want to have another line trace because we want to see where we put it so currently if the player puts it back down in the air it's just going to stay in the air we don't want it to do that we could simulate physics however that does get a bit glitchy with this system so i'm doing it a different way instead so i'm going to come up with the reset do once there and get a line trace by channel the start is going to be the current location of this item so we're going to get our placed item here get placed item out of this we're going to get the actor location so get active location and that is very simply the start of the line trace and then we're going to come up the return value of the get act location again and get a vector minus a vector with that going to the end because we're just drawing a line from the current location all the way down to see if it's hitting the floor i'm going to put a big number in the z so let's say a thousand so if the player places it really high up in the air the line will go all the way down to the floor and then it will move down to the floor that it hits actors to ignore we do want to do this so it does ignore itself because ignore self here is for the character blueprint we also want it to ignore this item so we'll come off access to ignore and we'll make an array within here we can just simply get a placed item connecting that in there like so very very simple and then after this we're going to come out with the out hit and break hit result opening this up once again to get these different results and we want to use the location because again we're setting the location for where we want it to go to so i'm going to right click on location promote the variable naming this new end lock connecting that into the output of the line trace there and i'm also going to close this break it result like so then we've got a new end we also want a new start so i'm going to add another variable naming this new start lock and i'm going to make this one a vector like so setting it after the new end there and what i'm going to set it to is just the location of the item so we can get placed item here and out of this we're going to get actor location connecting that into there like so so again it's going to go from the current location of the actor to the new end location which is going to be the floor which we'll find from this line trace here so this just means it's going to be placed on the floor and to make this nice and smooth again what we can do is simply just come out of here add a timeline once again i'm going to name this one fix lock so we're fixing the location and that one's going to the play from start not the play double click this add a float track i'll name this fix lock track and the length i'm going to set to once again be 0.5 seconds so half a second and then on here i'm going to do the exact same thing so add key to car float time of 0 value of 0 right click time of 0.5 and a value of 1. and that's right click add key recompile and close that because again it's the exact same thing we did before so you should know what it's doing and why we've done it and then we'll come out of that track once again and get a lap vector again the exact same way a is going to be our new start lock and b is going to be our new end log so once again we're going from the start to the end and to move it we're going to get our placed item so get placed item and out of that we'll set the actor location set actor location once again go into the update of the timeline and the new location will be the return value of that look there and that should be this now working for us so it's quite a lengthy bit of code so my apologies for that but again this is one of the more efficient ways that i came across doing it when testing this out earlier because we're moving it from the world to the character and back to the world again so once again this is for picking up and putting it down and the actual inspection will do in a future episode so now let's hit play to test this out and actually let's minimize this and place one in the world first so inspectable item parent there hit play go up to it we press e we picked up we can move around we press e again we're going to put it down and it goes back down to the floor and that works perfectly for us and if we pick it up and then go all the way up it should then go back to the floor like so again this works perfectly for picking up and placing an item in game as you'd like so i think that'll be for this video as we've done everything we want to do we've started setting up our inspection item but again this video does work independently for just picking up and putting down an item like so again you can do whatever you like with this code this is more of just a general pick up and put down so thanks so much for watching i hope you enjoyed and i hope you found it helpful and if you did make sure to like subscribe down below so thanks so much for watching and i'll see in the next one [Music] bye
Info
Channel: Matt Aspland
Views: 52,707
Rating: undefined out of 5
Keywords: ue4, unreal engine 4, unreal engine, tutorial, ue4 tutorial, unreal engine 4 tutorial, how to make a game, how to, 3d modelling, blender, unity, games design, graphic designer, ue5, unreal engine 5, inspect, item, inspection, system, inspect item, ue4 inspect, part 1, pick up, place down, move, objects, object, items, item inspect, moving, place, placing, picking, light, lantern, table, floor, turn, rotate
Id: aR7PVYo2ZGo
Channel Id: undefined
Length: 24min 41sec (1481 seconds)
Published: Sat Apr 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.