UNITY ScriptableObject Basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
have you ever heard about scriptable objects in unity and have you got no clue how to use them or what they can be useful for well I'm going to be shedding some basic lights onto the scriptable objects in this video normally in unity we always write Mony Behavior scripts and we've got those little Nifty methods that come with it with the start function and the update method that we keep running in it everything like that we can expose some fields at the top of the script and say oh I want this bullet to be this fast or I want my score to start with zero and I want to be able to I don't know jump this high but script objects it's a bit like a data container and unity says themselves that it's a good way to store information so it's not duplicated in memory and I find a little bit difficult to wrap my hand around how to explain that so I create a little script and how I've used it in for example in line War when we do a lot of missiles and shooting and maybe we have some different camera profiles and stuff so I'm going to be trying to show you a little bit how you can use scriptable objects in your game I've created a basic scene here and I've got a few objects and I've got this first one which is a target it's just got a mesh renderer and a cube and it's got a box collider and a script that I created called Target and you can see I've exposed a field called health and it's got 1,000 in health that's pretty good depending on what 1,000 lives no I don't know 1,000 anyway if I double click on target you see that here's the serialized field that I've exposed and it's giving me a default value of a th000 and then it's got this private method called apply damage and it takes an input parameter of damage and this is not even a public method because we can actually call this one within unity's little Nifty mod Behavior world and when this is executed on the target then we put a de buug log entry just to say we're applying amount of damage to this object and then we actually apply the damage by taking the health value and reducing it by the supplied damage parameter here and then we check if health is smaller or equal to zero then we just put in the debug log I've got no Health left so destroy me and then we destroy the game object So eventually this object will be destroyed just to see that you hit it as well I've just said that when it's applying damage just put a random color onto this box as well so we can see that it's actually getting hit I've also got this weapon and it's got a weapon script on and a few things we're going to run through it takes in an input here as well it's called ammo and here's actually where the scriptable object comes and this ammo is a container to describe here's I'm I'm calling it ammo fast light so we're going to have a look at that in a second it's also taken in a projectile prefab which is a normal Unity prefab those are more commonly worked with than scriptable objects and then I've pointed out a transform here where the muzzle is because we want the bullet to be fired from somewhere and if I click on this one I can see that the muzzle here is just at the front of our little fake weirdo barrel and it's pointing very important that it's pointing forward in the Z axis which is the blue axis here if we look at the weapon script here what's it actually doing so I've exposed these three Fields serialized field and it takes the ammo input here and this type is the scriptable object we're going to look at that in a second it has the game object which is the projectile prefab and it's got the transform which is a muzzle and then in the start method and we know that from the mon behaviors video right cuz you watch that one it runs when you start and this object is spawned it runs once and then it uses the monob behavior invoke repeating and it calls this fire method after 1 second and it calls it every 1 second so you'll just keep firing a bullet every 1 second and the fire method down here so it takes this input value which is the prefab it instantiates that or spawns or creates it in the world and it's going to place it at the muzzle transform position and at the muzzled rotation that's why it was important that the blue arrow was pointing for forward out of the muzzle because then we know that projectile is going to be spawned there and it's going to be flying forward into that blue arrow Direction and then we have to do something as well I want to configure this projectile to actually have the specific type of ammo normally when I've done stuff like this in the past I've created a different prefab for every bullet maybe I've got a prefab called plasma bullet and I've got another one called kinetic energy bullet or nail gun bullet or something like that but I've actually really liked working with this new scriptable object type because when you pull ammunition then you want want to be able to spawn maybe a thousand bullets and a whole pool of them and all of them are disabled and then when you fire a bullet it just activates that bullet and it animates it or like transports it somewhere and then when it hits something it deactivates it and returns it into to the pool and for games where you have a lot of bullets or like bullet hell games then this is the approach you want to do because you don't want to instantiate bullets like this all the time takes a lot of processing power and memory allocation it could make your game Run bit junky so I definitely recommend and I'm going to be making a video about the cooling process in the future as well but here's where the beauty of the scriptable object of ammo comes in so let's have a look at what that actually is when you create a scriptable objects it it's sort of like a mon Behavior but it isn't because you have to replace mon Behavior by scriptable object and here I've actually exposed two Fields one is the velocity which is a float and it defaults to 900 uh which is actually not allowed cuz I'm only allowing a range here of of something much less here I'm also let's see we'll default to something else here as well something like this and it also takes a mass so what what's the mass in kilograms of this particular projectile or this ammunition this is something you might not be aware of as well and you could use this in mon behaviors too when you expose fields and you can actually put a range property here and then you can put a tool tip if you want so when you configure these if I click on one of these that has got this one on it we see that we've got a cool range slider here that we can use and also the tool tip is you can actually put this one so when you configure your bullets you can have a whole bunch of cool properties here and this doesn't just go for bullets you could have vehicles or enemies or anything like that just imagine now that this scriptable object is a way for you to configure like a template or an archetype or a blueprint or something of a particular type of something and in this case it's a bullet I've also included here that you can actually use either methods or this is a public property it's called kinetic energy damage and you can call this one from like the money behavior and then it will get what the kinetic energy damages for the configuration of this one and it's using just the normal standard physics method to calculate kinetic energy on impact it takes half the mass and then times it by velocity squared you can bundle configuration so it's a data container but it's also possible to put calculations and do methods and things in here which are really useful for this particular ammo in this case and this is also weird you probably haven't seen this if you haven't worked with scriptable object and create asset menu it actually throws something into the unity editor here if I go up to assets and create you can see that there's a new ammo here that's nothing Unity specific if I click on that it creates something called new ammo and I can call this one fast heavy so it's created a new data object in your game here now and I can configure this one so we called it fast so velocity is going to be high and we called it heavy so mass is going to be high so I've got three different data containers you can also duplicate these These are basically like assets within unity and if you change them they'll change permanently even if you change them during run time it's going to change per permanently which is a bit different from how you do with instantiated prefabs so in this case I've got these three ones and we'll go back into the weapon script what happened here so it instantiated the projectile prefab which is a prefab and then it sets the ammo of that prefab to the ammo that we' have selected for the weapon and if I click on the weapon we can see that this fast light ammo is the one that's uh configured right now for this weapon fast ammo what is this uh projectile then we'll have a look at this prefab as well this is what's actually going to be spawned so what is this it's an object and it's got a mesh renderer and a sphere it's a really small one like this and it's got a trail renderer on it and a trail renderer they're pretty cool as well you can add them and they can add a little Trail so you can actually spot the bullet because if it didn't have a trail it it would just look like a little flicker of a I don't know like a BB gun or something so this is just to make it possible to see it apart from the trail renderer we also have a projectile script and this is a mon behavior that I've attached to it and if we look at what this does it's basically it's I've exposed the ammo type which is the this is the scriptable object and I can set this from externally and that's what we do when we instantiate this projectile we configure what type of animal ammo animals ammo it should be then we have this update method and Amon Behavior this runs every frame of the game so what we're going to do now is we're going to move this bullet or projectile pretty fast in the in the direction in Direction in the direction it fa it's facing and here's the thing you might think that you should use like on collision and rig bodies or something when you fire a bullet to detect like is this bullet now hitting this object but I don't recommend doing that because a bullet usually moves so fast that it might fly through the like a thin wall or even a thick wall it could actually go so fast that it's going to fly right through the wall it's going to update update and then it's going to ignore that there was a wall there and update on the other side that can happen for a fast bullet what I've done in this case in this demo example and this you could definitely use this for your own games as well in the future it's actually throw an a ray cast it's performing a ray cast from the current position of the bullet or the projectile in the direction of its own forward Direction so it looks down that blue arrow if it hits something we have a reference here to a ray cost hit and how fast is it going to fly well we're going to take the ammo velocity so whatever ammo this projectile is configured with it's going to be flying at that speed we have to multiply it by time. Delta time otherwise it's not going to be reliable very important if you run the update method here that you multiply it by time Delta time so we're going to look for as far as this is going to fly until the next upate date we're going to check do I hit anything along the way if I do this if I get a hit here then we're going to put the bullet at the point of the impact so I'm going to move the transforms position of this projectile to the hit point and then we're going to send a message now cuz we know what we hit and we're going to grab a reference to that collider of the hit and then we're going to send a message this is also unity's monob Behavior magic stuff here and we're going to look for a method called apply damage there and how much damage are we going to apply well we can send that and we'll pick it from the ammo scriptable object we pick the kinetic energy damage there and that's actually calculated on the Fly then whatever it was configured there with the mass and the velocity it'll get that damage amount and then I've also said that don't require a receiver cuz if you hit something that doesn't take damage then it would throw an error if it hit something and it didn't have that applied damage method so this is basically just going to be a silent fail if it doesn't have that and it's not going to apply any damage when the projectile hits it we want to hide the actual pallet otherwise it's going to look a bit strange that it's like little BB gun ball in suspended there by the impact point I'm going to get a reference to the mesh renderer of the bullet which is just a sphere and then I'm going to set the mesh render enabled to false and you might think why don't you just destroy the bullet well I could do that I could just do a game object destroy straight away but you can see here I'm actually destroying the game object after one second so why would I do that and not just destroy it instantly well it's because if I were to destroy the bullet object instantly the trailer would not continue to render so when you hit something it would basically just evaporate that whole Trail and it looks a bit weird you want to have that trail just hit the impact and then shrink into the wherever it was hit and then I actually also have to destroy this which is this mon Behavior if I don't destroy or delete this projectile mono Behavior then it's going to keep going back in the update function there and just going to try to do this over and over again and penetrate the the Target and just fly forward so I have to destroy this so we don't end up in this update method anymore basically we're sending an applyed message to apply damage we're hiding the mesh renderer we're queuing the object for deletion and we're destroying this function so it doesn't come back in so the trail renderer can complete itself if we didn't hit anything well then we're just going to translate the projectile in its forward Direction by the ammo scriptable objects configured velocity and we have to multiply it by time. Delta time all right so if we go back now out of the projectile we can see this in action I press play and we can see here it fires a bullet and it hits it and we applying 147 damage and eventually when we've reached past 1,000 damage then no Health left so destroy me and this one keeps just firing because it's it's uh just continuing forever so we had 147 damage there and if we go back to the weapon now we can see fast and light ammo what happens if we do slow and heavy ammo then and then we press play again I've configured new ammo types in for this Weapon It's applying 93 damage oh yeah it's because it's slow and heavy so it applies less damage Dage because it's moving slower so even if it's heavy it's moving slower but let's do the fast and heavy one then so we'll grab this one fast and heavy press play and now it's going to be shooting pretty fast and it just destroys destroyed the whole thing it applied so much damage 6,250 that's a lot of damage so I hope this give you a little insight on how you can use scriptable objects again I just want to say one more time that the beautiful part of this solution is that you can have one projectile prefab and then you can actually pull those up to a, thousand of projectiles ready to fly in any directions but then you can configure them with scriptable object data containers like this to give them different property and these are just a few example you could control for example how long the trail should be what type of a material should be on the trail if it should uh reduce its speed if it should have I don't know uh maybe it should have bullet drop or something like that so you can just extend this scriptable object now and configure it the way you want it so that's it that's a little quick intro a basic introduction to scriptable objects for you I hope you find use of this in your own games and uh don't forget to hit the like button if you learn something here and if you want to learn more about game development don't forget to subscribe to the channel and I also want to give a big shout out to all my patreons you guys are making this possible so you can head over to patreon.com infancia I'll be uploading all of these things to the tutorial to there so you can actually download my Unity projects and things like that play around with it all right until next time have a great one and I'll see you then bye for now
Info
Channel: Imphenzia
Views: 15,420
Rating: undefined out of 5
Keywords:
Id: DWiMI92Lzx0
Channel Id: undefined
Length: 13min 54sec (834 seconds)
Published: Tue Nov 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.