Projectile Trajectory Prediction in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we are going to learn how to draw a trajectory line for a physics-based projectile in unity hi I'm Peter and welcome to Sunny Valley Studio tutorials for this video I have prepared the simple Canon shooting project where I can aim and shoot a bullet or a cannonball out of my Cannon but the issue is that without a trajectory line I'm kind of shooting and hoping that I will hit a ship although they are pretty close to me so this is not that bad but the issue is that I'm not able to see or predict where our bullet will land so I would add to it a trajectory line so that it is easier for me to shoot those ships now to visualize our trajectory line we can use the line render component from Unity but we are also going to need a custom script by the way this project is available on my patreon the link will be in the description if you want to grab it so first I have this Canon object in my project and I will want to draw a line from this Canon from the muzzle of this canon in the Direction Where The Cannonball will fly so I'll select this Canon select this plus icon I will select the effects and I will select this line which contains the line render component by default now we can select it in the scene View and we can drag it up to see it better and let me press F to focus on it it doesn't look that great so first thing that we need to do to tweak the look of it is to go to our projects Tab and we are going to create a new material for this line render so right click here select create and I'm going to select the material now the new material will be created I'm going to call it line render material and I'm going to select in the inspector Shader I'm going to select Universal render pipeline since I'm using this pipeline for this project and I'm going to select the particles on the lit if you are using the standard Frederick pipeline you should find the same Shader but not in the Universal render pipeline category okay so now I will select my line and I will select in the inspector somewhere in the middle there should be this materials tab I'll drag here our line render material here and now it is a line so that's great it looks much better now in addition to that I can select my material and I have created this texture that is aligned with the outline for it so that we can better see the line render so I'll select my little trajectory line folder line render and I will drag you to the surface input of my material my texture to the base map here and here we have in the scene view a much more visible line render now if your line render is a bit thicker than mine you can select your line render component in the inspector and change the width because probably you have width of one just set it to be 0.1 I find this with to be Pro appropriate for my project although this is something that you want to tweak the next parameter that I want to set is this above the material stuff there is this use World space since I will be using World space coordinates to set the trajectory line of my Canon so I'll set this used World space to be true since now those points specified in this point array are going to represent the world coordinates so now as you can see our line render has disappeared and is probably much lower where there is point zero zero zero in the UNT game world okay so this is it for the setting of the Align render now we need to have a custom script so I'll select my folder trajectory line right click create a new c-sharp script let's call it trajectoryline okay great let me paste first the fields that we are going to have obviously we are going to need the reference to the line render so I have a serialized field private line renderer line render next I'm going to have the line segments integer value so serious field with the mean value of 3 so minimum I want to have at least three line segments private in align segments this will allow us to smooth our trajectory line for it not to be that Jagged because we are basically creating a line from point to point so the more point we create the smoother the line will be and last thing is a series field with the mean value 1 private float time of the flight probably it should be time of flight I have set it to 5 this will be in seconds what we need to do is we need to calculate the position of our projectile when it is flying towards its Target another simple example we can assume that only the gravity force is applied to it beside the start velocity or the force that we apply to the rigid body when we shoot our projectile so for the simple simulation we can use this trajectory formula to calculate the position of our projectile at each point when this traveling on its path towards hitting the ground or even going further because in our case we have no idea if it will collide with something or not this basically is the main difference between this example and our case here we can calculate the time of the flight of this object that is shot here but in our case since we have no idea if it will collide with something we need to assume how long it will fly this is our five seconds time I'm going to leave the link to this article in the description of this video so that you can get familiar with this equation if you want to so that's the point of this time of the flight parameter we are going to calculate the trajectory in terms of the next 5 Seconds of the flight of our projectile this might be something that you want to tweak in your own project to draw a shorter or a longer trajectory line next I'm going to create two more methods and here are the methods first one will be a public method void show trajectory line that will take in a vector 3 start position and a vector 3 start velocity as you might recall those are the parameters that we know next depending on the line segments value we are going to calculate a Time step so basically for each time step we are going to calculate the new position of our projectile that we are going to base on the equation that I have shown you just before and this will be equal to time of the flight so our 5 seconds divided by the line segments and this basically means that the more points we calculate the smoother the line will be next we are going to have a vector 3 array line render points equals calculate trajectory line start Point start velocity and we're going to pass here the time step so let me show you this method this is the bulk of the code that we need to draw our trajectory line at the same time this is basically the same equation that I have shown you just presented as a c-sharp code so we return here a vector 3 in this method and this method is called calculate's trajectory line it takes again the start point and the start velocity as Vector 3 as well as our float time step so we need to First create a vector 3 an array of vector threes line render points equals new Vector 3 array of the length of line segments this is just for efficiency we could very well use our list here now the first point obviously will be the start point so the line render point zero which is equal to the start Point next we are going to Loop 4 r i equals 1 since we already have the first point I less than line segments so this is the number of line segments so how smooth our line will be I plus plus and basically what we need to do is we need to calculate the time offset So based on our time step we're going to calculate the position of our projectile per each time step in the future so we need to know how far in time in advance we want to calculate this position so this will be the time step so per each segment we are going to Simply multiply this time step by I and we're going to get the time offset value so this is the simplification of the equation that I have used to create it in C sharp first of all we need to calculate the vector 3 position progress before applying gravity so progress before gravity is equal to the start velocity multiplied by the time offset as you might recall if no force is affecting a body the body stays in motion and the same force is applied to it so in our case this is the start velocity and we are going to multiply this by the time offset now obviously the gravity is affecting our projectile so we need to calculate how gravity will affect it and how it will modify its position so Vector 3 gravity offset will be the vector 3 up so the up Direction multiplied by the minus half and this is from the equation times the physics.gravity.y and we should multiply it by the time offset to the power of 2 so I just multiplied two times and this will be the gravity offset that the gravity will apply on our projectile when it is moving so as you might imagine now we need to put both of those values together so we have this Vector 3 new position and basically this is the same equation that I have shown you this will be the start point so since we have the offset from the start position we're going to get the start Point thus the progress before graph gravity and we need to subtract from it the gravity offsets of graph how gravity has affected our a projectile how it has dragged it downwards so now the line render point at the at the position I will be our new position that we have calculated here now I'm sure that my explanation isn't perfect make sure that to check the Khan Academy if you want to understand how the trajectory path is calculated but in a nutshell this is the c-sharp code that should calculate it for us so we need to look for each line segment and we need to return our Vector 3 array to our show trajectory line method so now to keep our all the trajectory line while we calculate the new values we are going to set line render that position count equals the line segments instead of setting it to zero and we're going to set line render set position to our new array so set position sticks in the vector 3 array so that's what we have returned from our calculate trajectory line method okay great so last step is to the call this method somewhere so let's go back to our project if you are enjoying this video so far be sure to click the like button share this video with others or click the things button to support the channel so I would really appreciate it thanks okay great all I need to do is Select my object that I want to have the line the trajectory line script on so this will be my Canon and I will drag here my projector line script okay I haven't saved it so now after saving I can see the fields that I need to fill so I need to have the line render a reference in our case this is the line that we have created previously so I will drag it here as a reference and this is the trajectory line script now I need to call the method on it to start drawing the line so I'll select the Canon shooting script that I have created previously and I will edit this script okay great now this Canon shooting script basically is responsible for shooting The Cannonball when I press the spacebar and I'm not waiting there is no delay between the shots so in this case I'm just creating the accountable and I setting I'm setting this rigid body and adding to it the force constantly I want to draw the trajectory line so I will need to have a reference to the trajectory line a script that we have created previously so let's create a private and this will be a serialized field and we are going to create a reference to the trajectory line and I'm going to call it trajectory line okay so in the update we need to draw our line so we are I'm going to call trajectory line and we get the show trajectory line and what we need to do here is we need to pass the start part I have created this rarest field private transform muzzle which is basically the muzzle of my Canon so I'm going to use this muzzle and I'm going to pass the position since this is the start point when I want to start drawing my trajectory line the next step is to calculate the start velocity so what we said in the art force in our case the velocity is simply the muzzle.forward direction since this is the direction of movement times the short force in this case I am setting it to be float equal to 30 I think I have increased it in the script in the inspector but basically we need to calculate the same thing so muzzle dot forward which is the direction times the short Force but that's not all because the rigid body has also the mass since the Cannonball has a mass wind to apply appropriate Force to make it move far enough to hit the chip so we need to divide this muzzle forward and times the shot Force by the mass of the Cannonball I have saved it again as a float value in my Canon shooting script this is the velocity that we want to pass to our show trajectory line method and this will be it now again if I slide down you can see that here when I press the space where I'm instantiating the ball I'm getting the rigid body I am setting the mass of the rigid body and I'm adding the force here I do not need to include the mass because the rigid body knows about his own Mass so it is applying to this Force the mass of the rigid body and I'm using the Force Mode dot impulse to make the ball fly in the direction where I am shooting okay and that's basically it I'm going to save the script and go back to my project great I'm going to select the Canon the Canon shooting script and I'm going to just drag here the trajectory line reference and now we should be able to press play and see the result okay and here I am inside the game and I can place my rotate my Canon up and down and left and right and I can see that the trajectory line is being drawn I'm going to press spacebar and as I can see the the ball is following this trajectory line because we have included all the forces that can affect it and of course in my case I can hit the ships and I can damage them so basically this is much easier to aim at those ships if we can see the trajectory line I have mentioned that this trajectory line that we are drawing here is dependent on the line segments value in this case we are calculating 59 segments we're starting from index 0 so this is 60 points and in our case we can modify the number of segments in the trajectory line script so if I continue playing and if I modify the line the number of segments to be for example 5 as you can see maybe I will turn to the wireframe mode as you can see our line here is very Jagged because we are basically calculating point where our projector will be with a bigger time step that's why this is jugged if I'm going to select the Canon and I'm going to increase the line segments value to be 200 we're going to get the perfectly smooth line where we can follow how our bullet will be flying but this will will cost us some performance that's why we can limit the time of the flight so for example we can set it to two or one as you can see now where when it is one it is much shorter if this is two it is a bit longer let's take a look at it it is probably enough for this purpose so basically those are the parameters that we can set if I set it to be 60 it is quite smooth line so it is enough for us to have a smooth trajectory line and we can control the length of it okay great if you want to learn more about making video games in unity check out my video courses the link will be in the description thanks a lot for watching see you in the next tutorial
Info
Channel: Sunny Valley Studio
Views: 8,630
Rating: undefined out of 5
Keywords: trajectory line unity, unity projectile curve, bullet curve unity
Id: U3hovyIWBLk
Channel Id: undefined
Length: 17min 20sec (1040 seconds)
Published: Fri Oct 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.