Bullet Spawn and Movement - Tank Battle - Unity2D

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi there in this series of videos i'm making a version of midway games boot hill which is going to feature tanks instead of the original cowboys in the first few videos i'm going to start dealing with setting up the game as a local couch multiplayer and then in later videos we're going to allow the game to be played over the local network by using udp because that's kind of exciting and in this video i'm going to let the bullets fly so i have a bullet here and i'm going to put this here this is the blue bullet uh and then i've got this kind of um another flavor of tank bullet um these are both gonna be one again and i'm going to have no one's there so this blue bullet is going to fire in that direction and the silver chrome ish gold bullet is going to fire that direction so if i drag this one on here first of all you can see the size of it there which is a good size obviously this is zero zero so i'm going to create another uh bullet object uh so it'll be left player left tank bullet uh because this is oh sorry i'm gonna reset this uh reset position and i'm gonna drag that into there and i'm gonna call that left bullet straight um and it's gonna fire from this point here now i need to know what that point is so if i go into the scene um i'm going to create a spawn point here that's going to fire the bullet so if we're going to left tank uh i'm going to do create empty and that's going to create an empty sprite down here i don't think you can see it that well but if i move the spray over to about uh here ish uh what is that 100 that would be 100 roughly there and then the can move y to about here which is about 42 it doesn't have to be super accurate but that's going to be the the spawn point so our spawn point for the bullet is in here so bullet spawn is going to be there i'm going to do the exact same thing on the right hand side tank so i'm going to do create empty and i'm going to call that bullet spawn um i probably should have made this a prefab but wherever we are uh and then i'm going to create this here i'm going to copy that component there and then the bullet spawn i'm going to paste those component values so if i go over to here you see that it's not quite right because it's actually behind the bullet so i'm going to change that to minus and then it's going to be in the the correct orientation so i'm going to save that now um and then i we need to spawn that that bullet there so here's our left tank bullet um so for our player controller we're going to have to have a public um game object bullet prefab public float bullet direction um and the bullet direction is going to be one or minus one because we're going to be facing either that way or that way and on fire look at that there i'm gonna have a public float bullet cooldown period equals and then it's going to be let's call it um let's say you can only fire every one second um and uh public private bull is cooling down so it's not currently cooling down so i want to say if it's greater than zero and it's not cooling down um uh then you we want to fire so to do fire actually it's going to be start core routine fire bullet and then we're going to have a enumerator fire fire bullet i'm just gonna return a null just now so we're gonna fire the bullet and then we're gonna yield return wait for a second but first we need to do is cooling down equals true so we can't fire anything just now and then we do yield return new wait for seconds uh cooldown and that's that we can get rid of that there so that's gonna be our that's our first line and that's our last thing actually our last line is is cooling down equals false because we're not cooling down after we've cooled down so just to reiterate here we're going to be checking to see if the fire button was pressed if it was pressed and we're not cooling down so we're not in the cooldown period we're going to fire the bullet and we're going to fire the bullet is we're going to say one second uh oh we also need um public transform form um bullet spawn point we need it to be there um we're gonna say um var bullet equals instantiate prefab and then we can also tell the position which is going to be spawn point transform and we need to specify the quaternion which is returning dot identity uh oh yeah so let me just put this all on multiple lines and that'll make it a little bit easier for me to see what's going on there so we're instantiating this copy here um and then we're gonna fire off into the the nether regions we don't have that code yet so we're gonna so this is player controllers and let's call this create a folder um bullet controller um i mean we only really have one bullet controller but i mean we could have projectile controller so we'll call a simple bullet controller um and then we'll have rid of that there enumerator start um and then float time to live equals two seconds so after two seconds it's gonna it's gonna kill itself um and then we'll save while time to live is greater than zero destroy game object um ttl plus equals time dot delta time should be negative delta time because we're subtracting that yield return null so every frame every tick we're going to be decrementing this but we also want to add to the position so it's going to be the same speed actually it's going to be a little bit faster than the the player so the reason why we want to be faster than the player is we don't want the player to catch up with the bullet um or run alongside it it's got to be just a little bit faster than the player so 420 is probably about right um and then we've got public uh float direction and that's gonna be one so we're going to say transform.position plus equals and then it's going to be vector 3 dot right because we're heading right that way times direction times 420 times time dot delta time that's a lot of multiplications in there all right so what's happening here so we're taking the transform position we're going to the right and the right is positive in our in our world or at least for bullets it's positive because we're doing the left player first so we're going to multiply it by the direction so if it's minus one then that means we're heading that way which is left technically we're going to multiply it by 420 and then we're going to multiply it by time delta time we are not checking any collisions just now we're not doing any of that we're just doing uh clear we're just doing bullet movement so that we have the bullet movement um do we need to have that there probably not oh we do need to have that there because we need to do var control equals bullet dot get component bullet control our direction equals direction we need to specify the direction um and we can get rid of that start because we don't need that and we don't need to update because you know what update does um i think that's everything [Music] uh okay that's okay so for our left tank um we now have these values here so we get bullet spawn point um a bullet spawn point which is going to be our bullet spawn for the left tank um yeah here's our left tank so there's a bullet spawners there our bullet prefab is our left tank bullet which is this one here and then the direction is to the right so it's positive to the right okay so uh if i fire oh there you go null reference where's the null reference oh i know why so for our left tank bullet we need to add the controller simple bullet controller oops ah simple bullet controller left tank simple bullet controller okay so we run this all right we need to get rid of that we've just just killed that um yeah so you probably saw what happened there so if you watch this side here after two seconds that disappears which is a problem because if we watch what happens here uh you see we've got a left tank bullet here and then uh missing game object happens so we've got a we've got to stop it from doing that so the way we do that is we go to left tank bullet and then there's a little check box here just uncheck it uh and then inside here what we can do is ctrl dot enabled equals true so we're going to enable it when we we start the script so now and i'll move that bullet in just a second just to show you um when we run this now uh the bullet just stays where it is so if we go back to our scene our left tank bullet uh we're gonna drag and drop that if we can pick it up we're gonna drag and drop that out of the scene and just plonk it down here just so that it's not visible so uh now when we do this here nothing why are we getting nothing [Music] what's happening well uh oh that's why because um it's not the position there it's actually world world position uh that should be the position the world space position of the transform bullet spawn point why is that okay now i'm confused what there's the bullet spawn point there which is at that position uh doesn't that say that right let me [Music] attach this just now okay transform negative 500 that sounds over right oh what's the local position oh wait baby baby what's local [Music] uh transform local position is that okay so if i do i've had problems with this before and i don't know why um this does this [Applause] so i've i've seen this before and and this is one of these foibles of unity uh it never yeah see that's now gonna be so that is so okay if i bring this to here and then i press fire you see that it's just doing it off of zero zero from here uh off of where the the sprite is and the reason why is because this bit never works for me i don't know why but it never works um i think maybe it's something to do with the next frame or something if you get answers uh please comment below but uh yeah this this thing never works i'm gonna um change this uh just now say bullet dot transform equals spawn dot transform dot position and see if that fixes the issue oh yeah transform position uh okay and then let's see if that works so it's it's doing it now i don't want to start on sticky keys um so this is minus 500 from where this position is but not where i want it to be uh okay so bullet spawn is there which is 500 but it's parenting to the wrong thing but what prefab is the bullet prefab the wrong thing no it's not the wrong thing that's the simple one there why does unity do this to me without fail it's like the simplest thing i just want to spawn at this point okay and then it never spawns and then i spend ages debugging it all right okay what am i doing wrong here so if i do bullet dot transform dot local position equals 3.0 so i set that to zero and then i set the world position to be here is this now going to work properly and if so why doesn't it work just want to set the position now that's infuriating uh transform bullet spawn point okay so if i instantiate it at that and i set the local position to that and then i say bullet dot transform.parent equals null is that gonna work oh dear where's the okay bullet spawn point bullet spawn is there okay is that one you can see it flashing like that okay so it's definitely going to this bullet spawn which is this point up here uh local position zero and then i set the parent to be no um what if i just said it to be just that so this is me telling it exactly where it's going to go all right so if i fire this okay that kind of makes sense see that there that kind of makes sense because it's parented to it but the spawn point really doesn't make any sense like i don't understand what i don't understand why the the local spawn point is so far away like uh look how tight we want oh that'll be why never mind okay that makes sense all right so if i take the left tank bullet here and then i just move it um minus six minus a thousand okay okay right ignore me let's just undo everything that i've just done there uh well that's okay unity so the issue was that when i dragged this sprite here it dragged this and not the parent so i'd selected the parent but when i clicked on that it didn't work and then i clicked on that again and then it selected the wrong thing and it didn't notice it so there's a lesson for you make sure you know what you're clicking uh all right so when i instantiate this here bullet the load transform where's the transform position minus -500 okay all right okay so that's that's good okay so it's coming out the right tank so when i fire i can't catch up with it and it needs to fire faster okay well that's that's a simple fix uh let's expose this so we're going to do public float speed r in units per second equals 600 let's have it as 600 because that'll be quite fast um and you'll get quite a good spread i think so we'll make that 600 and we can bring this scene in here put the game there fire and uh okay all right okay so now i'm going to do the exact same thing with the blue one now both the the blue tank and you go there's the blue tank there and the green tank now fire the weapons so that's that's good um and now i think what we'll do is we'll we'll break there and the next video will do the collision detection thank you so much for watching this video this is the boot hill uh series that i'm working on if you enjoyed this video please leave a comment and share on social media it really does help the channel and if you didn't like it please uh please tell me let me know in the comments below what i can do better what videos you want to see if you'd like reminders when i post up a new video click that subscribe button which is just down below there and tap the notification bell and youtube will let you know when i post a new video which is mostly every thursday anyway thank you so much for watching and i will catch you in the next video bye
Info
Channel: Sloan Kelly
Views: 143
Rating: undefined out of 5
Keywords: gamedev, game dev, game development, programming, game design, networking, udp, udpclient, c#, csharp, c-sharp, unity, made with unity, unity networking, networking with unity, unity udp, couch play, couchplay, play on the couch, side-by-side gaming, tank battle, tanks, coding
Id: agZH0650yAw
Channel Id: undefined
Length: 25min 16sec (1516 seconds)
Published: Thu Jul 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.