MULTIPLE TARGET CAMERA in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Thank you for another great tutorial! You made something that appears challenging very easy to accomplish.

👍︎︎ 2 👤︎︎ u/BeguiledAardvark 📅︎︎ Dec 18 2017 đź—«︎ replies
Captions
in this video we'll make a camera that follows around multiple targets also make the camera zoom in and out to fit the players this video is sponsored by udemy udemy is an awesome site for learning new skills and they have some really cool courses on game development in fact they just launched a unity course that is made in collaboration with unity themselves the course is called the ultimate guide to game development with unity and will take you from complete beginner to having a solid understanding of the engine as well as programming in C sharp the course also includes over 30 interactive challenges that will help you build both a 2d Galaxie shooter and a 3d first-person game and to make things even better the course tutor is a good friend of mine Jonathan Weinberger so to get started simply click the link in the description and get a discount along the way also special thanks to Judy Minh Arman DeRussy and infinity PBR for their support on patreon and with that let's get started so here I have a scene with two players that I can move around but currently the camera is completely static if I select it you can see I have nothing on here other than some color correction so let's fix this by making a new script that's it a component we call it multiple target camera let's hit new script C sharp and hit create an ad and let's double click it to open it up in visual studio now first let's remove our two methods to make some space and the very first thing we need is a list of all the targets that we want to follow so we'll create a public list of transforms and we'll call it our targets then we of course need to update the position of our camera according to these targets and we want to do that inside of the update method because we want to do it each frame however I don't want to use the ordinary update because that's also what I use for moving around the players instead what I want to use is late update this function is called in the exact same way as update but right after which makes it great for camera movement because our camera will only move after everything else has so inside this method we want to move our camera normally we do that by simply saving our cameras position equal to our targets position but in our case we have multiple targets so what position should we use the answer is that we want to set our cameras position to the center of all of the targets to do that will create a vector3 call it our center point and equal to a new method that will create cold get center point I will create that down here it's going to return a victor three we're going to call it get center point and it's not gonna have any arguments now first we want to check if the amount of targets we have is only equal to one in which case we don't need to do any calculations to find the center it's simply going to be the first targets so targets zero positions of dot position however if we have multiple targets we don't know their Center beforehand the way to find this out is by using something unity calls bounce bounce is a class in unity that we can use to encapsulate multiple objects if that sounds confusing to you try and further along so imagine we have three players and we want to create a box around these three players well for this we can use unity bounce if we add an object to this bounce class unity will automatically create a square to surround him as we add more objects unity will resize this square to fit them the cool thing about this is that the center of this box will always be the center between the objects and so this dot here will be where we want our camera to point to create this box through code we go bar bounds and set it equal to a new bounds and here we just want to start off our Bucks at the center of our first target and with the size of zero so vector 3.0 we can then loop through all of our targets so for int I equals zero as long as I is less than target's dot count in other words for each target we want to encapsulate this target so bounds that encapsulate and this will resize the box to now fit the new target so we'll feed in targets I dot position I'll close that off and then to get the center point we can simply return bounce dot Center and it will do all the appropriate calculations for us really really cool so now inside of our late update we have the center point of our objects and we could now go ahead and set transform that position equal to this center point directly but instead we probably want to offset our position a bit there's a good chance that we want to pull it back a little bit in order to view more of the scene and we might also want to adjust the height of our camera too this will create a variable will create a public vector three and we'll call it offset and then before we set our position will create a the vector3 we'll call it our new position and set it equal to center point plus our offset then when we set our position we said transformed up position equal to our new position and now our camera should actually follow around our players however if our targets happen to be zero we'll probably get an error so just at the top of our late update we'll make sure to check if targets dot count is equal to zero we'll simply return and not do anything let's now save this and hit into unity and you should now see both a list of targets and an offset if we go ahead and hit play of course at first nothing will happen here but if we now go and drag in player one to a list of targets you can see that our camera snaps immediately to his position we then used the offset to pull our camera back on the Z here I'm gonna pull it all the way up to around 50 as well let's pull it up a little bit so on the Y here I'm gonna erase it and our camera now follows around player 1 to also make it follow around the other player will it take player 2 and drag it into a list of targets and boom it's now going to follow the center of our two players so when I move our two players our camera will move with them let's copy these values so right click copy component exit play mode and paste them back in great so now our movement is working but it doesn't look that good yet that's because we want to add in some smoothing in order to make things feel more fluent to do that in our script whenever we set our position instead of just setting it equal to our new position directly we'll use vector 3 dot smooth tamp this is a function in unity that allows you to smooth out movement and this takes in a couple of arguments first we want to give it our current position we then want to give it our target position which is our new position and we also want to give it a variable that it can use to keep track of our current velocity to do that we go to the top and create a private vector3 called velocity now we don't need to edit this in any way it's only used by the smooth dam function and because we're not just feeding it the value of our velocity but instead want the function to modify it over time we use the keyword ref this stands for reference because we're referencing this variable so that the function knows where it is and then we'll write velocity finally we want to specify a smooth time let's go to the top here and create a public float called smooth time until it equal to something like 0.5 let's now feed it in down here and close it off and if we now save this go into unity and play we can see that our camera moves much much smoother and it's already starting to feel really good but there's something missing whenever the players are close to each other there's no reason to be this zoomed out we want to get as close to the action as possible so let's make our chemists seem in and out according to our players positions you can of course do this by simply moving the camera on the z axis but I think a much nicer effect is if we go to the camera and adjust the field of view through script now notice with the field of view as I increase it it zooms out and as I decrease it it seems in so now inside of our script let's take all of our movement code let's cut it and let's move it into a separate function void called move and let's paste the code here we then simply call move from inside of our late update that looks a lot cleaner and right next to this we can call zoom so again we want to go void zoom and now we can put all of our Suman code in here now the first thing that we need for this is a public float with our minimum zoom distance let's set this to a fill the view of 40 will also create a public float with our Mac zoom and that set this to a field of view of 10 now in order to do the actual assuming we need to figure out when we want to zoom in and when we want to zoom out and I found the best way to determine this is by using the greatest distance between the players so if we again have a look at our three players they all have a distance between them but one of these distance is greater than the others if we always take the greatest distance between the players will know how much to zoom out in order to fit all of them on the screen and in fact we can still use bounds to calculate boxes around our players but this time instead of getting this Center we simply want to get the width of the box so to do that through code let's create a function called get greatest distance and for now we can just show this value in the console so debug deadlock get greatest distance and of course we can create that method down here so this is going to return a float we'll call it get gray distance and again we want to create a variable called bounce and set it equal to a new bounce here we want to feed it our targets zero dot position and give it a size of 0 just like we did down here and again we want to go for and loop through each of the elements that will continue as long as I is less than target start count and for each of them we want to tell our bounding box to encapsulate the target the only thing that's different is that instead of returning bounce dart Center we simply want to return the width of the box so we'll go bounce dot size dot X and there we go so now if we save this go to unity and hit play you can see that it shows the distance between our players as I get closer to the other player it shrinks and as I get further away it grows and the cool thing about this is that if we go ahead and add a third player and remember to add him to our list as well you can now see that it shows the distance between our leftmost and rightmost player so if I move my player in the middle here nothing happens not until I cross one of the other players in which case the value starts changing pretty cool right so now that we have this value we can use it to determine our zoom to do that we'll create a float called our new zoom and we'll set it equal to math dot Lert so this method linearly interpolate between two values depending on a third value so we want to go between our max zoom and our minimum zoom depending on our you guessed it greatest distance however do note that the third value here normally goes between zero and one zero meaning maqsuum and one meaning minimum zoom and the distances in my game is normally around one and fifty so we definitely need to divide this by around 50 let's also make a variable of this so let's go to the top here and write public float and we can call this something like zoom limiter I will write that down here then finally we need a reference to our camera to do that we'll go to the top and create a private camera variable let's call it cam we can then create a start method where we'll set cam equal to get component of type camera and just to make sure that there's always a camera where this script is sitting we can go to the top here and require component of type camera and now down here in our still method we can set cam dot feel the view equal to our new zoom and just like we don't want to set our position directly but instead we want to smooth it we also want to smooth out our field of view so here we can use math dot lubricant to smooth it out we want to go between our current field of view so cam that feel the view and our new field of view so our new zoom and we want to do this based on time delta x so now it should smooth out our assuming and if we save this go into unity and full screen this we should see that our camera zooms in and out as our players get closer together in fact if I go ahead and cram all of them into the closed space here it will zoom all the way down to a field of view of 10 and if I get really far from each other it's going to zoom all the way out to a fill the view of 40 and the super neat part of this script is that you can easily update your targets array at runtime so if you wanted to kill off player 3 all you need to do is simply remove him from the list and there you go the camera now completely ignores him yay so that's pretty much it for this video again definitely check out the ultimate guide to game development with unity simply click the link in the description to get a discount on that thanks for watching and I will see you in the next video text of the awesome patreon supporters who donated in November and especially thanks to Judy Minh Amanda rue C infinity PPR and soft tune cyber crime Derrick Eames Kirk Faisal Murphy James P Dan Evans Thomas Wally Superman the great John Beauregard Kolka brown Jason the Tito Alex Kirk Itsuki manulis James Rogers Robert pond Rob fan and Rasmus you guys Rock
Info
Channel: Brackeys
Views: 132,035
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, beginner, easy, how, to, learn, course, series, tutorial, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, camera, multiple, target, targets, follow, more, many, player, players, script, fov, field of view, fieldofview, zoom, average, position
Id: aLpixrPvlB8
Channel Id: undefined
Length: 12min 55sec (775 seconds)
Published: Sun Dec 17 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.