How To Make a 3D Space Shooter Game in Unity - Tutorial Part 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video the second in my series on how to write a 3d space shooter game in unity i'm going to teach you how to animate the cockpit flight controls and we're also going to add a couple of cinemachine virtual cameras so we can swap between our cockpit view and a follow camera view with smooth transitions so let's jump right in and continue on our game development journey [Music] okay the next thing i want to do is i want to animate these cockpit controls so you see we've got this wonderful joystick and throttle controls here and i've already demonstrated uh in part one how you can go to these controls and you can rotate them so you can see the the joystick is rotating back and forth you can rotate it that way and i could do the same thing with the throttle controls so i think what i'm going to do i'm going to pick this cockpit interiors here and i'm going to go into my scripts folder and i'm going to create a folder and i'm going to call it it's not really an animation but it kind of is i'll just say animation controls and i'm going to create a new script and i'm going to call this animate cockpit controls and selecting our cockpit interiors i'm going to drag this script on there and we're gonna open it up so i know i'm gonna want a few serialized fields and i don't think i'm gonna want start but i am going to want an on update i think but first let's just get rid of start and let's add a serialized field oops there we go serialize field i want to transform for our joystick and again this is totally optional but i'm going to add some odin attributes just to kind of make this a little prettier in the inspector i'm going to create a box group and i'm going to call this flight control transforms and i'm also going to add a float for how far the joystick can move so i'll say end ranges and i'm going to want this to be required and i'll just go ahead and pull that up on this line and then put that down on the next line i will copy this attribute and let's add a vector 3 which is our joystick range and we'll just default that to vector 3.0 to zero it all out and let's create another field with that same attribute and this is going to be a list of our throttle so if you remember if we go back to our excuse me go back to our editor you can see that the throttle excuse me let's see throttle control you can see that it consists of two handles so in the scene view if i can get oriented correctly here you can see that it's got two different handles that move independently so what i'm going to do is in my script i'm going to create a list of transforms and i will call this throttles and once again we're going to have a range so let's add that attribute vector 3 throttle range all right now we're also going to want to have access to our input control add a serialized field oops serialize field required and i'm going to add the inline editor attribute so this is another optional odin attribute and i want the mode to be boxed and this is going to be a ship movement input we call it movement input and then we're going to want a private movement controls variable or actually property and it's going to be a expression body method that will return movement input dot movement controls so essentially this little script is going to use the same input that our ship uses to move to animate our control our cockpit controls so they're going to mirror the input that the ship is doing so we're going to do all of this in on update so our joystick local rotation we're going to convert from euler anger angles to a quaternion because if you remember unity the transform uses a vector 3 for the rotation so we're going to use the helper function quaternion dot euler and we're going to pass in our x y and z coordinates for our euler angle angles so we're going to use the controller input dot pitch amount we're going to multiply that by our joystick range and so that pitch amount is going to be negative or positive depending on whether we're pitching down or up and then we're going to multiply that by the range that we defined for how far the joystick can move up or down and we'll do the same thing with the yaw amount so the y-axis is going to be how much we're yawning so that will be joystick range dot y and then finally we'll take the roll amount and we'll multiply that times joystick range dot z did you all catch that dot x okay saying that local position is a vector three oh i want local rotation boy i'm just uh not on top of my game today all right we'll see if that works and then we'll come back and we'll do the throttle so go back into the unity editor and go back to the game view now let's look at our cockpit interior details here and you can see that odin is giving us some nice warnings here that we have not assigned these values so our joystick we're going to use the handle and we're going to need to calculate what we want our range to be and we can go ahead and add the throttle as well there's one throttle there's the second throttle okay so those are good now let's determine what the extents are that we want this to be able to move so let's pick our joystick handle and let's rotate it on the x-axis so it looks like negative 35 is good for pitching back yeah i think 35 is a good range so pulling back pull back 35 push forward 35 so what we'll do is we'll go down here and on joystick range for x we'll put 35 okay now let's look at it on the y-axis ah it looks like 35 is a kind of a good go-to value for both axes there let's come up here and say 35 and then finally on the z-axis what do you say 35 all the way around i'm not even sure i need to have a vector 3 but we'll go ahead and stick with that um let's hit play and see what happens okay i'm not seeing oh you know what odin's giving us an error because we didn't assign our input so we need to take our fighters ship move oh i also noticed that i did not save my prefab after i added this ship movement input so let's go ahead and say apply all and we're going to drag that onto our movement input over here so let's take that drag it there hit play so we're pitching up we're pitching down we're yawing left we're yawing right now one thing i notice is the role jerks all the way to the x and y location or to the left and right location and the reason for that is the way we implemented our desktop input controls so we're using the vertical axis for the thrust [Music] and then we're calculating the yaw and the pitch based on how far away from the center we are so we're getting a fraction but for roll we're either giving it one or negative one so there's a couple of ways that we could fix that i'll show you both ways one is we can we can use what's called lerp to lurp between the current value and where we want this value to end up and then another way would be able to use an axis on the input manager the same way we're using this vertical axis because this is already going to give us a normalized value from negative one to one but we can we can kind of simulate that in here so let's modify our roll amount in here we're at a local variable we'll call it roll um and then instead of returning this we're going to say roll equals and then we'll put this inside an else statement and we'll say that role equals that so basically we're saying we want our roll amount to be one negative one or zero depending on whether or not we're pressing the q key the e key or no keys and then down here we will set our roll amount and we're going to have to add a variable for that we're going to set that to we're going to use this built-in math f function called lerp and we'll take our current roll amount as our starting point we want to end up at roll and then we're going to adjust that by time delta time and we want to adjust it by a step value so let's just say three and let's create a field for that and we will default that to zero let's go back into the unity editor and i have a syntax error because we are not returning our roll amount all right back into the unity editor that error should go away we can hit play and now when we roll it should be a nice smooth roll instead of just jerking all the way over there left and right we're getting that nice smooth interpolation all right now the second way we could have done that is we could use the input manager so if we go into project settings and we look at the input manager you can see we've got all these different axes and we're only making use of vertical right now and you can see that vertical is mapped to the down arrow the up arrow and also the s key and the w key but you can also specify this sensitivity value and that's basically how much it's applying so it's doing the the lerping basically the built-in interpolation that i just did the input manager is doing that for us and this is equivalent to what i put in my code where i multiplied it by three it does the same thing for gravity gravity is how quickly it will go back to neutral after you stop pressing the key so all i could do if i wanted to handle it through the input manager is i could either add a new axes or i could rename one of the axes we don't use like fire three or jump i don't really know of any ship that jumps i could rename one of those to roll and i could map it to the two keys that we're using q and e and then i could set gravity to three and sensitivity to three and it would handle all of that for us but that's how you would do it i'm just going to go ahead and leave it the way we've done it since it's working um so you can now see both approaches there let's go ahead and animate our throttle so we're going to want to go into our script and we've already got our serialized fields here so let's look at our update method and see what we need to add here let's say vector 3 throttle rotation and we're just going to take the very first throttle that we got and we're going to take its current local rotation and we're going to convert it to euler angles so we're working with euler angles and then we're going to adjust that rotation on the x-axis i'm going to set it to control input dot thrust amount times our throttle range and you know throttle range i don't think it needs to be a vector three i think because we're only ever case so that the joystick we rotated on three axes we're only rotating the throttle on one so let's make this a float and we'll default it to 35 since we know that that worked good for us and now we have this we're going to iterate let's see for each transform throttle in throttles throttle dot local rotation we'll use our little helper quantum dot euler function and we'll pass in our throttle rotation there so that will calculate what our throttle rotation should be adjust it on the x-axis iterate through each of our throttle transforms and set their local rotation to be that throttle rotation let's go back into the editor and hit play and see what happens oh you see the throttle sticks moved oh but only one of them moved so i bet you i added the same handle twice i only have one in there let me just make sure i'm adding the both i'm going to go back to this i'm going to lock that grab these and drag them into throttles there we go unlock that hit play there we go and if i throttle backwards i think that looks really cool all right so that is animating our cockpit movement controls now the next thing i want to do is i'm going to want to add our virtual cameras so right now our main camera is part of the escort fighter and it's it's locked into position behind or inside the cockpit so what i'm going to do i'm going to go ahead and delete my escort fighter prefab and i'm going to go into my prefabs i'm going to open that prefab up and i'm going to delete that camera and i'm going to create a new empty game object here in our hierarchy and i'm going to call this camera setup i'm going to reset our transform to world origin and then in here i'm going to create a new main camera and i'm going to call that main camera and i'm going to make sure it's tagged as main camera cool and while i'm in here i'm going to create a couple more tags i'm going to add a called cockpit camera and i'm going to add a tag called follow camera let me rename that i don't want to have a space in it okay all right so our main camera is there it's tagged main camera it's got our audio listener all of that looks good um so now we want to add cinemachine virtual cameras so all this is by the way we've broken our match rotation on our background camera because i had deleted and re-added the main camera so let's go ahead and put that back there and under our camera setup let's add a virtual camera so we'll go down to right click cinemachine virtual camera and we're gonna we'll just leave that called cmvcam1 and in our escort fighter in our cockpit let's create an empty game object we're going to call that cockpit camera i'm actually going to move this camera into there and i'm going to reset its position i'm going to solo it and we don't want it to do any of this stuff which means we're going to say do nothing do nothing because this one is since it's a child of the cockpit it's going to move and rotate with the ship so we don't need to do any of the cinemachine following looking and stuff like that um so we're going to take this and let's move it up let's say something like 0.5 and let's move it backwards uh so we don't want to go oops what is that that's clipping through there okay i think that looks pretty good i might take our field of view and increase it just a little bit i think that looks pretty good [Music] okay so now we should have a virtual camera that's in our cockpit and it's currently soloed so that's the view we're going to have if we go back to our main camera you see it's added a cinemachine brain so we're going to use this to switch between different virtual cameras so right now we have a cockpit camera we're going to add a follow camera but let's make sure that the cockpit camera still works so i will hit play oh i forgot that we need to change our culling mask on this camera we want this to be depth only and we want to make sure that we don't render the background let's hit play okay so our cockpit camera is still working ah but we're no longer animating our cockpit controls oh you know what i think i i did not what i did not save my train i um prefab i didn't override my prefab before i deleted it and then mucked around with the camera so let's go back and look what is this error by the way animate cockpit controls it wants our movement input let's go back here and override our prefab now let's see what happens there we go so we've got our cockpit camera working we've got our animated cockpit controls all looking good all right so now let's add a follow camera so under camera setup let's right click here send a machine virtual camera and actually let me create an empty game object and i'm going to call that follow the camera i'm going to drag that into there all right now this is going to be a little bit different we want it to follow the fighter we wanted to look at the fighter okay so that all looks pretty good so let's see i think those all look good we're going to want to play with our positions a little bit so let's go up above the fighter of 10. let's go back that's the aim we don't want the aim we want this to be adjusted so let's go above the camera i mean above the ship we're already back about negative 10. let's go back a little bit farther let's just say negative 20. and then i want to increase our yaw damping a little bit so we're going to damp x y and z a little bit i think that all looks good so if we solo we switch between the different cameras you can see our different views there and we can actually enable and disable these game objects so i think that's pretty good for now we're going to need to create a camera manager so up here under managers let's create a new empty game object and call this camera manager and go into our scripts folder you know while i'm in here i'm going to change my view a little bit i'm going to drag this hierarchy over here i'm going to drag our project up under there i'm going to shrink our console down a little bit and i'm going to dock our scene view next to our game view just give me a little bit different perspective to work in all right so under our camera manager go into our scripts folder managers i'm going to create a new script i'm going to call this camera manager select our camera manager game object and drag that camera manager script onto it and open that up in the editor all right and we're going to want some serialized fields well first of all let me create an enumeration i'm going to have a cockpit camera and i want to make sure this matches the name of the tag i created and follow camera okay i think that's good and i'm going to want a serialized field it's going to be required and i will call this box group virtual cameras in the serialized field will be a list of game objects virtual cameras and then in start i will say set active camera and i'm going to pass in cockpit uh virtual cameras dot cockpit camera i will create a method called set active camera and i want to move update up above that okay so what i want to do here is i want to iterate through my cameras and if camera dot tag dot equals um activecamera.2 string then i want camera set active true actually i can simplify this i thought it would give me a nice way to refactor that i think i can just say camera dot set active heinz property really all right now rename it cam oh i think that'll do it for us so that should activate our virtual camera based on which one i pass in cockpit or follow and to test that i'm going to go into our tutorial i'm going to go into our hierarchy and i'm going to by default disable the cockpit camera so only our follow camera will be active until we start playing hmm and it did not pick the cockpit camera oh you know why if we go back to our camera manager i have not added any virtual camera so let's add cockpit camera and follow camera hit play and still did not choose that camera cockpit camera followed camera did i tag them correctly i did not all right let's try it now there we go we zoom into our cockpit alright so our camera manager will switch between the follow camera and the cockpit camera so the next thing we want to do is in our camera manager we want to be able to switch cameras by pressing different keys so i'm going to have a function or really can be a property put it up here i will call it um [Music] camera key pressed and i will say if well let's see four and i equals zero i is less than virtual cameras dot count increment i if input dot get key down key code dot alpha one plus i return let's make this an enum return i plus well i cast as a virtual camera so this one will be zero and this will be one okay otherwise return negative well we'll add no camera well let me give it we'll just default how about we just default to the cockpit camera no because i don't want to i don't want to keep calling that set act well it doesn't matter i don't think it's going to affect performance that much so an update we will set active camera camera key pressed let me just go back to doing this so no camera so i'm not picking any camera i should return that and i'm going to do some debug log statements here and go back into the editor keep an eye on the console so we see a bunch of no cameras i press one cockpit camera cockpit camera now hit two oh it's working now i don't really like the way that looks though that's a very strange follow camera to be sure gonna have to tweak those cinema machine settings so cockpit follow so it works but it doesn't really look like i would expect a good follow camera to look but i do like the transition between the cockpit and the follow let's take a look at our follow camera settings our cinema machine settings and what are some things that we need to do to change i think i want to change this to just be locked to target let's round that out to 2.5 that looks fine i don't think we're doing any roll damping i think we want to we're not doing any pitch damping do i want to change anything under aim maybe go a little bit above anything else i want to change in here let's give this a try okay so we're in the cockpit i switched to follow that looks better yeah that's a pretty decent follow camera very cool switch back to the cockpit camera i like it okay so we've got our cockpit controls animated and we have virtual camera switching between the cockpit and a follow camera next maybe i'll work on animating the engine turning the thrusts on and off adding a trail renderer maybe particle system to it and eventually we're going to have more virtual cameras we're going to have a camera for uh like when the ship is destroyed we'll switch out to give a good cinematic view of the ship being blown up i think that'll look pretty cool we may add some other cameras just for cinematic effects cut scenes or something but so that about wraps it up for this episode if you found this useful and i hope you did please do me a favor and click that like and subscribe button it really does help me to grow my channel and if you have any questions or comments or suggestions drop a chat down below and i'll do my best to respond to them as soon as i can so thanks again for watching and good luck on your game development journey [Music]
Info
Channel: Midnite Oil Software LLC
Views: 4,368
Rating: undefined out of 5
Keywords: unity, tutorial, games, game development, unity game engine, madewithunity, indiegamedev, indiedev, unity2d, unity3d, tutorials, cinemachine, virtual camera, follow camera, cockpit camera, animation
Id: FkQljYVEBVM
Channel Id: undefined
Length: 45min 56sec (2756 seconds)
Published: Sat Jun 25 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.