How To Make a Modular Weapon System in Unity!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys it's me a fax here and I have something to show you this tutorial will be about creating a module weapon system that is easily expandable I will be splitting this video into 6 parts with the six part having sub parts you can see all the timestamps on the screen right now all the assets that are used in this video are free to use and download but before we get into anything I have something to tell I'm almost at 200 subscribers and it may be small for you but it's it is a huge milestone for me I am so happy and glad about this and it's only possible because of you guys thank you so much 300 is so much more than that 100 subscriber goal I set for myself I will try to get 500 subscribers by the end of the year but remember to like subscribe and comment something on this video without further adieu let's start coding so in part 1 I will be showing you how to get the project set up with the weapons and assets import so we want to go to the asset store wait it to load when the asset store loaded go to the search bar and type low poly FPS back and as you can see there's the paid 150 dollars oh there's the 3-1 and we are going to use the free one so download it and when you have downloaded click on the import and when you have this import screen uncheck everything because we don't want any everything and go where there's components and drag down until there's meshes as you can see these meshes are their actual models of the weapons so just select guns because we only want guns here and we are not done because we still want the matter for this so scroll down where you can see low-poly FPS pack texture click on that we need that and scroll up where there's materials and click on gun guns materials so just import that so part two we will add weapons to the scene and when we import it the low poly FPS pack there were two different guns or weapons there was the assault rifle and then there was the hand gun pistol thing I actually used this pistol in my grappling or two cleft grappling hook tutorial so remember to check that out but yeah these are really good looking assets and they are free there's only two but I can recommend buying the one hundred and fifty dollar pack because there are so many assets there and it's super super good so let's add the assault rifle first to the scene let's drag it there and yeah it's really small so I would recommend before we actually even change anything create an empty poly assault rifle reset the transform and drag it up and then just track the assault rifle on it and reset and now we have the actual pivot point of the weapon and change the scale to something like to you because this looks much more normal to me and but before we even do anything else we want to Center the actual assault weapons this part here because we must rotate the weapon on this point but now it will rotate like this as you can see but we want to rotate it from here so just drag this to somewhere here check the position no we went over so change it back and now now it is good so now when we rotate you can see it rotates from that one point so now we have two assault rifle in the scene we don't have a weapon class yet but we want to make in this the lair weapon and no this object only don't change the child objects later this will delay of this will be controlled by the weapon script we are gonna make so now create a new folder for briefer and in prefabs at the assault rifle because we want to set change the global settings here like that so yeah we have two assault rifle now I will just rotate it here to look cool like that and now we want to add the pistol so we need to do the same thing let's create a new empty cold pistol just like that reset the transform drag this up and in the low poly FPS pack drag in the handgun to be the child of the pistol and remember let's put this two two two two so it will be bigger and let's enter this part to be on the pivot point and like that not quite a little bit back and maybe a little bit that way a little bit more that way and I think that is good so now when we rotate you can see the come you rotate from that pure pond so that is good now let's create a prefab from that so we can change the settings easily for all and let's just let's just drag it somewhere here like this so that is it part two done we have imported the weapons they are looking snazzy and we can get to actually scripting the game now for part free we want to create two scripts called weapon and we want to also create a weapon manager script with no and we also want to create a weapon manager script with script and wisdom manager so for picking up and throwing stuff we'll start off with editing the weapon class so we'll start off by removing the default boilerplate code then I'll just tidy this script up by removing the unwanted imports and to get us started let's declare some variables so that we will use to make this class so we want this thing we want this public so we want this public float throw false and then we want throw extra force this is for throwing upwards and this is for horizontally then we won the plot public float rotation force and it will be applied when you throw the weapon how fast it will rotate also we want this header and stuff like public in weapon GFX layer this will be used for looking with the weapons through the walls so they won't be clipping but I'll show you how to do that then we want the public game object weapon GFX like that and then we want them public whole either GFX Eliza then we also want to describe it bull hell so this will be true if the weapon is being held and false if the weapon isn't being held then we also want this private rigidbody RB this is gonna be the rigid body of the weapon that is gonna be added and those are the variables we need so to get started let's add a start function and rigid body equals game object the add component rigid body and now on the start every weapon will have a rigid body so they can affect the and they can interact with their environment then we want to set the rigid body mass to 0.1 F because if the boxes are one then how could a weapon be one too and that's all for the start method now we'll start make the pickup and drop the functions so to do that we want on public boo public void pick up like that and then we want any public void draw like that and for the pickup we want also to have a transform weapon holder and actual weapon manager will pass on the weapon holder to our weapon class so we can set our transforms parent to their weapon holder also we want on pickup to destroy rigid body because we want one and we don't want it to interact with the world anymore when you have picked up we want to set transform the global position to zero this will reset the position of the weapon and transform the local rotation to quaternion the identity and got when that identity is like with two three but as Danny said who even understands quaternions so now we have set the actual transform positions and we want the health to be true so we want to weapon to be held and then we want GFX Collider that enable to be false because we want don't want the weapons to actually collide with the world when you guys pick them up you can also not set this to false but then you will hit boxes and it won't be fun also before we write the drop function we want to check for if we are being held then we want to return because you can pick up a weapon that is being held right now and copy this because we are now gonna make the drop function paste it in and if we are not being held we can't be dropped it makes sense so we want our rigidbody so just copy this we want to create a new rigidbody when we are being dropped like that then we want our current remove sort of there we won't have a parent then held will be false because not nothing is being and we are not being held anymore we want to enable the GFX Collider like this equal true so enabled equals two then after we create the rigidbody also want the drop function to have a transform player camera like that and for the player camera we want the forward wha-wha forward to be player camera load forward right so now let's set the rigid body up level city to forward not effectively forward just forward times fro boss also we want to set the rigid body the velocity particles vector team up so it's the opposite direction times throw extra force so this will throw it to go in any direction you are looking in with the same force and this will just throw it up so it will feel like csgo for win we also want to set the rigid body dot angular velocity because that is the velocity on how much the actual object is rotating we want this to be random dot only unit sphere times and rotation force like that and that's it we can go back - assault rifle and we have to force here and gfx Collider so we need to add a Collider to everything so just click on every object and add the mesh Collider on them and there's as you can see there's four object with mesh colliders and they need to have convex on but there's only one GFX Collider here where you can put it so go back to the script change the collider to an array and GFX colliders now create a for loop for each loop and the collection is GFX colliders and the variable name is Collider and just Collider but enabled equals false and it might say that there's a Collider already in the component so let's just rename this to call and set call to enable and also copy this paste it here and coal I've ever been able to go through and that's it so go back to the game set the colliders to be these so just break them in here on the GFX colliders and it will add them to the list now if you play you can see that the weapon has colliders and you can push it like that and it works so it's fully functional so now we want to open the pepper manager so double click on that and open the script and remove the boilerplate and also remove the unnecessary imports we want to add some variables in weapon manager too so we won't let the public flow pickup range this will be the range that you can pick up from public hello paper radius so this will be how how big the actual pickup sphere is I will show you how that works in the game that we created this then we want this public in weapon layer so we can actually raycast it to the weapon and actually see what weapon it is and we also want a public transformed weapon holder and a public transform player like that and we also want to private and private wearables like private do this weapon l and pry with weapons hell wear like that now the variables are done and let's create the actual script so this script will only use the update loop so create an update function like that and create an ear East weapon health and Breton else so let's start with the earth so if a weapon is held and and if input look get key down get key down and we want the cue so changing the actual key here will this will be the drop key so a you can add H so you can drop from H and here but I'll just use cube or if or if the key is down and help web and so if the key is down we can add this function so health weapon drop and let's stop the player camera like that and also health weapon if they snow pickups we are not holding a weapon anymore and this weapon help equals false like that and now in the else function we want to check for else B input don't get key down and this will be e so the key code e will be the pick up a key so I want to pick up the weapons with E and drop them with Q so now let's actually call the picking up of the weapon so we have already checked the e game yeah so create a war get list equals near make us array cast it but not an object but an array and the size of the array should be 256 then create the wall hit number equals physics the capturecast nan allocation like that I will explain all the stuff I will write here later on but just by player camera those position player camera block position plus player camera forward times pick up range so this is the range mm then in a pickup radius then play a camera dot forward and finally give it the hit list like that so and the point one is where the actual capture starts point two is where the capsule ends the radius is how thick the capsule is and the direction is just in the what direction the capsule is going and the hit list is the list and we need to create this by yourself and I think 256 objects is enough for our scene but if you have a bigger scene you should maybe increase this because if there's more objects than 256 in front of you then it might get a little buggy and it might it might not even pick up so now let's create a real waste equal to new list and we want the system collections get a request like that and it's the raycast it raycast hit list like that then make a follow in I equals 0 I equal it number the least amount of objects hit and I plus books not just make this a bar then what it equals hit list I like that then add a get hit transform and got game object the player if it isn't the weapon layer will just return because we don't want to actually break ass the weapon or rakers anything that is in the weapon and then we want if hit the point equals vector dot vector dot 0 then really bad hit I will explain why it goes like this and then else if LC physics no physics the break us the starting position is fire camera top position hit dot point not get info hit the point - player camera position mmm then we want out but not behavior get info and hit the distance was certain corner and after that we'll also want to check that the heat info doctrine from equals mmm the actual heat block transform like that and relist the ad hit so what does this do so it will check the hit top point and if it's zero you can clearly see it a weapon and there's no you can see the center of the weapon so you don't need to actually check if the hit point is behind the wall so if you can clearly see it we just add it to the list because yeah it's clear that there but if we don't see the center of the mass or the pivot point then we'll check with the raycast straight towards the point we hit that if you can actually even see the weapon and if we can see it and the heat so if it doesn't hit the and weapon it doesn't run this but if it's hit the web it hits the weapon we can run this now check for if realist doc count equals zero then we return now we'll need to create a helper function or private get distance and this will be if this will take away just it then return vector P dot distance and that's why your camera position and the other one is tip-top point equals equals zero then if it if it if the hit dot point is zero then you want to compare to hit the transform position but if it's not zero then just simply like that that's the helper function done so after we have the real list we want to solve the list so the number one object is the closed number one in the list is the closest object to our u so to do that we want to make realistic so I want to sort the list we want to add a action get one on variable hit one we're about to and this will be a function that returns a number zero one or minus one so we want the distance one to be or not just one missed one to be get a distance to get one copy that line and we want distance to to be a distance to hit two and now we want to determine now the ABS and distance one distance one miles distance to this less than 0.001 F zero distance one is distance 2 minus 1 1 so what does this do this part of the code checks if the numbers are almost identical if they are identical then they should be close in the list but if they aren't identical and distance 2 is bigger than distance 1 so distance one is closer you want it to be closer to the start so you give it minus 1 but if distance 1 is bigger than distance to you want it to be further away from the start so it's not the closest object to your screen now we want to add is weapon health to help weapon equals real list 0 because that's the first one and the closest one transform top cap component the weapon and we want to help open the pickup and weapon all like that so that's it for this part and that's it for picking up weapons it's done so save the asset and go back to your unity scene so when you open unity create a new and team on the main camera and call it weapon manager add a script called the weapon manager that you just created hmm create an empty weapon folder and in the weapon manager you want to track the weapon hold it there you want to Jack main camera here and pick up range and let's say it could be 2 and radius on 2 5 and also weapon layer so the weapon layer will be 10 like that and that's done and now we need to check for the weapon hold so now if we take a weapon like this assault rifle and make a new copy and Center it or like put it as a child of weapon holder and reset it you can see that in this position the weapon doesn't look right and it shouldn't be there so to fix this we want to take the weapon holder and actually move it so it looks more normal like that and as you can see you can still whether you can see because it's not fully there we need to drag these different values to test what works for you and for me variables or numbers like these work so I'll just drag this weapon here and I think this is a good position for my weapon like this so just remove the assault rifle and you've done but before you play I forgot and I made a mistake so as you can see here you shouldn't return but you should continue because if you return the whole function stops but if you continue it just continues to the nest and next I so save that go back to unity and if you play you can actually pick up the weapon as you can see and if you press Q you can throw it like it should work like a good boy but as you can see the weapon is clipping and that is no good we don't want the weapon to clip it can like clip here that doesn't matter but we don't want it to click when we are holding so to fix this leave play mode when you've exited play mode you need to go back to your script your weapon open your weapon script and as you can see we are not used to weapon GFX lair or weapon GFX but we need to fix this so from the game object make it to an array and with weapon GFX is like that and now we need to copy the loop so just make a copy of it let's name this G of X and not G of X colliders for weapon GF X's and on the GF x set the GF x got lair equals weapon GFX lair on pick up but when you drop it you want to set it to 0 so just copy the followed and set it to 0 and go back to unity so once you are back in unity just go to your assault rifle like here and in the weapon G of X this just drag all of these all of these visuals yeah you need to make some changes so our weapon can stay on top and so here when that is select your main camera and under cooling mask remove a weapon GFX layer and now if we play you can see that our weapon has there's no weapon it's invisible and but if you throw it it it comes back to life but it is actually there but you can see because we remove the layer so how to fix this is to duplicate the camera copy/paste now we want to remove the weapon manager of the new camera rename it to weapon camera remove the audio listener because we don't want the audio listener then select only select deaf only from the clear flex death only and then cooling mask nothing and weapon GFX so didn't this will only show weapon GFX but as you can see this vision the post processing is done two times and we don't want that so how to fix that is go back to your main camera and just remove the layer from here and if you play now you can see that it doesn't work and there's one reason for that because weapon cameras def is minus one so and change it to one and play and as you can see if I pick up the weapon you can see through the walls and there's no problem and also the effects are still here so it doesn't matter okay that's good but maybe and maybe we want to change the fov of the weapon and i will do that so to tell you that we want to go to the weapon camera and change the field of view so let's try 55 and play and if we pick it up you can see the weapon is much smaller and closer for me and this is good so here we can see that it's maybe two hit and the weapon so while we are still in play mode go to your weapon weapon holder and just change some change songs and settings here like that and that that this is completely optional but I usually like that ever we is less than the actual player F are we and 70 now you can also change the fov more so if you want something worth fifty you can do that and I think I like 50 and I just make some final change it on the weapon holder like this something like this and this and this so when you are done with your changes just right click and copy component closed play mode and paste component values and we also want to change the weapon cameras field of view to it because nothing saves in planar so if we play now again you can see that the weapon is correcting there and you can see through walls it's that is so you are done with part three yeah you're done with part three picking up thrown so next will be actually shooting and then will be you I and then some next so see you in the next part so for part four shooting we want to open the weapon script right here yeah and we want to add some public variables so make some space for them and add public in max ammo so this will be the actual capacity of your magazine you can change this to anything then you want to add public in shots per second so how many times per second you can shoot or you could say that's the delay you can shoot then public float reload speed this will be in seconds on how much time it takes to reload public well it falls so this is how much your weapon will actually hit the boxes or any other rigidbody because this will check for widget but it's not Legos public float range so how far this weapon can shoot and that's all for the public variables and we can also make some headers so it's easier to organize throwing shooting and I usually label the miscellaneous as day we also want some private miracles so just create a private bull reloading another one for and that's it and also we want an private int and so that's all for the private variables and now we want to make an update loop so this will be our first update loop and the only update loop we are gonna make anymore so in the update loop we want to check if we are being held but if we aren't being held so it's not held just returned because we want one we don't want to run anything if we are being held and then check for if input but get mouse button zero and we also want to make another public verbal actually public boom tak a boo I will explain this what this does so capital and this means that if you can just press down the button you have to actually click every time you want to show so if it's capable get mouse button down or if it's just you can press down get mouse button and not shooting so there's no cooldown and not read reloading reloading then you can actually shoot and will reduce a mobile - - and then we want to make a private void issue shoot we will run that here and we also want to make co-routine for reloading and shooting cooldowns so driving I am in the rate ienumerator shooting cool down like that and just copy make a new line and just call this real reloading reload still done like that so in shooting cooldown we want to set shooting equals three then we want to wait for new baseball seconds one second divided by shots per second and after we have waited that done shooting in two spots so you can shoot them also for reloading just copy this change this the reload time or reload speed and just put reloading equals true and shooting equals false and in here we want to start recording so if ammo is less than less or equal to zero so you have zero ammo we want to do a real real up to them but if you still have ammo just do a shoot in cooldown like that sorry guys I forgot something in the table here so just cut this and put it in bracket this will come useful in later but I forgot this actually and I already made the other stuff so just just fix this and you'll be good and we also want to check for if an input get key down or key codes are like that and it's not reloading already and ammo is actually less than maximal so we have fused then you can just make this then you can just start the car routine called reload cool but now we want to actually make the shooting so in pickup we also want instead of this we also want the transform player camera and we want to make a private forum player camera and just set the player camera to be like a camera like that so the variable we passed we want to set the private variable as that when we shoot we want to check for here physics the precast and this will check for start-up player camera position player camera dot forward out will get info so this is study team so if we even hit and range and if we don't actually hit anything so this will return true if we hit but if we don't hit will just return and we want to get the rigid body of the object so our B equals hitting fault of transforms of camp component rigid body but there always is in the rigid body like the ground doesn't actually have any physics so if rigid body is Richard by equals no just return because we don't want to add any force and then rigid pallet of velocity we want that we want to add velocity V so play your camera dot forward times kid force and that's it that's shooting and we still need to go to every manager and weapon holder and play a camera and that's for it with shooting so go back to your unity project when the unity project has slowed it caught your assault rifle and I think a good amount for ammo for assault rifle is 25 or 30 but I like 25 I think 20 shots per second is enough and then reload speed I think one point two five seconds is enough and for hit for three point five this is a lot you can always tone it down but this makes it a little bit more wacky and for range I think 20 and we want it to be fully auto so don't check this tab and we also want to save the settings for all assault rifles so apply all like that we are not setting the ammo so when we're done reloading we want to set the amount max ammo and in the start function you want to set them ammo to Maxim so go back and press the play button so now as you can see you can shoot the boxes like that and reloading or anything doesn't seem really you can see it right now because there's no you why we are gonna make you I next but for now you have to just feel it so as you can see I can shoot now but if I press reload I can shoot on delete a free loader so that's shooting it's that easy it's literally that easy and we can even Bowl with this big box like that so exit play mode and you've done that's part four so in part 5 we are going to create the UI so go back to the scene view and create a new UI and canvas so just double click on the canvas and as you can see it has created a second mass but it's not done you want to change from constant pixel size to scale with screen size and for this you want to set them to three eight forty times twenty sixteen so this one will be the resolution of 4k and you also want to set the match to 0.5 this is hard to explain but this will keep the UI crisp and good-looking for all devices and the same size so when you are back in the editor window click on the 2d option and we first want to create the actual crosser so we want to create the crosshair pipe right-clicking UI and image so you can see that the image is centered but it's a little bit big and I've also created this simple circle circle image and drag the circle image to the source image or any other vertical image you can use anything but I'll just use a simple circle I'll just name it to cross them and make it 25 and 25 or maybe 15 tiles times 15 so I think that's good so that will show the center of the screen force so it's easier to aim but we also want to see the ammo so how to add that go back to the scene view create a new UI text text must pro you will see the pop-up that opens just click import text master action essentials and you don't need the examples and extras so just close this now you want to open this anchor presets and press shift and click on this one as you can see so we want to set the position acts and also we don't want to make it a child of cross sir so just put it as a parent as a child of canvas named it hello.tex like that then for the position X minus 200 and position wide 200 and as you can see it's really really tiny but we want to change this to be bottom and right autosize on maximum to 999 and you can make it bigger as you want but I'll just change settings here so I think the height should be 100 and the width should be something like thousand just make the width big because now when we change the height you can see that it will change the size of the actual text so I will set something like 150 and you can check this by 10m all remaining and I think that's a good size so just remove everything there like that save it now you have created all the needed things here so go back to your script and go to the weapon manager right you make public XMS pro tips I'm all text and go to the weapon script and in the pickup function right here at a pessimist Pro text and I'm on text right here we want to create a private variable named drive it Texas Pro text and all text and just set this amount text as the amount of text we are passing here so I'm all text equals ammo text go back to the weapon manager and just set this to also give ammo text go to the weapon class and where you can see actually pick pick up so you want to write memo text the text equals mo+ these dividers plus Maximo then just copy that you want to also go down here and add a more text the x equals nothing so when you drop the weapon it will actually show no texture so scroll up go to the shoot or not and to here update and you want to paste this and same for reload cool down you want to paste the same thing here so now save this and also here you want to set this to say reloading so when you are reloading the ammo text will say reload so save the script and go back to unity and once unity has compiled the changes just clear the chat but before we press the play button go to the main camera weapon manager and click on that and we want to just set the ammo text to be an alt text or you can just drag it from here so now when you press the play button there's no text on the bottom right corner but when you pick it up you can see it's 25 and when you shoot it goes down and when you're reloading it's just reloading and if you reload mid mid ammo you can still see that it says it correctly and that's how he is you is easy isn't this easy like girls like you can make a cool shooting game super easy like that so that's what five done and now let's get on with the extras that will make your weapon field and weapon system feel extra need an extra good so to add reload animations go to your weapon script again and create and private by float so I can just copy this line and name it to bro Haitian fine like that then we want to go to the reload countdown and we want to set the rotation time to zero so it starts at zero go to update and here if we are reloading then make these brackets then we want to increment spin time or rotation time here rotation time by time.deltatime like that and then we want a spin Delta but to be this just copy this cosine thingy like that and just set not time but actually rotation part time divided by not animation time but real speed like that and then we just want to transform the local rotation equals quaternion the euler new vector3 and spin delta x 360 0 0 and that's it that's reload animation 68 studies and save your script and go back to unity so when unity has compiled your script you don't have to actually change any settings just press the play button pick up your weapon shoot and reload as you can see that's reload animations it's super simple and super fun and let's just Paul once again because it's super fun and there's actually 10 boxes by accident I just placed in random which and I got ten boxes so that's a strike yay I got all of them down go away box nobody likes you yeah roller yeah roller the way so that's reload animations done now we want to add shooting kick back to the weapon so when you shoot the weapon actually moves backwards so now exit play mode and to add that we want to go back to the weapon class we want to go up and we want to add two public Bob rebels public float kick back force and public float kick back and that's all the variables you need and then down go down to your shoot function and then write this transform dot local position minus equal new vector3 0 0 but we want to effect Z with kick back force like that and that's applying the force but we also need to reset the force so we actually want to change this whole function so just cut it right if time is less than animation time then we can run this but if not we want to set the transform but local rotation the identity like that and then we want to add scoping and then we want to add the transform the block of position equals vector V dot load transform the local position and then we want to add vector 3.00 because we want to loop it to zero and we also want to add smooth so kickbacks move times I and I'll actually just rename this to reset smooth because we are going to use this for scoping and if it's kickbacks move it won't make sense for scoping so just save the function and go back to unity once the unity has compiled your changes go to your assault rifle set your kickback force to something like 0 1 0 75 and reads us move to 11.75 I have tested these numbers and they feel good so when you press play and pick up the weapon you can see that you can just actually kick back when you shoot so let's just to shoot this like that and that's kick back it's as I say it's that easy I wish I would have known it it would be this easy when I started I created some really bad weapon buttons some weapon systems but yeah that's why I'm making this tutorial to enlighten you guys so that's it that's for kickback and now we want to add something really cool and that would be sway and after that scoping so sway is when you rotate and the weapon follows you and that will make a most a lot more sense when we actually create it so we don't want to create the sway in the weapon class we want it to create we want to create it in weapon manager class so go up and we want to add some public variables so we want to add public public float sway side public float sway to me so I'll move how much it will smooth it and we also want to add public public transform and sway holder so we will create a sway holder for the weapons like that so sway holder and that's that's why size and this so go down here and into the if weapon dot is held we want to create some space for a function so we want to create this mouse Delta equals minus mu vector to because we want the sway to actually follow our mouse not to lead it so minus vector to input but get axis and this will be Mouse X and input.getaxis Mouse money Y and that's the Delta we need and now for spray holder but local position equals vector free doubler sway holder not local position is the start point vector zero is the end point and space smooth x times delta time because delta time will always make it to run almost exactly on every frame rate but swipe holder and my local position and then we also want to add sway all the local position plus people vector free like this but we want to set mouse delta here times sway size like that and that's way so save the script and go back to your editor and once unit has compiled your script once again go to your weapon manager and we want to set these variables so some good settings for these are for sway sides to be 0.004 and thus weights move to pay 25 and I feel like that is really good and it feels natural so but we also need to add this way holder so to do that we want to create a new empty sway holder we want to drag the weapon holder to be the child of the sway holder and just track the sway holder this way old and like that yeah the sway is done so as you can see the weapon will try to follow where you look so if you look up the weapon will also try to look up and look left right and this will be better when we actually add scope in next but for now right now it feels more natural and the weapon actually moves and follows so as you can see this weapon system is coming alive and actually feeling and this it's not only static you can actually feel that changes and shooting and stuff and that's the power of simple math you can make everything look more natural and moving by making some really easy functions just that just learn or add add positions so now exit play mode and now we want to add a scope maybe the best thing I could or you could add to a weapon system is scoping so to do that you need to actually edit both classes so let's just go to the weapon first and scroll up to shooting and that public vector free scope position and in here you want to go to update and else and just to stopping and we want to also create a private ball smoking like that and scoping equals input input the get mouse button one because we will we want to scope with the right button and the player is not reloading and now as we want to change the vector freeway from vectors is zero until if we are scoping then we want to learn to scroll position or scope possible so position and if we are not scoping just back to the vector field at work or with the 3.0 and then go down to your pickup function and right here you want to add scoping little spots so when you pick up you don't want the scope and then we also need to expose this public bull scoping and make this arrows and just right scoping and what this will do is this is only a getter so you can set the weapon scoping but you can only get it so now save the weapon class and go to weapon manager so in the weapon manager we want to add some public variables so we want to add public flow default fov so what the default field we love you public float scope field of view and we also want to add public both ever we and that's it we also need that's it for some these variables we also need public camera player camera because we already have a player camera just play a cam for short so that's it we don't need any other variables and down here where does the update we don't even want to put this anywhere else but we want to put the cam camera player camera and if you also want to change the play the actual camera fov of the weapon camera too so we want yeah and if you want to do that we want to make it an array and player cams and now we need to loop over it so player camera dot for each so Ryder will already make the like voyage for me but just copy this for each for me and add camera do you not play a camera but play of cams it needs to be camera in player cams so here camera out of yield of U equals math s doubler and camera don't kill the view is weapon held and held weapon scoping so if we have a weapon in our hands and it is scoping then we want to work to scoped for fo B but if we don't are scoping we want to be the default before we and now we also need to add carefully smooth times time dot Delta time and that's coping done there's one thing we want to do because as you remember we have our crosshair but when we scope we use our iron sights so we just want to add another public image and that image will be the crosshair image and before this update loop just but crosshair image and dot gitaji in-game object set active and just copy this and change it that if weapon isn't held or the weapon isn't scoping then set to true so that's it that's all you need to do for what scoping and save your script and go back to unity when unity has compiled your scripts you need to go to the assault rifle and there's the scoped position we won't touch that yet but we actually need to go to weapon manager and see the default F or we scoped f OB & fo we smooth so good settings for this that default fov is 70 scope fo e is for the actually and this move is 11 because 11 is a good alert and smooth number for quick quick smooth and cross our image you need to set this to crossfire or just drag it from canvas right there so now you have your weapon manager done like that and well you need to still drag your cameras so weapon camera main camera right that like that and then weapon manager is done actually but it's not done yet because scope position isn't set so just copy assault rifle to your weapon holder make it a child of it reset the position and rotation so you can see that it's there but we want to actually put the scoped position so you for for the effect I would say just change the fov D for this so we can see how it will look like and on the assault rifle you want to set the exposition to minus this amount so it's centered then we want to change the Y amount to something like 1.6 seven and we want to actually set this to something else like 1.8 no 1.9 is here so one point eight eight five and now it's almost under one point five eight eight and then we also want to change the Z value like this I would say this is too much but this is okay and we can also always test it but now you want to set the position as scope position x y and z and just reset this overrides apply remove that assault rifle and as you can see the numbers are applied here too and just reset your weapon managers weapon camera and main cameras fov 270 because we don't need that right now and that's it all that I can see is that is done so when you play you pick up a weapon you can scope it like this and you can see that the weapon follows you or the scope follows you and and also when you shoot you can see that it correctly zooms in and kicks kicked us and if we broke when we are scoping it doesn't matter it just deactivates the scope because well yeah it should do so right now you can see it is shooting and scoping and that's all it's that is as I always say it studies but if you don't want to actually change the fov of the weapon just go to the water manager and remove the pattern camera phone yes so just set the size to one and drag in the main camera right yeah and now when you play you can see that the FOV is super small and the weapon is super small too and it doesn't look so I would say you should change both of their focus and you can always test some variables so like scope fo b25 so it's this much scope like that and it's a different like this is a really scoped one and use you can also always throw the weapon it has one mo you throw it it has one a more still and if you shoot it it will reload like that and we can also add multiple weapons so go back to the scene view and let's right-click on double click on assault rifle and let's just BAM it around because it really doesn't matter where it is because these are physics objects so they will interact with the world correctly let's just add some assault rifles here let's rotate this one like this and just plate and as you can see the assault rifles drop like that and you can pick that up this has 25 ammo will shoot it at 16 picked ISA it has 25 because it's modular and it doesn't matter how many weapons are in the scene it will work and it won't even lag that's why I like the system so much it's really easily expandable it looks really good for the effort you actually used and it's not even that hard to make it's really easy and that's why I like it a lot so yeah so now you've done there's nothing more to make it's scoping and all but that's a great exercise let's try to set up the pistol as a weapon too so I will show you how to create a new weapon from scratch so let's copy the weapon script from assault rifle pistol paste is new we also want to add mesh colliders for all these mesh Collider and we want to make it convex so now everything has a mesh Collider and that's cool like that and also there's a child of Maggie and we also want to set that as mesh Collider and comics like that let's go to a pistol and just put the GFX colliders as these that we just created and also the handgun itself and weapon GFX is just do the same like that like that and the weapon GFX layer is still the same it it doesn't change and let's set the resets move to the same and keep back force maybe more because this is a topical weapon we only wanted to shoot when we click max ammo let's set this to 10 shots per second maybe 4 5 let's at 5 animation time pickup that's a good number I'll just keep the fro force maybe for and throw extra force maybe 10 because this is a lighter weapon then we want to change the real of speed to may be 0.95 so it's faster to reload we want the hit force to be less because it's a pistol the range could be the same and now for the scope position let's just save prefab settings and copy the pistol put it on the weapon holder and just reset it as you can see it looks good here but we also want to set up the scoped position so just set the cameras to was it 40 yeah I think we set it to 40 in the weapon manager yes the Scott F obvious body let's just put minus 0.49 that's not enough - 5 9 and that Center and then let's drag it upwards and that's really good that's just under critical and let's just ragged in something like that and that's it for the pistol just we already saved overrides well we didn't copy these values we forgot so just copy these values yeah overrides apply all delete the pistol change some settings here to be 70 like that so it's normal let's copy this handgun multiple times because we can there's nothing stopping us like that and like that and let's set one on top of before for some bling-bling and press play and when you go you can pick it up oh yeah sorry I forgot to add the lair of the pistol to weapon so don't change the cylinder children only this one like that and overrides the pistol and now every pistol has two weapon Vallejo so now when we play and pick it up you can see that you can pick it up you can shoot like that and you can also shoot with a scope but this is more for tapping weapon so yeah so that's it you are done it took some time but wasn't it is I hope this video shows you that it isn't too hard to add complex or big things if you split them into segments or parts but if you enjoyed this tutorial you will enjoy other videos from me and remember to subscribe to get notified when I post new game dev content but that's it for this time I hope you liked the video and see you next time [Music] [Music]
Info
Channel: Affax
Views: 11,017
Rating: undefined out of 5
Keywords: affax, devlog, game, coding, code, unity, learn, games, c#, programming, development, develop, halo 4, havocado, gaming, affax devlog, affax gaming, discord, website, devblog, indie, pc, game design, indie game, dani, jonas tyroller, indie gamedev, online games, game developer, game dev, unity 3d, game jam, blackthornprod, gameplay, stick fight the game, multiplayer, unity3d, brackeys, gamedev, gmtk, unity game dev, indie game devlog, grappling gun, grappling, unity tutorial, tutorial, how to, 3d, affax.dev
Id: QUgQe1K9fH0
Channel Id: undefined
Length: 75min 50sec (4550 seconds)
Published: Mon Mar 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.