Switch Characters In Game By Walking Up To Them - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to the mentioned four tutorial in today's video i'm going to show you how to switch between different characters in game so what we're going to do is we're going to walk up to the character press e and we're going to switch characters so let me hit play and show you what it's going to look like so we can get in i'm playing as my swat character here if i walk close enough to this other character it's going to say press e to switch characters if i press e we've now switched characters and i'm now playing as this one instead we do everything we want to here if i go back i'm going to switch characters again and so this is what we make today works perfectly this is a great little mechanic you sometimes see in games so we'll be making this today so let me delete this code and i'll show you how i've done it so what you're going to want to do first is obviously have your two different characters so all i've done is i've got my swap character here which looks like this which i made in a previous video and then i've just also duplicated that to get the normal mannequin back like so so i have my two different characters which will work independently and have following different codes in them so this is what i've got you're going to want to make sure you have that as well and once you've got those we're going to want to open them both up so i've just done that now so i've got the third person character one and swat character both opened up here in one of these what we're going to do is in the viewport we're going to add components and add a sphere collision now it doesn't matter which one you do this in first as we're going to copy all over so once you've got the sphere collision in here you're going to scale it up to the size you want so essentially if the other character is inside of this sphere then they're going to switch characters so if you want the other one to be able to be this distance from this character then that's fine habit is that so i think i'm going to make it a tiny bit bigger and i think that's going to be good for me so that's the sphere that i've got here and what we want to do is change the collision settings on it so if we scroll down on the right down here under collision we're going to change it from collision presets to be custom and this is because we don't want this to actually trigger anything else for example we have a door based on a box trigger we don't want this to set off as well we don't want this to be the actual collision we only want this to be for pawns so we've got custom we're going to press ignore at the top so everything is on ignore and then find pawn and put that as overlap so with this sphere collision here we're only going to be checking for another pawn which is obviously our other character so once you've done that we can compile and save that and what i'm going to do is select that sphere ctrl c to copy it go to my other character and hit control v to paste it in here so now i have the sphere in here the same size and the same collision settings and then we can compile that and go back to our other character to finish off this code so if we go to the event graph we can right click on our sphere collision and the components list up in the top left add event analog component begin overlap i'll just move that down there right click on it again and add event add a component end overlap so we have to begin and end overlap events for the sphere collision out of other actor we're going to cast to our other character and that's because this is what we're checking for so out the other actor we want to see if that character has overlapped it so for me i want that to be cast to third person character one as i'm currently in the swat character i want to cast to my third person character one as it's the other blueprint because again we're checking to see if that character overlaps this sphere collision and then we'll do the same for the end of up as well so cast to the same character there so third person character one for me after this on the begin overlap we're going to create a widget so create widget there and we'll create that actual widget in a second but our return value we're going to add to viewport and we'll come out the return value again and get a remove from parent which is going to be connected into the cast off of the end overlap and this is just going to simply tell the player to press e to switch characters putting that on and off screen again we'll make the widget in a second then if we hold down g and left click we can get gate the open will be an add to viewport the close will be removed from parent and that's like it sounds we're going to open it close the gate so we can then press e if we are close enough to it so let's press e so i'm going to right click and get an e keyboard event plugging the press into enter there so if we are close enough to it and we press e we can switch characters which will be out the exit of the gate now you can use whatever button you like it doesn't have to be e and you can create an action mapping as well so i'll do that to show you because it's slightly more efficient so we're gonna go to edit project settings once it loads we're gonna go down to input down here open the action mappings plus action mapping i'm going to name this one switch characters and i'm going to change it to be the eq again it's going to be e f left click whatever you like button to be e and the benefit of doing action mappings is we can have multiple keys keys for different consoles and we can also set up key bindings which is obviously allowing the player to change which key does what so we can close that and let's delete the e key there right click and search for switch characters or whatever you've just named it pressed still going to the enter of that there like so it's like say if we're close enough to it and we press e or whatever button you have we're going to go into this code here this code here you want to come out of azter person character 1 or just out of your cast i'm going to get a possessed node making sure it's spelt like that then untick context sensitive and i get possessed there under pawn i'm going to right click and enable contact sensitive again plug that into the exit the import stays as that character and the target is going to be get player controller like so and so that is what's going to actually switch the characters so now we can switch the other character so what i'm going to do is before i compile i'm going to actually create that widget so if i minimize this right click go to user interface and create a widget blueprint i'm gonna name this one switch characters widget like so opening that up straight away and all i'm gonna do in here is just put in some text so you can do whatever you want in here like say i'm just going to do some text anchoring it to the bottom middle ticking size content and i'm going to change the text to be press e to switch characters and i'll put the size to about 40. i might give it an outline as well again you can customize this to get it completely how you want really customizing it to get it to look great for you and i'm just going to move that into the middle like so so that is the basic widget which i'm going to have it's just going to simply say press e to switch characters so compile save close that back in our character blueprint here in the event graph we're going to go to class and get that switch characters widget there in the create widget compile and save that i might just move this out a little bit like so and now this will work perfectly going from this character to the other one however we also want to be able to go back so what we can do is we just select all of this code ctrl c to copy it go into the other character and since we've already put the sphere collision in here and go to the event graph and hit ctrl v to paste this code and now all we need to do is change the casts and this will still work the same so if we delete these casts here out of other actor we're going to cast two this is now our other character which is the swap character so swat character again use these for whatever they are for you so again i'm casting to this one up here so then that we're obviously going to create widget put that again down here of other actor for the end overlap going to remove from parent there and then also as what character in pawn for the possessed there and now this should work perfectly for us so if we compile and save this we can minimize and hit play to see if this is working so i'm playing my swot character here we can do everything we should normally be able to do we go close enough to this character it's going to say press e to switch characters we walk away that's removed from the screen if we go back again press e we've now switched into this character and we can do everything we want in here and if we go back press e we've searched characters once again so this works perfectly so i think that'll be it for this video if you've done everything you want to do we set up this system in which we can walk up to a different character and press the button to switch characters like this which again you see in different games sometimes roleplaying games or anything like that we just walk up press a button and it switches characters which you're gonna play as like so so thanks so much for watching i hope you enjoyed it and i hope you find 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 [Music] you
Info
Channel: Matt Aspland
Views: 14,101
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, games design, ue5, unreal engine 5, change, character, characters, toggle, how to change, button press, input, hotkey, key, press, button, easy, easily, quick, quickly, efficiently, pawn, pawns, posses, how to use, how to posses, multiple, different, when, near, walking, up to, to, up, them, switch, in game, in, game, in-game, close, enough, e key, how to switch, rpg, real time, pressing, a button
Id: qQyM7lSEzyw
Channel Id: undefined
Length: 8min 20sec (500 seconds)
Published: Sun Feb 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.