1 00:00:03,990 --> 00:00:05,190 ‫Come back in this video. 2 00:00:05,190 --> 00:00:09,810 ‫We are going to take care of the camera so that it moves with our player. 3 00:00:09,810 --> 00:00:11,700 ‫So let's create a new script for that. 4 00:00:11,910 --> 00:00:19,050 ‫I'm going to create a new script and I'm going to call that one follow cam because it's the following 5 00:00:19,050 --> 00:00:19,530 ‫camera. 6 00:00:19,530 --> 00:00:24,780 ‫It follows our player, and I'm going to assign that to our main camera straightaway. 7 00:00:24,780 --> 00:00:28,080 ‫So let's drag that into its components. 8 00:00:28,080 --> 00:00:30,360 ‫Follow Cam and now let's open it up. 9 00:00:30,570 --> 00:00:34,650 ‫What we will do here is we're going to use the awake method. 10 00:00:35,610 --> 00:00:39,810 ‫We're not going to use update, but what we all need are two things. 11 00:00:39,810 --> 00:00:42,150 ‫We need the transform target. 12 00:00:42,150 --> 00:00:44,970 ‫So we need the position of the target that we need to follow. 13 00:00:44,970 --> 00:00:50,010 ‫And it's public because we want to assign it from from within unity. 14 00:00:50,370 --> 00:00:52,140 ‫So it's the target. 15 00:00:52,560 --> 00:01:00,570 ‫And then we need a private vector three, which is the offset to our player. 16 00:01:00,570 --> 00:01:06,990 ‫So it's pretty much what is our position in comparison to the player and let's set the offset within 17 00:01:06,990 --> 00:01:08,310 ‫the awake method. 18 00:01:08,310 --> 00:01:19,470 ‫So we just say offset is equal to transform the position minus the target dot position. 19 00:01:19,770 --> 00:01:26,130 ‫So we take the position of our camera itself and we subtract the position of our target, which is our 20 00:01:26,130 --> 00:01:26,790 ‫player. 21 00:01:26,790 --> 00:01:33,960 ‫So the difference here is a3d vector, which is a vector three variable here, and we save that with 22 00:01:33,960 --> 00:01:38,640 ‫an offset and now we can go ahead and update that information. 23 00:01:38,640 --> 00:01:43,740 ‫So we use the void late update method. 24 00:01:43,770 --> 00:01:48,090 ‫Late update is called every frame if the behavior is enabled. 25 00:01:48,090 --> 00:01:51,480 ‫So, so this one is used for cameras in general. 26 00:01:51,480 --> 00:01:56,490 ‫So you later update is something that you should use for camera updates, for example. 27 00:01:56,550 --> 00:01:57,090 ‫All right. 28 00:01:57,540 --> 00:02:00,450 ‫And let's go ahead transform position. 29 00:02:00,450 --> 00:02:05,700 ‫So let's change the position of our camera by the target position. 30 00:02:05,700 --> 00:02:07,980 ‫So let's set it to the target position. 31 00:02:07,980 --> 00:02:14,250 ‫But of course, we may not get the offset, otherwise our camera would be just inside of our player. 32 00:02:14,250 --> 00:02:19,680 ‫And now it's going to be based on the distance that it has from the start. 33 00:02:19,680 --> 00:02:22,500 ‫So let's go ahead and save that script. 34 00:02:22,500 --> 00:02:24,750 ‫Let's go to Unity and let's have a quick look. 35 00:02:24,750 --> 00:02:32,070 ‫So our main camera now will have a new public variable here, which we can assign target, and now we 36 00:02:32,070 --> 00:02:34,230 ‫can say our character is the target. 37 00:02:34,470 --> 00:02:39,780 ‫And as you can see now, our main camera has a position of one, five and minus five, and our character 38 00:02:39,780 --> 00:02:45,300 ‫has a position of let's set that to 00.5 and zero. 39 00:02:45,990 --> 00:02:48,450 ‫So there is a distance or a difference. 40 00:02:48,450 --> 00:02:54,870 ‫So the X difference is one, the wide difference is 4.5 and the different Z difference is minus five. 41 00:02:54,870 --> 00:03:02,280 ‫So now our main camera will always maintain that distance, that offset to our player. 42 00:03:03,240 --> 00:03:05,820 ‫Now let's save that scene and run the code. 43 00:03:05,820 --> 00:03:08,760 ‫So let's have a look if our camera follows along. 44 00:03:08,760 --> 00:03:12,240 ‫And as you can see now, I screwed that one up. 45 00:03:12,300 --> 00:03:13,410 ‫Let's do it again. 46 00:03:15,200 --> 00:03:19,880 ‫Our player is moving and great. 47 00:03:19,880 --> 00:03:21,710 ‫So our camera follows along. 48 00:03:21,710 --> 00:03:22,430 ‫Perfect. 49 00:03:22,850 --> 00:03:23,480 ‫So that's it. 50 00:03:23,510 --> 00:03:28,550 ‫Now you know how to make your camera follow your player, which is perfect for an endless runner. 51 00:03:29,210 --> 00:03:31,970 ‫In the next video, we are going to animate our player. 52 00:03:31,970 --> 00:03:32,840 ‫So see you there.