DRAG & SHOOT in Unity | 2D Game Dev Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

'p' sounds hurt my ears. too close to the mic? pop shield?

👍︎︎ 2 👤︎︎ u/gwinnell 📅︎︎ Nov 05 2019 🗫︎ replies

This post appears to be a direct link to a video.

As a reminder, please note that posting footage of a game in a standalone thread to request feedback or show off your work is against the rules of /r/gamedev. That content would be more appropriate as a comment in the next Screenshot Saturday (or a more fitting weekly thread), where you'll have the opportunity to share 2-way feedback with others.

/r/gamedev puts an emphasis on knowledge sharing. If you want to make a standalone post about your game, make sure it's informative and geared specifically towards other developers.

Please check out the following resources for more information:

Weekly Threads 101: Making Good Use of /r/gamedev

Posting about your projects on /r/gamedev (Guide)

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

👍︎︎ 1 👤︎︎ u/AutoModerator 📅︎︎ Nov 05 2019 🗫︎ replies
Captions
what is that fellow game developers my name's Tyler Potts and welcome to muddy wolf this is a different video to my usual deathlok's I want to take some time out instead of showing you what I'm making to teach you how to make the game we're currently making in my Deathlok so if you want to see this game the behind scenes of death log then go check those out on the channel but in this video we would be making this power bar sort of shoot your character drag and shoot to get some sort of velocity as you can see we just fire it into the portal and bam there we go so that's we're gonna create in this video so sit back grab your snacks and let's go okay guys let's get into it let's I've opened up unity hub here and I'm gonna create a new project I'm gonna select 2d and I'm gonna name this drag and shoot and so I'm gonna click create and it's gonna load up a Unity instance or unity project and we'll way interestin okay guys so the project's open up and the first thing I'm going to do is shrink this because it's very big I'm an almost could bring this down though but know what first things we're going to do is we're gonna go and I'm gonna drag in some assets basically some very basic shapes into my assets no come back here back in drag and drop into the assets folder there we go so let me just show you what these shapes are quickly they are basic shapes of square circle triangle and hexagon won't you go be using the square and probably the circle in this video so you can create these very easily I don't know if I should put them in in the description so you can download them because they are very easy to make but it's up to you anyway I'm gonna make this two five six and I'm gonna set this to multiple so we have multiple sprites because we do and I'm gonna go to a sprite editor i'm gonna click apply and this opened up on the wrong screen again twice twice let's make this a little bit bigger and can i zoom in there we go I'm going to click slice and we'll go select automatic center and click slice now if you click on the screen you'll see there is fair for different slices which match the different shapes let's click apply and once our players close that and now let's open up our shapes and you can see we have different shapes I'm gonna drag in a square I'm gonna make it set on the 0 and minus 4 and I'm gonna make the X equal to 99 18 yep and the y equal to 2 I'm then gonna set this to my little -5 and that's perfect so that's just going to be our ground I'm gonna add a box Collider to this 2d and what a score disallowed our object cloud fit so our object doesn't just fall straight down into no way so let's create a circle now which is going to be our object let's drag it into the scene anywhere will do let's I mean let me just reset that zero there we go and let's add a circle Collider 2d on this shape because it's a circle we also need a rigidbody 2d to add some forces to it and to be able to apply force this so adding that and this will apply graph C so if we click play our circle is going to drop straight down onto the platform that is the rigidbody coercing the gravity there to kick in let's come out so we have our rigidbody 2d check a circle Collider check and a ground where we can still check that's great we now need to create a new script and when we call this the drag n shoot script cuz obviously this is you're gonna drag and you're gonna shoot that's what's gonna happen so I'm gonna well I'm gonna double click this to my computer first oh yeah double click this to open it up in facial studio automatically officials to your code but this is just visual studio so let's remove the starting items in there and we're going to create a few different elements we're gonna save public float power is equal to ten F so this is our first variable and it's gonna be the power off the the force we add to our circle and we pull back and let go we're going to multiply our we're gonna multiply it by power so we have that we also need a reference to our rigidbody 2d because there needs to be 2d and when we call this our beat sample rigid body we need the rigidbody 2d and so we can apply forces to our ball so let's say that I'm gonna go back to you quickly and I'm just gonna rename the shape one two bull just so we know save and you're gonna see down here we've got ten and we can apply the rigidbody 2d perfect so we go back to our script now and we can now go in and add a couple of variables we also need a Ferriero book called we need a vector two and we're gonna call this min power will then go need enough effector to and when we call this max power so this is going to clamp our we're going to use these values to say this is the max power we control back Adamle the min power we control back on our x and y values so obviously if we depend on different screen sizes if we dragged all the way back from the top right to the bottom left that's going to give us the maximum power we could possibly do but if we limit this by using a min power and a max power that means we can no even if we drag from the top right to the bottom left it's only gonna max out a certain power so you can't just win by having a bigger screen size so once I stun we're going to then create a reference to our camera and call it camp because we're going to need to we're going to need to get our we need to convert the dragged dick mouth position from a screen position to a world position but we'll do that inside the script so we've got a camera reference that I think that we need a couple more we need a fector - called force which is basically WD force we apply by calculating our two points yes so we're going to use default we're going to apply the force I'm going to more power by the power to give us the force we need to add on to our character we're then going to get a vector that's not affected Asif the Contra we need effect of free which is called our start point we then need a enough effect a free called our end point and needs other two points this the start point from where we first click and the end point is where we drag to and let go so that is how we get those two points we now need a start method so I'm going to do start and I'm just going to set the camera equal to Campbell Cameron main and this is just gonna grab any the first camera object it finds which is attached to the main camera tag or is tagged with the main camera so back to the script and that's what we need in our start so start is called as soon as the dragon shoe is ready and then we're gonna make an update function which is called every frame so each frame per second this will be called now we need to set up a few things we need to say we need to get some inputs we would say if input dot get mouse button down see arrow which stands for the left mouse burn we're just gonna debug log right mouse button just so we can test it's working so let's go into our scene here let's click play and if you look at the bottom left corner and I click your say it said we press the right my man if our you left click you'll see nothing actually happens and we only have the one log on our screen because it's only called once and not twice which is perfect so that's how the script is working but we want to get the position it's when we first click we want to set the start point equal to a new vector3 or well firstly let's just say kam or input mouse position now let's just debug dot log our start point you can see what's happening here so let's go back in here let's kick click play and if I click here you'll see it says were 647 on the X and 360 on the Y now you probably thinking they are huge numbers when you drag and pull and add a force is gonna apply a huge amount force and you are correct and that's because these are pixels these are in the pixels off the screen so the further I go along here if I click in the bottom right here it should apply near enough to 1920 someone in 1920 screen so that is not what we want I'm gonna impose or and play that and basically to fix that we want to say well I want to keep the it's that point but to fix that we want to say can dot screen to world point and we're gonna pass through this now that's gonna transform it into the world point if we hit safe and we go back in and we play again this time the number should be considerably smaller so this is zero so right here is about zero actually say it's zero see report so if we go to the top writers should be only about eight year eight and four and if we go backwards you'll see it's - and that's because this is the world point and this is to dead center section so now we're getting a more accurate in world points but one issue are going to run into is this start point has well when we click it's gonna have a minus 10 on the said axis which we don't want so week there's enough weight fixed weeks a start point dot C is equal to 15 and that now set I believe that should set this start point to 15 so if we say debug dot log and we could say start point we should now see that our so as you can see down there it was minus 10 in the bottom left corner but now if we click it's now 15 which is perfect because basically we don't want it to be equal to minus 10 because it's going to hide behind elements in the scene when we start to add a line so you know in my tutorial or at the beginning where I clicked and I dragged a nil as a line that would be disappeared if it was minus 10 so that is how light is working so we've got the mouse down which is perfect we now need to get when we'd let the mouse up so if we take input dot get mouse button up all right can we do 0 this is now gonna say when we when our mouse button we pull up we want to set the end point equal to again the same camp so you actually just want this and right here there we go but we want to set start point to be also end points we also want it to be 15 and not minus 10 there we go so that is now correct and we can now set our force our force is going to be equal to a new vector 2 and we're basically gonna say the drop list I'm saying math F dot clamp so we're basically clamp our values between two points which we did earlier and I'm going to say start point dot X minus end point dot X and that it's gonna clamp our points between each other and then we're gonna give the min power so we set a variable called min bogus at X we're then gonna all say max power dot X Odessa r2 so that's one that's our one math X that's just for the x value now go say math F dot clamp again because we need say our y-values and in here we're gonna say the well basically do the exact same thing so I'm gonna copy this and then we're paste this into clamp but we're going to change all this to Y so the Y values of all our things which is perfect we're also going to do something here which is going to basically fix some issues so if we change to your for graphic size for a camera we want to basically divide by that actually we don't cuz we're not going to be changed in our camera so we won't go into that now we've got our force we basically just want to say RB dot add force that's four that's not force odd force and we want to say our force times the power we set and then we're gonna say force mode - D - impulse we one that had an instant velocity - our rigid body instead of just a employ force so now we've got that we can go back and we need to set up a few variables so let's click on the bull fairy wall down here scroll down and you can see we've got this that we got set to 10 the min power is gonna be 0 or actually it's going to be negative 8 that will allow us to go backwards and a negative 8 and the max power is also going to be 8 so if we save and we click play we should now be able to drag and let go oh and it's gone it's gone it's gone far and wide that balls gone maybe maybe we should lower this or maybe does the power should go down to probably about 5 maybe - I don't think it needs to be any bigger than that you can adjust the file used to matter we want so if we drag and we launch there you go you can see and now is it so if we drag straight down we'll go straight up Oh God actually could see the ball flows around and let's bring it back down there we go I don't know if this F gonna stop there we go so our we can stop the game anyway cool so that's now applying the force so that is the basic concept for drag and shoot now we can set the max power to be even low so if we said the max power should just be free and then - free and then times it by 2 and we hit play and we drag and shoot you'll see the power is a lot less and if I drag really far that is literally the max power we have so we can adjust those to be how we want the the rigid body to be so now I want to show you how to when you click Add you drag to apply a line between the two points so to do that we need to create a on our board you want to add a line renderer so we're gonna say line renderer we're going to apply this on and it's gonna be here we're going to set the so if we set the positions here you'll see there's enough when we set to Ted and you can see this is pink line what goes through and it's very thick we don't want it to be that thick I'm actually gonna sell it to be 0.5 but then I'm also gonna set a second one so I'm going to double click in here to create a new point and you can see I create this like divot so I'm going to alright I'm actually flip these around so that makes this one a lot smaller so about 0.25 which is here and I'm gonna set this one to be equal to 5 so the bigger one is coming down right because there's a lot smaller about there okay so it will look like when we drag and we pull now I'm actually gonna apply a material to this which is on my upper screen again and this is just gonna be the sprite default well actually maybe the line defy don't notice any difference there is no difference so that means we can apply colors to this so I'm gonna open up the color gradient editor and I'm gonna click here and I'm gonna set the Alpha to zero because I want it to go to be invisible now it's looking a bit weird there but that's because we'll go the wrong way if we would set this to zero and this one's Ted it'll be the right way Oh or will it so we actually want to flip these round so it's like that that makes more sense it so now they're going the right way I'm also going to add a cap third ca5 to make it a rounded edge or actually one makes kind of a trying or not fifty-one one makes this triangle effect although I would reset this so let's bring this back down no guess we've got like kind of a point which I kind of like and we'll also need this used world space biank we want it to be in the world space and not attached to the ball if we turn this off and our points was let's say it's FC so we said five or we move the ball you'll see it moves with it which is fine in some cases but we want it to be using the world space so now when we move the ball it doesn't actually affect a line because I could get confusing I'm gonna set these positions size to zero which is basically gonna remove the line for us so it's not always appearing and now I'm gonna create a script called line trajectory to call it trajectory line maybe tragic tree line and let's create this script and let's double click it to open it up in fiesque code now in here we just want to remove these again we'll probably need to pin a medal we don't need them right now so they're gone we want to basically we want to requite well we want to say public line renderer l/r and then we'll go say read awake we want to say L R is equal to get component line renderer and now it's going to get our line renderer we actually want to go up here and say require component type off and we will say line renderer said it's going to require a component of line renderer before this classes initiated which basically means this will never fail because sometimes I'm awake you could awake it before the line render house awoke so we've got that we now want to create a function called public void render line which is going to take two two arguments which is going to be to factor freeze the first one this can be called the start point and the second one if you can guess it it's going to be called the end point and these go be the two points we create so when we call this function we want to say LR dot position count so if you remember I set the position count on the ball equal to zero which made the line disappear we now need to bring those back so we're going to say it's equal to two we don't want to create a vector free array and say points and we're going to say new vector free and we're gonna pass through a two so there's a new effect free of Ray we're just gonna have to wait it's gonna have two points available in it so we're gonna say points zero because they start from a zero so no we say equal to the start point I think I say points one and we're set that equal to the end point and that is all we need to do for the points we now need to set lips if you look s al are set positions and working to pass through points which is basically gonna add in the two points for us we now want to create one last thing called public void ends line which is basically going to once we've tried and shot it's gonna well actually would do this after so we can demonstrate it so we've created this line but we need to go into a dragon shoot script and basically call this function and call this function we need to say so we've got the mouse down which is fine but now we want to say if the mouse is held down so we'll go say input get but note get mouse get mouse button and we're side equal to zero so this means if the left mouse burn is down or just held then run this script in here every frame so what could I say vector will say set vector free current point is where we're currently dragging our point to I'm going to say is equal to and basically we just want to grab this this code right here again so which one change it to current point said is equal to 15 now all we want to say is TL dot rental we actually need to get a reference to our line or trajectory line which will go called TL which the script is going to be on here and on start we could probably say TL is equal to get component trajectory line and actually I'm going to come here and I'm not going to make this public it's just gonna be one it gets itself and then we're going to say TL dots render line and we will say start point and current point which is our end point so now if we hit save and we go back to our game and we hit play and I click and I drag you'll see we've got the point of our trajectory problem is though it is staying when I let go so although we've let go of detector II it's still showing this line on our screen so that's what the end line was going to be for so let's reopen our trajectory line script and let's create a new function called and line it is going to take no parameters but it's gonna say LR dot position count is equal to zero and that's all it needs to do so it basically just needs to set the count to serious it removes the two which are there we could also we could s many ways you can do this you can deactivate the actual line renderer which probably won't help too much but you could do a lot of different things now we've got out let's go back to drag and shoot and now when we get our mouse button up we want to say TL for R or L R is that notes TL dot ends line and now it's going to call once we let go so if we go into our unity again and we click on the play burn back in the scene so now if we drag and we shoot and we dragon will shoot line disappears after we've shot you could also do other things so the more you drag so the longer it is the different color it changed to and stuff like that so just an indicator of how much power you sync and stuff like that with the line which is pretty awesome but we're not going to go flying this video so I hope you enjoyed the video if you did don't forget to leave a thumbs up if you want to see more then don't forget to subscribe and drop with your feedback and comments down in the comment section below finally don't forget to go check out the deaf logs I'm making that basically the game you saw and start this in unity and it's been pretty fun so fun I'm good be doing a lot more of it so I make those weekly and you can see those every Sunday thank you for watching and peace out [Music] [Applause] [Music]
Info
Channel: Muddy Wolf
Views: 31,695
Rating: undefined out of 5
Keywords: game dev, unity, game development, c#, devlog, indie dev, indie game dev, tutorial, unity 2d, input system
Id: Tsha7rp58LI
Channel Id: undefined
Length: 26min 8sec (1568 seconds)
Published: Tue Nov 05 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.