Use A Magnet To Bring Objects To You - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to another engine 4 tutorial in today's video i'm showing you how to create a basic magnet in which we can just left click and pull an object towards us so let me show you what this is going to look like now so we get in we go to this object when i hold left click it's going to come towards us and we can move around with it if i let go it's going to go back down as well and you see if we just hold and let go it's going to stop like so so it's just a basic magnet which will pull objects towards it so recently i did a grapple hook which takes you to an object now this is just going to bring an object towards us so this is what we're making today it looks similar to inspect item system but it's not so i will probably do a video of that in the near future of an inspect item but today we'll be making this so let me delete this code and i'll show you how i've done it so our first step we want to take is when to open up our character blueprint so for me that's the third person character so that's content third person bp blueprints third person character but for you this could be third first whatever you've named it once we're in here we want to find some empty space i'm gonna scroll down here get some empty space we want to get an action mapping so we're going to go to edit project settings once this loads we're going to scroll down to input down here i'm going to hit the plus action mapping here i'm going to name this one magnet you can name this absolutely whatever you like but that makes sense to me i'm going to set this to be the left mouse button again set this to whichever key you want as well so you can have this as e or f or anything like that and the benefit of action mappings is we can have multiple keys set up for different consoles we can also set up key bindings so once you've done that we're going to close that and then in the event graph back here we're going to right click and search what we just named it so i called mine magnet like so and now we have it there now this is going to fire off our code to activate and deactivate the magnet now you can do this on whatever you like so if it's a gun you can do it from your fire which you have or anything like that any sort of code this is just how we're going to be firing it off so what we're going to do next is we're going to hit the plus variable and we're going to call this one magnet on question mark leaving it as a boolean will compile and leave its default value as false so it's not on off of pressed we're going to set that to be true so take it enough of released we're going to set it to be false so leave it unticked and so this is just us telling the code that when the player is holding down the left mouse button they want the magnet to be on and when they release the left mouse button the magnet is off so then we need to set up the code for the magnet being on so we're going to do this using custom event so we just go over to the right find some empty space right click and add a custom event like so i'm going to name this one magnet on as well or event magnet on anything you like and out of this we can hold down b left click to get a branch plugging that in there like so condition of this is going to be magnet on so we only want to fire this code if the magnet is on and the reason we're doing that is because this is going to end up being a loop so to break the loop it's just basically if the magnets off that will break the loop off of true all you want to do is you want to get a line trace by channel like so and to draw this line what we're going to do is we want to get a reference to our camera so that's the fpp camera for me but for you it could be follow camera or well if you've got it named as it works best in first person but it does also work in third now with your camera reference we're going to get world location now for return with this we're going to get an addition so a vector plus a vector and then the return value of just the get world location goes into start so don't do anything with the addition just yet out of the camera again we're going to get forward vector so the forward facing direction out of this we're going to get a multiply a vector multiplied by a float and the value of this float is how long we want the line to be so i want this to be 1500 but you can set this to a thousand two thousand five hundred any value you like and then this is going to go into the other value of the addition in the addition it goes in the end so what we're doing is we're starting the line from where the camera is and then we're gonna go 1500 units in front of the camera and have that at the end and the reason the addition is here is just to keep it going in a straight line what we're going to do is we can put draw debug type as for duration so we can test it but i know this works so i don't need to so i'll leave it as none everything else will keep the same after this we're going to hold down b left click to get a branch plugging that into there like so the condition of the return value and this basically just means if the line trace has hit something so if it has we want to see what it hit if it hasn't we're going to loop it so we'll do that in a second so out of the out hit we're just going to break hit result like so open up the options there and we're going to come out of hit component off of hit component we're going to get is any simulating physics like so because we only want to use the magnet on the object which is simulating physics now you don't need to do that if you don't want it doesn't stop it from working but it obviously does if we're using this this is just how we're going to be doing it now you can do tags as well so you can have a tag as magnetic but i think the physics works just as good if not better it's easier for us to implement it's just quicker so after this we're gonna hold down b left click to get another branch with that return value as a condition this branch going to the true of the first branch so if we have hit something we're going to see if it's simulating physics if it is so for true we're going to right-click the hit component again and promote the variable and call this magnet target like so again set that off of true so what this is doing is it just means that this component here is the target for the magnet so it's the object we want to be moving i'm just going to double click this to get some rear nodes in here like so off of false i want to do is i want to hold down d left click to get a delay setting this to something very small like 0.001 plugging it into the fault of that wrench and the fault of the first branch over here like so i'll tell you why we're doing that in a second off of the completed of this we're going to simply call function magnet on and so what this is doing is if we don't hit anything or the object we have hit doesn't have physics simulated it's just going to loop this so it's going to check again and so it's going to be constantly looping and checking until we turn the magnet off or until we hit something which we want the magnet to move ie the magnet target here so after the magnet target we want to move the target so that's something else we need to set up as well so we're going to do is we're going to come down underneath this i'm going to right click and add another custom event and this one is going to be called move target like so again you can name this whatever you like this is just what makes sense for me actually also before we set this up what we need to do is off of our input action magnet here of set magnet on to be true we're going to also call function magnet on so we're telling the code the magnet is on and then we're also actually activating it and calling this code here to constantly loop until we find something we want to magnetize and pull towards us or until the magnets turned off so again once we hit something we want to pull off this event here to do this event we want to get a reference so we can move it towards us we want to get a reference of where we want it to end up so to do that we're going to go to the viewport here i'm going to add a component i'm just going to add a cube like so actually i'll rename this one to be magnet reference so this is where we want the object to end up so i'm just going to move this out in front of the player to about there so i think this is where i want the object to be once it's moved towards us you can put this anywhere you like i think that's going to look good for me and what i want so i think that's good like that what i want to then also do is i want to scroll down until i find the collision settings i want to just disable all of its collision so untickle these no and set it to no collision i also want to untick visible and tick hidden in game so the player doesn't see this at all it's as if it's not there there's no collision and they don't see it we're only using it for a reference so we know where to move the object to i'm going to go back to the event graph over here like so as now we have set that up so now we're going to do of this move target event here is we're going to get a reference to our magnet target so we set that variable earlier so we can drag and drop get alphys we're going to set simulate physics to be false so leave it unticked so we want the physics to be simulated for us to attach to it and to get it we want to turn it off because if we leave it on once we move it towards us it's going to start glitching out and it will eventually just fly off out the player's hand and it will glitch so if you want to see what that looks like you can leave that ticked but it might make a cool thing that you want as well because it means that the player can only hold it for a certain amount of time before it just lets go the magnet wears off anything like that but again i'm going to leave it like this and after this i'm not going to connect up yet i'm going to right click i'm going to add a timeline like so and i'm going to name this one move target t for timeline like that i'm going to double click this to open it up straight away i'm going to set the length of this to be 1 like so and then we're going to add a float track here now you'll probably want to leave the length as one as well and i'll show you why after we finish making this this flow track i'm just going to name move track like so i'm going to right click add a key to curfload time 0 value 0 right click add another key time one so the end of our timeline and value of one so again it goes from the start to the end like so close the timeline like that then what we want to do is open up our components tab here and get a reference to that move target t timeline there we get that out of this we're going to set play rate like that plugging that after the set simulated physics and then into the play from start of the timeline making sure it's the play from start so that every time we do it it always goes from where the object is to our object magnet reference this new rate i'm just going to set as 0.3 as i want it to be a little bit slower than one second so obviously you can set this up as the length in here but this is a little bit better because then if you want to create a dynamic system which i did in my grapple quick video you can then do that here as well but i think point three is gonna be good for me because it's gonna be a fixed distance anyway a fixed maximum distance that is because that's the line trace so i think a rate of 0.3 is good then after this this is where we actually move it so off of update we're going to move it so what we're going to do is we're going to get a reference to our magnet reference there so drag and drop in there this we're going to get world location so we're getting the location of where we want the object to end up we're going to right click that return value promote your variable i'm going to call this one player location or you can call this reference location anything like that but this makes sense for me and we're going to plug that into update so every time the timeline updates so when we are moving it we're going to be resetting the player location so if the player is moving then it's going to reset this like so so it always ends up in the correct place then after this we're going to drag and drop a reference to the magnet target here get magnet target out of this we're going to set world location like so again plugging that into the set plate location there so every time the timing updates we are actually moving the magnet target as well so that's the object we'll set up the new location in a second so what i'm going to do now is out of the move track i'm going to get a lerp and a lob vector with the return value of that going to the new location there so this is going to go from a to b using our timeline a we want to have as the actual magnet target location so the object so we're going to get another reference to the magnet target out of that we're going to get world location like so plugging that into a like that so it's going from where the object is and b as we want it to go to and that is the player location which is this variable we set up here so we just drag and drop that in to b like that and now this will work perfectly for us so this is it done of actually moving it but what we need to do is offer finished i want to just call the function move target again like so so that it is looped it stays there so it's going to loop this timeline always moving it to where the player is going so if you move around it moves with you until obviously we decide to stop it which we'll do by just stopping the timeline then the final step is to go back up to our magnet input action here off of false so the released we're going to call the reference to our timeline here so magnet target t out of that we're going to stop like so so stop the timeline like that which is what i mentioned a second ago and then we're going to get the magnet target get magnet target we're going to set simulate physics back to true so plug that in there and tick it again like so now if we compile and save this this should be the code done so let's test this out now so if we minimize this hit play to test it you can see we have that cube there so actually i'll show you how to do that as well so i this is just a cube i drag and dropped in here and then all i did was i just scroll down and tick to simulate physics and that way we can use it as a magnet target so let's try this again so now if we left click you can see nothing's happening that's because we forgot one big step so let me do that now i forgot to call this function here so we've got the move target set up but we need to call it off of this true branch up here so we have true set magnet target we also need to just call function move target like so just forgot to do that now for compile save this will work perfectly just quickly before i end it while i was editing this and i was coming to make the thumbnail i realized that there was actually a bug in this so if i hit play i'll show you what i'm saying so if we just hold down left click to use the magnet but we don't hit anything and then we let go when we exit we're going to get this error here access and on try to reproperty magnet target and when is it it's when we're trying to set the simulated physics if i click that it will take us here so what's happening is it's trying to access this variable here but we haven't set it yet as we didn't hit anything here so an easy way to avoid this is we're just going to move this out a little bit so move the set simulate physics come out of the magnet target and get an is valid node with the question mark there putting the execution of stop is valid we're going to set simulate physics and is not valid will not go into anything so we're only going to set simulate physics to true if we have a value in the magnetic target variable here and this should now fix this problem for us so if we get rid of that hit play do the same thing so i'm just holding it down here let go and you see we don't have the issue the error there anymore so like i say that's just a quick fix to a bug which i found when coming to make a thumbnail for this so we go back and now we can see we left click we moved the magnet we turned the magnet on sorry and moved the object towards us like so we're moving around it stays with us if i let go it's going to be put back down like so and we can do this multiple times i'll do it with this other object as well like so and this works perfectly so i think that'll be for this video if we've done everything we want to do we set up a magnet in which we can hold down the button and the object will just come towards us and we can move around with it and stuff like that and then we can let go whenever we don't want it anymore like so 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 thanks so much for watching and i'll see in the next one you
Info
Channel: Matt Aspland
Views: 10,166
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, blender, games design, graphic designer, ue5, unreal engine 5, magnet, grapple, system, hook, bring, object, item, inspect, move, towards, you, player, cube, box, physics, simulate, simulating, bring to you, me, drag, pull, timeline, opposite, smooth, quick, easy, basic, simple, close, closer, push, away, from, the, hold, toggle, held, holding, by, ue4 magnet, ue4 grapple, ue4 inspect item, ue4 bring item
Id: xwZaDdrl8ok
Channel Id: undefined
Length: 14min 31sec (871 seconds)
Published: Sun Jan 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.