3D Projectile Projection - Unity tutorial BEGINNER friendly

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

This is my first full tutorial I've made so please give feedback on the video style/speed/clarity!

👍︎︎ 1 👤︎︎ u/koniga 📅︎︎ Jan 23 2021 🗫︎ replies
Captions
hello are you trying to achieve this effect for your game where you can see the path of the thing you're trying to shoot or blast from a certain place well regardless of your answer i'm adam i make games and here's how to do that for this tutorial i'm using unity 2019 but you can use almost any version of unity that you prefer none of these components are recently outdated nor are they recently added our scene right now is just this wall of cubes which are just boxes with box colliders and rigid body components attached to them we're going to start by creating our canon game object so we'll start by creating an empty object and i'm going to name it canon i'm immediately going to drag this canon into our prefabs folder to make it into a prefab i prefer making my prefab start with an empty game object and having all relevant components be a child game object but that's just a personal preference and you can do this any way you'd like to reset its position to zero zero zero and then i already have a canon model already ready to go but you can use anything you'd like even a unity default cylinder would work fine for this now i'm going to take the head component and i'm actually going to raise it up a little bit just so it's out of the ground but this is still set to zero zero zero as its relative position to the head prefab um i'm also going to add what we're going to call our shot points we'll create a second empty game object and we'll call it shot point first we're going to override i'm going to apply all and we're going to do the rest inside the prefab editor we're going to double tap our canon as a prefab and now we're in the prefab editor and now we can look here at our canon and sort of manipulate it in this scene we're going to take our shot point component and we're actually going to use our little gizmo to look to the side and i'm going to click here to go into orthogonal mode then we're going to take our shot point and we're going to put it up to where we want the cannonball to come out of the cannon i'll say roughly around here then we're going to rotate it at this point make sure you're set to local up here on the top left and not global so you can still see the relative orientation of your child game object for this one we're going to rotate it a little bit so that it's about in line with the canon and when we click back to our position changer we can see the green arrow is pointing out in front of the canon that's our up vector and we're going to add a couple components to the parent game object of this prefab the first one is going to be a line renderer of which you don't need to change a whole lot of components about it um you can play with the colors here later if you'd like to to make it not just a white line but the main thing we need to change is make sure that this use world space is checked and for us it is and make sure that we set the line renderer to default line we'll collapse this and we're going to add two scripts which i've already pre-coded but you can feel free to add two empty c-sharp scripts and then follow along as i walk through the code first we're going to add a canon controller which i've already added here and then we're going to add a draw projection script you can see these already have components on them but we're going to walk through what they do before we walk through this code we're going to set up our canon ball game object as well so let's go out of the prefab editor and here we're going to add a new 3d object sphere we're going to reset its position to 0 0 0 and we're going to call this object cannonball we're going to add a rigid body component to it and we're going to set its mass to around 10. now we're going to just kind of look at it relative to the cannon to get its size roughly correct so we'll put it around here it's probably a good size but i'm going to up it up to 1.5 or reset its position and we're going to drag the cannonball into our previous folder to make it a prefab then we will actually delete the cannonball instance from our scene now let's take a look back at our canon prefab in the prefab editor let's walk through the code first we have our canon controller first we set up a public float called rotation speed and this is going to be the speed by which we can turn the cannon left and right or up and down then we're going to add a public float called blast power and this will be the speed at which the cannonball blasts out of the cannon if you're going to change this to whatever you'd like to but i chose five then we'll declare a public game object called cannonball which will be our prefab later and then we'll add a public transform called shot point which we've already made earlier i'm going to drag into this slot i've commented it out but we also have a public game object explosion and that's just for added effect later that i won't cover entirely in this tutorial in our update loop first thing we're going to do is declare a variable called horizontal rotation which will come from input.getaccess horizontal which will read inputs from the a and d keys or the left and right arrow keys then we're going to declare a float called vertical rotation which we'll get from input.getaxis vertical which will read from the up and down arrow keys or the wns letter keys then we're going to adjust the canon's transform.rotation by setting it to quaternion.euler transform.rotation.euler angles which is its current rotation and we're going to add to that rotation a new vector 3 0 because we're not going to rotate it on its x-axis and then we're going to rotate it by horizontal rotation multiplied by rotation speed which will rotate it on its y-axis so left and right and then we're going to rotate it by vertical rotation times rotation speed which will turn it up and down on its z-axis lastly we're going to have a command so you can actually shoot a cannonball so for this we're going to do if input.getdownkey keycode.space we're going to instantiate a cannonball game object at cannonball at shot point dot root position and with shot point dot rotation and we're going to set this instantiated object to game object created cannonball and then in the second line of code rated cannonball dot get component rigidbody and we're gonna set its velocity to shockpoint.transform.up multiplied by blast power as soon as the cannonball is created it will set its velocity to a direction that is outwards from the cannon away from the cannon i'm not going to cover the commented out code in this tutorial but these are just added effects to make the canon work a little bit better with just this script this is the behavior you should see in the game now we can rotate our cannon left and right or up and down now let's take a look at our draw projection script the first thing we're going to do is get the instance of our canon controller and we're doing this because we're actually going to use the cannonballs initial velocity to determine how to draw the line out of the canon then we're going to declare our line renderer and we're going to get that as part of the game object next we're going to declare two variables a public int called num points and a public float called time between points the number of points will be the number of points on your line and the time between points is really just the distance between those points so a high number of points but a low time between points will be a short line that's really smooth and a low numb point and a high time between points will be a longer but jaggedy line so you're going to want to sort of tweak these for your liking then we're going to declare a public layer mask called collidable layers we're going to use this later so that when the line collides with a surface that it shouldn't really be going through the line will stop drawing itself at that point in our start function we're going to set canon controller to get component cannon controller and we're going to start our line renderer to get component line renderer in our update loop we're first going to set our line renderer.position count to num points to make sure the line renderer can take in as many points as we ask it to take in then we're going to declare an empty list of vector3s called points and we're going to instantiate it as a new list vector3 then we're going to get the cannonballs starting position which will be our canon controller dot shot point dot position because that's where the cannonball is coming out of the cannon then we're going to get its starting velocity by doing canon controller dot shot point dot up multiplied by canon controller dot blast power this line of code might seem familiar to you and that is because it's the same exact code that you'll see here shotpoint.transform.up multiplied by blastpower now that we have those two things we're going to draw our line so we're going to do 4 load t and it doesn't have to be t but t is used in the equation that we'll be using that i'll show in a moment float t equals zero t is less than the number of points and then t gets incremented by time between points so t plus equals time too many points now we're going to continually be creating new points and adding them to our list so we'll do vector3 new point equals starting position plus t multiplied by starting velocity and that is because this will handle the x and z components of our new point because in projectile motion the horizontal velocity of an object is actually constant as it moves through the air the only thing that changes is its y velocity and that is what we're going to handle here in this line we're going to pull from an equation that i found online we're going to pull from this one right here y equals y naught which is the original position y plus the original velocity in the y direction multiplied by t minus one half the force of gravity multiplied by t squared and our new point y is going to equal our starting position y naught plus our starting velocity y multiplied by t plus the force of gravity multiplied by 1.5 or divided by 2 times t squared now that we have our point set up we're going to add that point to the list and then we're gonna keep iterating through and adding new points to the list to get the line to stop drawing itself once it hits a wall we're gonna do if physics dot overlap sphere at the point we're looking at two as a radius but you can play with this value to get a different overlaps here of different sizes and we're gonna pass in our collidable layers and see if the length of this resulting array is greater than zero now what does this do physics dot sphere draws a sphere at the point that you request at a certain radius and returns to you a list of all the objects with which that sphere collides so we're asking is to have it draw a sphere at the point we're looking at and if it's collided with more than zero things then we're gonna stop drawing the line so at that point we're gonna have a line renderer change its position count to just the current amount of points that we've got and we're gonna have it break from our four loop then at the very end of all of this we're gonna have line renderer dot set positions to our points two array because this takes in an array of points and not a list of points now when we jump back into unity you'll see there's a lot of things we need to set up in our canon controller we have not set the canon ball so we're going to go into our prefabs and we're going to drag the cannonball prefab here we haven't told its shot point so we're going to drag the shot point from our prefab to this component and then here in the collidable layers we actually need to set everything except the canon ball to be a collidable layer how we're going to do that is we're going to go to layers and i've already set it up here but if you add a layer you can add a new layer called canon ball which i've already done so i will not add it here and then you'll want to go to your canon ball and change its layer to cannonball then when we go back to our canon we're going to have the collidable layers be everything except the canon ball and now it says mix but if we look here everything is checked except the cannonball layer now if we hit play you'll see our drawing does work but this isn't exactly as exciting as we wanted it to be we can go back into our canon and we can change our blast power to say let's change it to 30. and now we can really blast our way through the wall as some added effects i'll change the color here to a sort of yellowy and then change the end to sort of more of an orange and i'll uncomment out some of the code so we'll add a explosion game object and we'll have it instantiate and destroy that explosion object every time we press the space bar and we'll have it shake the screen already made this explosion game object here which all it does is cause this little explosion we won't be covering this today but feel free to look up a unity tutorial on making explosions it's pretty simple we're gonna make sure to drag our explosion game object into the prefab and that's everything i hope you liked this video and if you did please like comment subscribe and i might make more videos in the future thank you very much and goodbye if this tutorial was hard to follow there is also a link in the description for a github repo with this entire project on it
Info
Channel: Adam Konig
Views: 7,322
Rating: undefined out of 5
Keywords: game development, game dev, unity, unity3D, projectile, projection, video games
Id: RnEO3MRPr5Y
Channel Id: undefined
Length: 12min 24sec (744 seconds)
Published: Sat Jan 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.