How to Make a Hunger System and Eating in Unreal Engine 5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys welcome to New real Engine 5 tutorial today I am going to show you how to create a simple hunger system it's going to be a very easy way to follow so let's get started all right so the first thing I'm going to do is open up my first person character blueprint or whatever character blueprint you're using and what we need to do is basically uh go ahead and create a new variable in this case this variable will be the current hunger so let's just name it hunger and let's change this to a float so it can be a nice number with decimals now let's just compile and put a default value for this hunger in this case let's begin with around 70 of hunger right and let's say that the maximum will be for now 100 so now what we're going to do is create two different custom events let's right click create a custom event and the first one will be increase hunger all right and then we'll right click create another custom event which will be decrease hunger all right so now with that said when we increase hunger we're going to get the hunger variable get it and just add it by a certain amount for example three right just as an example we could even pass this as a parameter which is even cooler so we could put like amount and then we just need to get the hunger set it and then it will be this new variable so it will update it right great so with that said we just need to do the decrease hunger which will be very similar right but also let's first of all make sure that the h will not go more than um 100 so we're going to check if this is going to be um bigger than 100 and if it is bigger than 100 we are simply just going to go ahead and get hunger and set it to 100 okay so we're going to make sure that it will not go over 100 now for decrease we're going to do exactly the same thing but of course the inverse so we're going to get Hunger and instead of adding it we're going to subtract it by what will an incoming amount let's just name this amount right which is a parameter input from the custom event and then we're going to go ahead and just set the hunger after this so we will update the variable right and then we're going to make sure that hunger will not go less than zero okay and if so we're going to set it to zero and you sprint uh hunger right hunger uh damage and we will just go ahead and simply do uh apply damage right and what we can do is apply damage to our character in this case damage actor will be self so we'll damage ourselves and then put a base damage of Imagine uh two right now we will not cover of course a health system in this video but I do have many of those so if you want to check that out go ahead and have many in my channel how to make a health system then you can interact these two nodes with that but anyway now of course we need to make use of this two notes but there's one thing that I want to do first which is Showcase this on a slider right so we can see it on the UI so for this let's go back to our content browser let's right click create a new user interface which is going to be a widget blueprint let's create a user widget and let's name this something else that you be uncore hunger let's open this up and the first thing you have to do is go and add a canvas panel into our hieres that way we can start to add things into our screen then let's get a progress part put it I don't know whenever you prefer let's put it like top on the um bottom left sorry and let's put the anchor to be at the bottom left now let's just go ahead and just change the size X and Y so it's a bit bigger now I'm going to make it a bit ex saturated okay so we can see better for the tutorial and then the thing is that this percentage will go from 0o to one not from 0 to 100 like we have it on our uh first person caus blueprint so just for you know a A visual representation let's put 7 as if we had 70% I'm going to change the field color to something has more orange as is normally like that in survival games okay and uh the thing that now of course we need to update this percentage value depending on this hunger variable right so how do we do this well what we can do is create a binding so if we go to the percent we can write click sorry left click create a binding and now we will create a new kind of function so what we can do here is basically just um get the player character that is in our level which will be our player and we're going to cast to the first person character blueprint or whatever character blueprint you're using to access that variable now we can get health okay which should be sorry not get Hunger not get health sorry about that and now as I mentioned before that value goes from zero to one not from 0 to 100 like in this one so the only thing that we need to do is divide this by the maximum hunger which which in this case is 100 and now that would return us a value between 0 and 1 and we can plug it in so now the only thing left over will be to go to the first person G blueprint go to the begin play which is when the game starts and right at the end of the input and all that stuff I'm going to go ahead and use create a new widget which in this case will be the one that we just created which is hunger and add this to the viewport so it be in the player screen and now with that said you will see that if I go and press play we in indeed we have our hunger bar at the bottom left and everything is linked but the only thing is that of course we are currently not doing anything with those two custom events right we are not making use of them so nothing changes so just to test okay let's just right click go and put for example uh the one key all right so let's type the bug key one which is going to be uh kind of for only debugging testing and let's put that when you press it okay we will call the the increase hunger and then we will just put an amount of maybe five okay and then just right click the bug key2 all right and let's call in this case the decrease hunger okay and let's put uh five also three or two let's put two so now if you see and I press play you can see that if I go and press one in my keyboard will go ahead and add as you can see only until 100 and then if I press two I subtract okay and everything works so the only thing we need to do is kind of use this to custom events so what we're going to do is go ahead and just delete these two things and I'm going to go to the begin play which is when the game starts right let me separate this a bit and what I'm going to do is basically go ahead and just call the decrease hunger uh in a loop okay every so often we will decrease the players's hunger so we will need to eat right uh so let's just go in the begin play right when the game starts and we will call this a note which is the set timer by event so every so often in this case let's put it as every second in a loop so let's take this we will call a specific event what event well in this case we're going to make use of this uh decrease hunger event so we actually can bind it so let me go ahead and move this things around okay there we go so it's a bit closer and I'm going to go ahead and use just drag event to this other okay so actually we cannot do that we need to create a separate event but it's okay let's just call this decrease um hunger uh timer okay and in this case we will just simply call decrease hunger okay as simple as that now let's go ahead and just put an amount for example we can put um two just to put something right or uh one it's going to be a bit better because is every second and with that you can see that when I go and press play at the begin Play We Will begin a timer that every second as you can see it is decreasing by one okay in a loop it will never end and it's noticeable we can maybe put it to be more often right and then the only thing we need to do is of course go ahead and um you know um increase our hunger when we as you can see that's decreasing if you play close mention to the bottom left so to eat let me quickly just create a little item that we can interact with we you know will be kind of a fruit so let's right click go to blueprint class create a nectar let's call this something as bpor fruit and let's open this up now let me just add a simple static mesh which will be a simple sphere uh which actually is going to be easier if I just search here sphere there we go and this will be my prototype model okay just for now let's put a point three for example and let's change the you know uh this to kind of orange okay imagine it is a fruit okay and now the only thing going to do is go and make sure that this sphere okay has Collision which will be required to be block all Dynamic okay that's good and then I am going to go and create an interface so we can interact with this fruit so for this is right click go to the blueprint section create a blueprint interface let's call there something as BPI uncore um eat okay we can we can just name it even interaction something like that but in this case going to be easier if we just name it eat for this example so in here we'll exactly go eat all right and we will not need any inputs or outputs for now so we can just compile save and now just um go to our uh fruit and eat but there's something thing that we want which actually I just thought about it and we want to now put because for each fruit you might want a different amount of hunger filling in so in this case let's add a new input parameter which will be the hunger amount that we will add and as you guessed it will be a float so now with that we can close this and just go to class settings in our fruit blueprint and just add that interface which is BPI eat and now we have this new function which we can double click to open this up so from outside in this case our player when we see this fruit in our level we can interact with it and we will call this generic eat event which we will do basically a simple thing and just fill the um players uh you know hunger okay which will basically output that um that amount what amount well let's say for example we want 10 okay we will do 10 and we will also print a little string which will be like um eating right and there we go and we've done really need to do anything more we could just simply play a sound okay 2D so we can just do like um I don't know an interaction like for example this camera shutter of course that's not really of eating but you get my point and that's it now in the player what we need to do is detect this so let's just go to an empty space right click go and type the E key okay so when we press the E key what we would do is something called a sphere Trace by Channel and this will be an inv this will uh right cast that will go from one point to another and we will detect whatever is in front of us okay in this case we're going to go ahead and use get the full camera okay do way get um world location and that is where it will begin our line trace and then we want to do another one which is the forward Vector so we can get at the direction that we're looking and we are only going to go ahead and multiply this by right clicking converting this to float so it's a nice number let's put like 250 okay so this this is the distance that we will be able to interact with that fruit let's add these two vectors together and this will be our end point now because this is a sphere radius we need a radius so let's put something as 15 and then the trace channel will be visibility because uh it's basically what the Collision will have on fruit if I check here make sure that it is block all Dynamic and visibility is checked to through cool so now on here the only thing we need to do is just put this to for duration for the bug and you can see that when I go ahead and press play and press the E key you will see this right cast going on so if we have a fruit here we can interact with that fruit so let's go ahead and just make sure that we will only continue with a branch if we have hited something and then we can get this outad break it and this will give us all the parameters about what we have collided with in this case we are going to interact with the hit actor so let's just add a dust interface Implement interface uh node which will detect if we have that eat interface if we do have that eat interface we are just going to go ahead here and call the eat message okay the E function from the BPI and the cool thing is that because we created a function we don't need to go ahead and cast to BP fruit okay and then call the eat uh function from there okay we directly can go ahead and do it generically with this interface so we can create different fruit Blueprints and this function will be Universal and it will work for all of them and now the only thing I need to do is just call the increase hunger uh custom event that we created and pass the amount as a parameter and that's it if I now go here into my world go and drag my fruit can I make it like this right go and press play and go and interact with this fruit okay boom as you can see I'm increasing my Hunger of course right now we have the loop very uh fast so instantly it will also decrease the hunger uh we can maybe kind of play around with that with the timer but you get the idea and one last thing going to do is just you know delete the fruit after we have eaten that fruit so let me also increase this to maybe like 30 and now the thing is that in here we cannot simply go and do a a destroy why not well because it will never reach the return node so it will not give us the amount that we want so what we need to do instead is basically you know do it with a bit of delay but we canot add a delay here because we we cannot add delays in functions so instead we need to do a quick like kind of you know patch which is good to go to the main event graph right click create a custom event and call this destroy fruit right and then in here we can exactly add a delay of uh I don't know maybe like like1 second and then indeed destroy the actor which will be S and now from here we can go and call that destroy um fruit custom event that we created and it will work corly because in here it will reach this because we have a small delay so we will leave that space and now yes if we go back to our map or level and just press play go to our fruit and itat it boom we will fear our hunger and it will be destroyed and then we go we're good to go so that's it guys if you found this tutorial helpful I would really appreciate it you could like the video and subscribe to my channel remember I have full access to the Prof file through patre or YouTube members check out my Discord server you know it's a lot already more than 6,000 VI deaths already so go ahead and join us um follow me on my socials and now yes with say byebye Happ
Info
Channel: Gorka Games
Views: 2,355
Rating: undefined out of 5
Keywords: games, videojuegos, unreal engine, unreal engine 5, ue5 hunger system, tutorial, quixel, megascans, unreal engine eat, hunger progress bar in unreal engine 5, ue5 eat fruit interaction, survival hunger tutorial, widget hunger, unreal engine 5 how to consume food, unreal engine 5 eat interaction, unreal engine 5 simple hunger system
Id: T9LaC-tqT0M
Channel Id: undefined
Length: 15min 59sec (959 seconds)
Published: Tue Feb 20 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.