How Unity Scriptable Objects made this Weapon System Great

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
scriptable objects can be a very very nice way of handling your code it makes your handling of game logic specifics much more scalable i'll be showing you how to build a weapon system using scriptable objects so let's jump into unity and let's get started so this is what we will look like once you finish the tutorial let me go ahead and show you what is the final product of this course if we go ahead here and rotate our weapon with the camera and then just leave the mouse pressed we can shoot at an automatic pace then i'm going to show you how to do this with scriptable objects and switch a weapon and create a different type of weapon let's go ahead and disable this one and if you see here if i maintain my mouse pressed i can only shoot once this is because scriptable object is handling the properties of a different weapon which is single fire and automatic mode so if i just click and click and then i can shoot correctly alright so let me go ahead and show you how it's done using scriptable objects there's a basic configuration that you must do before we jump into scriptable objects it's just a basic player capsule capsule object with the camera as a child optic uh as a child object and just a weapon as the child of the camera that way when you rotate the weapon will rotate with the camera i'm not gonna dive in too much into details of how to make the camera rotate using using the new input system but you can go ahead and click on the link on the description to learn more about how to rotate the camera for a first person shooter so let's go ahead and dive into the system what we have here is a basic setup of a player of a first person shooter we have a caption object called player as a child we have a camera and as a child of the camera we have a weapons empty object the weapons is a container that will hold the rifle one and rifle o2 the two types of rifles in the game i have created different colors for these two rifles so you can spot their difference the first one is green and the second one is red just for you to know which one is automatic and which one is single shot all right so go ahead and visit the unity asset store if you want assets to go ahead and replicate the systems there's a lot of weapon free assets that are free so you can download it i'll i'll make sure to post a link in the description so you can easily go ahead and download it all right so after you have this setup of a player the first thing that we're going to do is that we're going to learn how to create our uh script unscriptable object so let me show you the code of what an of what an scriptable object is and how it is created jumping into a visual code here we can see that a scriptable object is just a basic c sharp class that extends from scriptable object class the other thing that is that extends on it's an interface called iso release serialization callback receiver this is if you want to serialize some data using these three methods sorry these two methods really because this is the initialization method one before serialize and after this year release read more about it here if you want to know what to do on before serialize or after deserialize we're not gonna do that in this course because that is not part of the um of the items that we need to cover all right so go ahead and create this class here and extend from scriptable object all right after you have created this class you can tag the class with this header this header creates an asset menu file so that you can create scriptable objects easier when you try to find them how to create them let me show you what this does in unity so in unity if we want to create an encryptable object we go to assets create and now we can see our weapon so for scriptable object to be able to create that item one of the most powerful things about scriptable objects is that unity handles performance very well what it does is that instead of creating more instances of that blueprint if you want to call it like that it just holds a reference to one single copy so all the other scriptable objects are just copied with different values so that way the performance in unity is much much better than just creating different c sharp classes as a base that someone can inherit from going back into unity sorry going back into visual code we can now create properties that define descriptable object like the weapon name is a string the shot type which is an enum and a fire rate which is how fast the weapon can shoot this enum is very basic if you want to see how i created it is this basic them saying is that is the weapon auto single or burst shot type after this we have a serialized field which is a field that you can set in the editor for the bullet or the projectile that this weapon will shoot that way you can have different types of ammo for different types of weapons now that you have a better understanding of what scriptable objects can do and how are they very useful for us let's go ahead and implement those scriptable objects and actually use them in our weapon class system going into visual code now create a class or an abstract class i'll explain to that more in a second but create an abstract class that is called base weapon i just named it base weapons tutorial just for the sake of the tutorial because i already had a base weapon class it's a basic c sharp class or unity class that extends from mana behavior some properties here that are that are important for this weapon system to work we are going to need a reference to the muscle which means that muscle is the part of the weapon where the bullet prefab is going to shoot from so basically we have a muscle in this uh rifle all the way on the bottom and it's this one and that's the forward direction after you created a muscle or reference it in the script create a float called next time to fire this will be useful to know how fast the weapon can shoot then reference a reference a serializable field called weapon scriptable object this is the scriptable object that we just created in the previous section which is this one and lastly just create a property called actions for us to know when the player is actually shooting or not this is not important in this course but if i can just explain it very fast is the is the is the script that handles the unity new input system events on the start method we want to reference that actions class from the root of the player we're going to get a component then this important method which is called check fire rate this is going to check how fast the bullet can shoot and it's very basic you just check if our current time is more or equal or is greater or equal than the next time configured to file to fire the next time to fire it is an addition of the current time plus one over the fire rate of the weapons scriptable object this is important as if we set the weapon scriptable object fire rate to be higher the weapon is going to be actually shooting faster because this addition is going to be more and if we set it to be a lower value then we have a slower shooting weapon so if this condition is met we can shoot if this condition is not met we need to wait for this condition to be true that's how we check the fire rate in the optic function the only thing that we're doing is we were saying if we are actually pressing the mouse button or clicking the mouse then go ahead and check the weapon scriptable object shot type if the shot type is automatic then check the fire rate if the shot type is not automatic and it's single mode don't check the fire rate and just instantiate the bullet and then set the is shooting to false so we can prevent shooting again just by holding the mouse and the last method we have in this class is instance bullet what this does is that it receives an origin from the instantiation position and rotation and it uses the weaponscriptable object's bullet prefab in order to instantiate the correct ammo projectile for that weapon and the last thing is an abstract void method called shoot more on that in a second okay now that you've seen this uh basic or base weapon class it's basically just a class that checks the fire rate of the weapon instantiates a bullet and it uses all the properties that we configured in the weapons scriptable object that way we just need that one class referencing to the weapons scriptable object and now we can have multiple weapons scriptable objects for our rifle one two three sniper shotgun so on and so forth so that's the power of scriptable objects the last thing that we need to do is create a class called rifle01 or you can create it just weapon 01 or however you want and just extend that from base weapon tutorial which is the base class that we should that we just showed in the previous lesson override its shooting method more on that to come i will explain this and on the start method called the base start and under update called the base update why are we overriding the avoid shoot method here this is very important but in the next in the next tutorial about this i'll be explaining that this override of the shoot method is to do some custom logic before shooting you will learn that when as we extend weapons and we have for example a shotgun is a good example we need to adjust the number of muscles that the shotgun will have before shooting because we need to spread the bullets we can do other custom logic in this part of the override function so that way we're not calling the function directly but each weapon this can be rifle one but it can be shotgun or sniper can have a different functionality that way this system is very powerful because each weapon has its own script that overrides the shoot functionality but it doesn't repeat the logic of shooting so let's go back to unity and let me show you how we integrate these scripts with the weapon objects lastly go ahead and attach the rifle 01 script to the weapon and the other one as well now you will see two properties here muscle which is the weapon muscle that i showed you before and weapon scriptable object it's the ones we created so the first one is this one which is the rightful of one that has its own properties and it's automatic and the second one is the same one but it's single shot and has the different type of ammo so this so the rifle one go ahead and drag the rifle one to the scriptable object uh serializable field and on the other rifle that we cannot see but i'm going to go ahead and turn it on right now which is the red one go ahead and drag the rifle o2 scriptable object that we just created this way those two weapons will have different properties and they will behave diff they will behave differently so let's go ahead and disable this enable the first one and let's go ahead and see how this whole system works and that's it that's all you need for a weapon system using scriptable objects this is a very powerful system that we can extend on in the upcoming videos so you can see here i'm rotating and if i leave my mouse clicked we have an automatic rifle if we simulate the switching weapons here and i switch to my secondary weapon if i press the click and i leave it pressed this should not keep shooting on automatic mode it's just one but if we shoot by pressing each click then we can shoot again the powerful thing also about scriptable object is that if i can change this on the go and set this to automatic this if i just press it it'll behave the same way as the other although i need to set the fire rate to be something like 15. and let's go here and there you go and that's it you've successfully created a weapon system using scriptable objects you saw that it's very powerful so in the upcoming videos i want to show you and extend on this weapon system we will probably creating a sniper and a shotgun weapon so that you can stand on the undescriptable objects weapon system i hope you learned a lot leave a like comment and subscribe and i'll see you guys on the next video bye
Info
Channel: Animars Dev
Views: 7,384
Rating: undefined out of 5
Keywords: game development, game dev, c#, unity tutorial, unity3d, unity 3d, coding, devlog, programming, unity, unity scriptable objects, unity tutorial for beginners, scriptable object unity, c# tutorial, c# tutorial for beginners, c# game, c# game development, c# game development tutorial, c# unity, c# scripting, unity weapon system, unity weapon, unity guns, indie game dev, unity devlog, devlog fps, fps devlog
Id: cPUvYK3965M
Channel Id: undefined
Length: 14min 18sec (858 seconds)
Published: Wed Feb 23 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.