The Best Way to Create an RPG Player in Unity for Beginners in 2024

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so let's talk about RPGs they are without a doubt one of the most popular game genres of all time as I can personally tell and what I like the most about RPGs is that they're relatively easy to create with all of them having one thing in common which at the same time is also their most important mechanic which is nothing less than the player so first what we need to do is adding the player game object itself and in order to do that we can simply use an empty game object and like call it player so the name is self-explanatory and doing it is as simply as clicking right and then selecting your respective item within the hierarchy and in case you're a beginner most of those game objects are also going to be those very objects you can find in your finished game later on if you want my personal help on this please feel free to join my premium course next if you can see in the inspector there are any components attached to your player game object make sure to remove except for the Sprite renderer in case you want to make your player character the same style like me which is like this player style you know and love from Paper Mario and as the name suggests this very component is going to render a single Sprite so a two dimensional a surface if you will containing a texture right within your Unity project and in case you got some awesome Sprites sitting in your project already please feel free to just add them to your Sprite renderer by dragging and dropping them into the spread field next I wanted to change its position and scale a bit by hitting W or R I think it is and then using the mous two position or scale it and since I initially felt it was a bit off having that Sprite floating like just in the air I also added a shatter to it by just adding yet another Sprite and placing it right below the player heading up and making it making it very dark by changing ing the color attribute from its respective Sprite renderer and even though you can tell it's like not the briest one of all um I felt like it was fastest to just use the standard um knobs right here and later on also um made it slightly transparent Again by using the color field from this this bright Rend and since as far as I'm concerned there's no a um where the player Falls right to the ground so that's why we also want to add a a collider to it and in my case I found it was the easiest to just use a box collider here whose borders you can simply Dr so it you feel like it fits your player finishing those components off by adding a rigid body to it to which is un's component which is characteristic for enabling like physics like gravity and and such so now that we got our player object up and running let's continue by creating our very first first script by clicking right and then choose and create and C script which we can then already attach to the player two to add Its Behavior to it that you're going to code with me over the course of this video so buckle up and then let's open our player script by double clicking on it however before starting to code you might have realized that some RPGs like I believe the old Final Fantasy game steart have a distinct player for the the game's overw world so if you want to add a similar one to this to I would encourage you to use a whole another game object for that and also a whole another script as your Overworld players control is most likely differ from your um original one as in most cases it's just a Sprite moving across the map without the ability to jump or to attack whatsoever you can always feel free to ask me for my personal help on it on my premium Discord so what mechanic of the player we we could start with well at that point it's only a floating Sprite so how about adding its basement its basic movement first and what I like to do with new script is clearing things up by removing all of its methods and also those unnecessary usings right at the very top now to be able to move the player we want to be able to change one important vir within unity's inspector which is the the player speed and we can tell Unity um our player should have this attribute to it by writing public float because we don't we don't only want it to be an integer and then well just use speed here so it's self-explanatory again now as you can see you're already able to use your speed variable and input input any value into it like two and that's it however Unity doesn't yet know what to do with that value so that's what we want to teach Unity next and if you think about it moving the player has definitely something to do with um manipulating its physics like its velocity so the component we need to ask in order to do that is our rigid body and the way we can tell Unity through the script we need to do we want to do something with the Richard body component is by typing Richard body which body at um the very top and then opening the awake method you can see right here which you can think of as yes some kind of micro program or code snippet which is being executed automatically at the very start of the game or even before it this is where we want to assign the Richard body handle we got within our code to the exteral component within Unity which you can do by by typing in Richard body equals and then get component rigid body again all of this is not going to make our Player move unfortunately so that is why we want to open yet another method which is the fixed update method which is being which is getting called continuously and I'd encourage you to use it for um yeah for all things related to um to the users input for the users inputs like yeah movement and right in there we want to ask the players rigid body component to add a velocity to it in other words to make the player move and the easiest way and safest way to do that is by just siding a value to to um the Richard Body by using Richard body velocity and then assigning a new Vector 3 to it which is just a data type containing three three different attributes called XYZ as you might have guessed this one is pre like predestined to become um yeah to be used for positions and um yeah vectors and stuff and as you can see I'm going with the notation here of telling the attribute you want to assign within the within the definition although you don't have to write X colon Y colon and Z colon here but I feel like it's it's easier for you then then the Y component of the Y value we don't want to change at all here so make sure to use the value it already has by using Richard body. velocity y here and do never ever use zero here as it would like cancel out um our jump later on as we're about to apply a vertical Force there and by setting it to zero again we would eliminate it basically then for x and z we want to be using input. get AIS horizontal or vertically or vertical respectively which will make the x and z value depend on the player input which on the keyboard is Like A and D for horizontal and W and S for vertical and the reason why we're using get xes here is because we want the movement to have a smooth start in beginning as as this will either be zero in case the pair is not doing any input at all or anything from zero to um one with one included depending on how um long the player presses the respective key or how hard the player pushes um like the stick on on the controller and also please make sure to not be like anod like me here and um make sure to somehow include your speed Fireball we actually created for that very purpose um within here by multiplying the horizontal and vertical input with speed so that it actually has an effect on the player's movement and as you can see well our player seems to be somewhat moving however it's yeah there is also falling right through the ground and it's all in all behaving um in a strange way and the reason for that is because our rigid body component which I told you this characteristic for a game object's physics is actually doing more here than it should and we can restrict it from manipulating the play's Rotation by expanding constraints and then locking all kinds of rotation so feel free to take a look at what you got now and voila the player's movement seems to be working nonetheless an RPG with a fixed camera does still feel somewhat awkward and with the unity making the the camera follow the player is as easy as just changing its position and rotation beforehand and then simply attaching the camera game object as a child game object to your player game object causing all of the player's children to move alongside with it which is also why the shadow Works actually however if you're using a 3 model for the player please make sure to uh rotate not the whole the entire player object and instead only the the the game object the model is attached to to avoid yeah rotating the whole camera with it again you know where to find me and not sure what you think but I think this feels a whole lot better now although although we still missing animations obviously and I'm just realizing my Demo World behaves a bit awkward but not having any colliders to those walls but to the grass for some reason anyway but before doing that as far as I'm concerned most of modern day RPGs also uh let the player uh jump and the way you can add a jumping feature to your player is by adding a few more lines of code to your fixed update method for instance and in case like you don't feel like extracting your different lines of code here um yeah I'd like to ask you to at least add some comments as I'm doing right here so you know what snippet of code is doing what and one thing we do not want to do is to have the player jump infinitely or like continuously so we somewh need to find out whether the user actually wants to jump which is by just checking the input so make sure to use an if statement here and then asking for input do get button down with a jump as a which on desktop refers to space which I think is fairly intuitive and not sure if you realize but using get but justk button here does also work or at Le at least it looks like it because what this is going to do then is it as long as the user is holding on the space key it will continuously asking our player script to jump resulting in an infinite jump however what you want to do is per is per hit of this the space bar um you're asking the script for only one single jump action so we can make sure we only um execute the following code within the very first frame of our input by using get button down so just so you know like a few minutes ago jumping obviously has something to do with our physics or respect or our Richard body component respectively and the easiest way to implement it is by calling Richard body. Ed force and then we need to has in some information like the the direction we want to apply the force to which is obviously to the upside and hands we want to be able to manipulate the player's jump Force at any time we again want to multiply it with our very own variable which I'm going to call jump here and then last we want we need to do is telling Unity what kind of force we want to add which is obviously not a continuous one instead we want to have one impulse and that's why I'd ask you to and type enforce mode. impose as the second parameter or jump variable however we can add right below the speed one which I encourage you to use a FL again for so how does our executed result look like well not too bad except for one thing we're still able to jump infinitely and why is that because we do not restrict the user from just spamming space and thus firing like a 100 um jump prompts at once so with that you can see that we actually took one thing for granted we actually can't which is We we forgot about telling Unity that objects or characters should only be able to jump as long as they're on the ground except obviously you want to add a double jump and the best way to do that is speing what's called array which in which in unity is an invisible object still being able to um interact with your with with its surroundings or a physical layer and thus being able to detect collisions without the the user noticing that would be very annoying and you can add this additional check by adding a new function the is grounded function or you can obviously name it however you want which is going to yeah paste or return a value a billion so yes or no to the point it has been executed from so besides our input we want to add an additional condition which we do by using this and sign twice and then calling our very own as grounded function and inside the thing we want to shoot our array by calling physics. raycast and then the first information we need to pass in should be the starting point in our case the play's position referenc to by transform. position then we need to have information about the race Direction which should be to the downside and then we want to store the result whether an object has been hit or not with a new variable called hit by using out hit or you can also declare it right in there by using out Ras hit. hit and our fourth and final attribute or argument is going to be the race maximum distance which I was using half of the players y scale in the beginning plus something very small however what I figured out worked best was adding a custom height variable here and dividing that by two plus something small enough this new variable we obviously need to add at the very top here again ourselves however if we return true right away here meaning the player is grounded as long as what we typed in above is mad we would still encounter another issue which is the player would still think um he's grounded at any point because we didn't tell Unity the ray shouldn't yeah stop at the player so if we started the player and shoot a r the ray which still hit um a collider which is the player once itself obviously so in order to prevent that we also need to check whether the object hit doesn't have a tech player to it or the tag more accurately as Unity you're able to add a single TCH to your all of your game objects through the inspector as you can see right here so let's fire up the thing again shall we and from what it seems like everything is working the way it should [Music] awesome one more thing I got on my list though and those are the players animations which believe me are going to make everything just feel incredibly more authentic and uh and yeah good or engaging was the word engaging and when it comes to animations I'm happy to tell you that Unity has its very own State machine to it we can use it for to implement our animations very easily which we can access by going to window and the uh top navigation bar I think it's called and then adding animation and the animator the animation tab being the editor for each and every animation itself and the animator being the state machine with one state representing your one animation as you might have thought thus make sure to add a folder for your animations and then feel free to add an animation called player idle whatever you feel most comfortable with as you can see within the animator tab Unity does always um interpret the first or the only animation as um you have the default state it will automatically create a transition to you being the initial one and right here if you're creating a player with a 3D mesh you can hit onto the um red circle and apply your changes um like right away yourself as you like might have know from from from lender and and like modeling or animation software called um automatic key frames however since I personally I'm using a Sprite character here I I'm just able to um select all spres from my respective sprad sheet referring to the idle animation and just dragging them right in here however since those guys are pretty close to each other right now um the animation is going to be very quickly at you so I thought best by changing um the speed within the animator state to um1 here and if I now execute the thing once more here you can see that thing looks like it's working now good so now you're able to add your players work or run function to you by again opening the respective drop down and clicking new animation and just like with the idle one the way I'm able to do it is by just adding all those Sprites here unfortunately if you're if you execute your game once more you'll see that um at least so far Unity doesn't have any reason to assume we want to change any of those um active States so what you need to make sure to do now is adding a transition from your state to walk State and since we don't want our state machine to go that very transition right away we also want to switch the uh the subtab to parameters at a new Bo fion called or working or running to specify um yeah what log logical State player finds um himself in and depending on the working B like whether it's true or false going um the respective transition within the state machine or the animator and that you can do by simply select the transition and adding a new condition which should be um the walking Boolean and then whatever uh Val you wanted to have for moving from idle to walk it should be uh true and vice versa it yeah should be false obviously trying it out now still wouldn't give you the results you're looking for because because now we got that Boolean giving us the required information on one hand but on the other hand we do not we don't even change it at any point so we still need to make sure like from our code to um tell the animator um what logical that we find um ourselves by changing the respective Boolean um within the code so add a new comment here if you feel like it and then the way you should be doing it is by checking whether the player's velocity like using R Body velocity whether the player's um x velocity or Z velocity is not equal to zero meaning the player is moving and on at least either of those sides or at least one of those sides then this entire statement um here turns out to be true cuz we're using an or here like by using those two um lines here and then calling animator do setb your name of your Boolean which was most likely walking or running and then once you said it to true and don't forget to ensure you actually got um access to your animated component by doing it the same way like the rigid body one oh I think with the rigid body one you should also be doing be putting a new in front of it and you should assign the actual animator component to the animator variable by using that component again however by doing it like this you also need to make at least a branch of this condition to set the Boolean for running back to force so there's an even easier way to do that and that is by putting the entire condition right into where um where we got true before as um every condition um still a Boolean statement meaning um yeah it's going to be evaluated as true or false anyway so you got it in the right way and if you were following me closely you might have noticed I didn't told you to um add is grounded um to the same condition which is because it was breaking my neck later on so yeah do yourself a favor and leave that um those third subcondition um with is grounded yeah just drop it if you were to try it out right now you would realize it would still feel Prett awkward um considering your inputs because all of our conditions like or all of our transitions you still have an extra time to it meaning in the worst case it might take well like 1 second or whatever um after you startop pressing um your key until the player is um is stopping to move and the best way to fix it is by just unchecking the has exit time box and all of your um all of your transitions and unfortunately I do not have a a second camera for my hands unlike some girls do so unfortunately I can show you like how much better it feels now but I can but I can tell you it does what still feels a bit off though is our player is always heading um to the right side no matter the direction we go so yeah how about um just flipping the player Sprite whenever we realize our player or our user wants the player to be moving to the left and as it turns out that is as easy as creating access to the Sprite renderer the way I told you earlier and then near your animation code accessing the Sprite render and setting flip X to well we don't know whether to set it to true or false yet but again the most comp the easiest way to do that is by just putting the condition right after that which in our case want to flip um the x-axis in case the player's velocity on the x-axis smaller than zero meaning the player is heading to the left and now although always feels a bit strange to um yet to see your flipped Sprite for the first time um I can tell you your um players are um going to love that just take a look at how much more authentic it feels [Music] now the only thing not feeling authentic right now though is um yeah the players jump as we're still playing the walk animation When jumping so let's make sure that mechanic is covered with an nice little animation to add a new animation to your player and call it like player jump and then simply set your key frames or yeah my Cas as I had to realize that yeah actually actually was just a simple a single Sprite so this time was just draging a single Sprite here well well it's better than nothing I suppose then don't foret get to teach your animator State machine about the transitions you'd expect when um you're thinking yourself into the position of your players or the user so this time add a new trigger which basically acts like a Boolean but only like once so as soon as you fire the trigger um it will stay true until the animation has been or the transition has been going by the state machine and then it will automatic basically return back to um its idle State um which is your Force equivalent to force then add a transition both from the idle state to the jump State and from the walk state to the jump State and make both of those transition fire um as soon as um the trigger is firing by adding it to the conditions list and you can also tell it doesn't require any further um specification of its its value as it's yeah practically one valued nonetheless trying it out now would still lead to nothing as we again need to um yeah fire the trigger manually from our code which we can do by adding the command to line 50 so um yeah within the jump scope it's only being executed if the player indeed um yeah is allowed to perform a jump and this time around again instead of using set boo set po we're using set trigger here with the name of our trigger jumping without adding any further value also you can always reset triggers finally by using I think it's reset trigger in case you changed your mind before um get the frame passed sadly well not for the player but not for but for the player we've now successfully Frozen um our player game object um to um to remain within the uh jumping state for eternity because we didn't add the transition the other way around um so make sure to add a transition from your jumping state to your idle State and also from a jump to walk so we get this like kind of triangle and now our um challenge is to tell whether the player is is grounded as we do not want to uh return to either one of those two states before we're actually grounded um and you can do that either by um yeah scrapping the jumping trigger and making it a a Boolean or by adding yet another Boolean for is grounded in case you want to discriminate between um yeah actually jumping and just um being in the air without jumping like in case you fall um then you should go um with this approach here finally make your jump to idle transition so that it's that it fires when walking is false but is grounded is true and the other one should fire whenever working is true as it's the working one um and it's uh it's grounded as as well and here you can see me yeah fixing the issue we were talking about earlier like an idiot oh and uh make sure to set your grounded bullion here too and um yeah in order to receive the respective value we we we made a function for that earlier so just feel free to call it here and pass the its return value right to the animator and now just take a look at how smoothly were able to to make the player jump playing playing the respective animation just like young K who had just figure out he's able to jump and separate himself from the ground he has been pulled towards without the last over the course of the last years now finally I want to show you something you're probably going to love me for and that is um I want to quickly point out towards a 2d rigging which is a technique um similar to 3D rigging which allows you to add yeah what's called Bones or a skeleton to your Jud spr I mean it's a third party but yeah causing any movement of those bones um during runtime to um yeah to make this your player of sprite follow alongside with it which might be very useful to you in case you plan to have like you very beautiful animations like with H or whatever and you do not feel like yeah creating like 60 Sprites per second then you can yeah simply um add your bones to it and make those move instead to save yourself some time another mechanic some players um within RPGs have is um being able to execute to perform an attack on the overw 2 or like in this in this form causing any battles to be entered with a a I think First Strike it's it's called mostly um if you want to implement that too I would encourage you to just yeah play animation and then like shooting one or multiple Rays like um to the side and it case it it hits an object which is attacked an enemy um yeah initializing the the battle with an with a flag so it's to true then the only thing I can tell you is um whenever you feel like you need help with any of this or you want to have um my personal assistance when it comes to implementing any of those features I um yeah was just pointing out to you um yeah feel free to join my premium Discord and just yeah just ask me and uh hopefully you can find it uh on my patreon within within the description not sure if you know but um YouTube just added an AB testing feature for thumbnails finally so I yeah feel free to tell me in the comments which one of those you received if you want to learn Unity by recreating Minecraft within Unity there's a video on my channel for that too and yeah hopefully I see you there and happy programming [Music]
Info
Channel: Jadon | Unity Tutorials
Views: 1,000
Rating: undefined out of 5
Keywords: Unity, Game Dev, Game Development, Unity Tutorial, unity game, unity 3d, unity game development, csharp, game development, unity tutorial, game dev, gamedev, gaming, rpg, Paper Mario: The Thousand-Year Doort, Paper Mario ttyd, ttyd, rpgs in Unity
Id: os4_ViOpE5E
Channel Id: undefined
Length: 31min 2sec (1862 seconds)
Published: Fri Jul 05 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.