0:00:02.372,0:00:05.599 So far we have created a play field 0:00:05.599,0:00:07.807 and a Player game object 0:00:07.807,0:00:09.807 and we have the player moving 0:00:09.807,0:00:11.807 under our control. 0:00:11.807,0:00:13.807 In this lesson we'll create 0:00:13.807,0:00:16.112 the collectable objects 0:00:16.112,0:00:18.112 for our player to pick up. 0:00:19.097,0:00:21.097 First open the Sprites folder. 0:00:22.026,0:00:24.026 Drag the sprite Pickup 0:00:24.026,0:00:26.026 in to the hierarchy. 0:00:30.353,0:00:32.353 You'll notice after dragging in 0:00:32.353,0:00:34.353 the Pickup sprite that we 0:00:34.353,0:00:35.795 still can't see it. 0:00:36.811,0:00:38.811 That's because it's being rendered 0:00:38.811,0:00:42.359 behind both the Player 0:00:42.359,0:00:44.359 and Background sprites. 0:00:44.359,0:00:46.359 To bring it forward we 0:00:46.359,0:00:49.267 need to set the sorting layer 0:00:49.267,0:00:51.267 in the sprite renderer component. 0:00:53.042,0:00:56.442 Set the sorting layer to Pickups. 0:00:58.952,0:01:00.952 Now the Player game object 0:01:00.952,0:01:02.952 is still in the way. 0:01:03.604,0:01:06.121 This is because the Player 0:01:06.121,0:01:08.121 sorting layer is in front 0:01:08.121,0:01:10.121 of the Pickups sorting layer. 0:01:11.092,0:01:13.995 Let's temporarily deactivate 0:01:13.995,0:01:15.995 the Player game object. 0:01:16.342,0:01:18.342 Select the Player game object 0:01:18.342,0:01:20.342 and deselect the checkbox 0:01:20.342,0:01:22.342 in front of the Name field. 0:01:22.911,0:01:25.866 This is the game object's Active checkbox. 0:01:26.254,0:01:28.422 Deselecting it will deactivate 0:01:28.422,0:01:30.615 the game object in the scene. 0:01:30.615,0:01:32.615 This will give us a clear working space 0:01:32.615,0:01:34.615 for our new pickup object 0:01:34.615,0:01:36.172 which we can now see. 0:01:37.031,0:01:39.031 Focus the scene view camera on the 0:01:39.031,0:01:41.031 pickup object by highlighting it 0:01:41.031,0:01:43.031 and pressing the F key whilst 0:01:43.031,0:01:45.031 the cursor is over the scene view. 0:01:46.043,0:01:48.043 So that we can collide with the pickup 0:01:48.043,0:01:50.858 let's add a circle collider 2D 0:01:50.858,0:01:52.509 component to it. 0:01:52.509,0:01:54.509 With the pickup highlighted click 0:01:54.509,0:01:58.109 Add Component - Physics 2D 0:01:58.109,0:02:00.597 then Circle Collider 2D. 0:02:01.968,0:02:03.968 Adjust the Radius property 0:02:03.968,0:02:05.968 of the circle collider 2D 0:02:05.968,0:02:09.292 so that it fits the sprite artwork visually b 0:02:09.618,0:02:11.887 by dragging in the Radius field. 0:02:13.454,0:02:15.454 To be effective the collectable 0:02:15.454,0:02:18.071 must attract the attention of the player. 0:02:18.778,0:02:21.141 So let's make the object more attractive. 0:02:22.012,0:02:24.012 There's one thing I feel certainly 0:02:24.012,0:02:26.012 attracts the attention of a player 0:02:26.012,0:02:27.803 and that is movement. 0:02:27.803,0:02:29.540 So let's rotate our pickup. 0:02:29.969,0:02:32.384 One way to do this is with a script. 0:02:33.397,0:02:35.924 With the pickup object still selected 0:02:35.924,0:02:37.924 use the Add Component button 0:02:37.924,0:02:39.924 in the inspector. 0:02:40.423,0:02:43.294 Let's create a new script called Rotator. 0:02:44.365,0:02:46.365 Click Create and Add to confirm 0:02:46.365,0:02:48.365 and let's organise the script by 0:02:48.365,0:02:50.365 placing it in the Scripts folder. 0:02:52.445,0:02:54.445 Open the Scripts folder and 0:02:54.445,0:02:56.445 double click to open it for editing. 0:02:58.775,0:03:00.775 We want the sprite to spin and we want 0:03:00.775,0:03:02.775 to do it with this script. 0:03:02.775,0:03:04.775 Let's remove the sample code 0:03:04.775,0:03:06.775 we don't need. 0:03:07.718,0:03:09.718 We will not be using forces 0:03:09.718,0:03:11.718 so we can use Update 0:03:11.718,0:03:13.718 rather than Fixed Update. 0:03:14.176,0:03:17.362 We want to rotate the object every frame. 0:03:17.362,0:03:19.362 To make the pickups spin we 0:03:19.362,0:03:22.025 don't want to set the transform rotation 0:03:22.025,0:03:24.679 but we want to rotate the transform. 0:03:25.581,0:03:28.757 Type Transform inside Update. 0:03:31.767,0:03:33.767 Select it and hold down the 0:03:33.767,0:03:36.168 control key on windows or the command 0:03:36.168,0:03:38.684 key on mac and type ' 0:03:40.512,0:03:42.512 Again this brings up the page 0:03:42.512,0:03:44.329 with a search term Transform. 0:03:44.845,0:03:46.845 Select Transform. 0:03:48.953,0:03:53.046 Scroll down until you see Transform's public functions. 0:03:54.656,0:03:57.930 There are two main ways to affect the transform. 0:03:57.930,0:04:00.390 These are Translate and Rotate. 0:04:01.194,0:04:03.194 Translate moves the game object 0:04:03.194,0:04:05.194 using it's transform. 0:04:06.317,0:04:08.582 Rotate rotates the game object 0:04:08.582,0:04:10.582 using it's transform. 0:04:11.511,0:04:13.511 We will use rotate. 0:04:14.357,0:04:16.357 So let's click on the link. 0:04:16.357,0:04:19.694 This brings up the page for Transform.Rotate. 0:04:20.901,0:04:23.482 Note again the two signatures. 0:04:24.342,0:04:27.381 One is using a vector3 0:04:27.381,0:04:29.381 and the other is using 3 0:04:29.381,0:04:33.697 float values for X, Y or Z. 0:04:34.501,0:04:36.501 Both have the optional parameter 0:04:36.501,0:04:38.501 Space which we will 0:04:38.501,0:04:40.501 leave at default for this lesson. 0:04:41.153,0:04:43.153 Again we will choose the most simple 0:04:43.153,0:04:46.603 form that only uses the vector3 for direction. 0:04:47.497,0:04:49.497 Let's return to our code. 0:04:51.633,0:04:53.633 After transform, 0:04:53.633,0:04:55.633 making sure that transform is written to 0:04:55.633,0:04:57.245 begin with a lowercase t, 0:04:57.910,0:05:06.416 write .rotate (new Vector3 (0, 0, 45)) 0:05:07.747,0:05:09.747 This means we'll rotate 0:05:09.747,0:05:12.179 around the Z axis. 0:05:12.857,0:05:14.857 Now this action also needs 0:05:14.857,0:05:17.596 to be smooth and frame rate independent 0:05:18.109,0:05:21.049 so we need to multiply the vector3 value 0:05:21.049,0:05:23.932 by Time.deltaTime. 0:05:25.430,0:05:27.430 It is worth noting that even 0:05:27.430,0:05:29.430 though we are working with a 2D 0:05:29.430,0:05:32.066 sprite we are using a vector3 0:05:32.066,0:05:34.846 to rotate the collectable's transform. 0:05:35.373,0:05:37.373 This is because the transform 0:05:37.373,0:05:39.373 of the 2D sprite 0:05:39.373,0:05:42.274 still exists in the 3D volume. 0:05:43.037,0:05:45.037 When using the transform component 0:05:45.037,0:05:47.037 with 2D objects we simply 0:05:47.037,0:05:49.908 ignore the axis we don't need. 0:05:50.657,0:05:53.317 Now that we are done with our Rotator script 0:05:53.317,0:05:55.944 save this script and return to Unity. 0:05:56.859,0:05:59.371 Let's test by entering play mode. 0:06:00.052,0:06:03.368 And we can see our pickup object rotates. 0:06:04.909,0:06:06.909 Let's exit play mode. 0:06:07.838,0:06:10.551 Okay, we have the start of a working 0:06:10.551,0:06:12.256 pickup object. 0:06:12.256,0:06:14.256 Next we want to place these pickups 0:06:14.256,0:06:16.062 around the game area. 0:06:16.672,0:06:18.672 But before we do this we need to do 0:06:18.672,0:06:20.142 one important step. 0:06:20.880,0:06:22.880 We need to make our pickup object 0:06:22.880,0:06:24.271 in to a prefab. 0:06:25.102,0:06:27.102 Remember, a prefab is an 0:06:27.102,0:06:29.102 asset that contains a template 0:06:29.102,0:06:31.768 or blueprint of a game object 0:06:31.768,0:06:33.768 or game object family. 0:06:34.378,0:06:36.378 We create a prefab from an 0:06:36.378,0:06:39.650 existing game object or game object family 0:06:40.122,0:06:42.122 and once we create it 0:06:42.122,0:06:44.122 we can use it in any scene 0:06:44.122,0:06:46.122 in our current project. 0:06:47.107,0:06:49.107 With a prefab of our pickup object 0:06:49.107,0:06:51.107 we will be able to make changes to 0:06:51.107,0:06:53.107 a single instance in our scene 0:06:53.107,0:06:55.387 or to the prefab asset itself. 0:06:55.886,0:06:57.886 And all of the pickup objects in our 0:06:57.886,0:07:00.883 game will be updated with those changes. 0:07:01.758,0:07:03.758 You'll notice we already have a folder 0:07:03.758,0:07:05.437 to hold our prefabs. 0:07:05.980,0:07:07.980 Let's drag the Pickup game object 0:07:07.980,0:07:09.980 from our hierarchy and place 0:07:09.980,0:07:12.415 it in to our Prefabs folder. 0:07:13.395,0:07:15.395 When we drag and item from our hierarchy 0:07:15.395,0:07:17.395 in to our project view 0:07:17.395,0:07:19.395 we create a new prefab 0:07:19.395,0:07:21.395 asset in our project. 0:07:22.560,0:07:24.560 Before we spread our collectables 0:07:24.560,0:07:26.560 around the game area we should create 0:07:26.560,0:07:28.560 a new game object 0:07:28.560,0:07:30.560 to hold our pickups and 0:07:30.560,0:07:32.560 to help organise our hierarchy. 0:07:33.101,0:07:35.101 Let's create a new game object. 0:07:37.126,0:07:39.126 And call it Pickups. 0:07:41.443,0:07:43.443 Check the transform to make sure our 0:07:43.443,0:07:45.443 Pickups holder object 0:07:45.443,0:07:48.122 is at origin, or (0, 0, 0). 0:07:48.553,0:07:50.553 And if not use the component 0:07:50.553,0:07:52.553 context menu to reset it. 0:07:53.274,0:07:57.259 Next drag our Pickup game object on to it. 0:07:58.077,0:08:00.077 Now we want to spread a 0:08:00.077,0:08:02.077 number of these pickup objects 0:08:02.077,0:08:03.524 around the play area. 0:08:03.524,0:08:05.524 Make sure the pickup game object is 0:08:05.524,0:08:07.524 selected, and not the parent. 0:08:09.008,0:08:11.008 Now let's back out a little 0:08:11.008,0:08:13.008 so we can see the entire game area. 0:08:15.033,0:08:17.033 Click and drag the pickup game object 0:08:17.033,0:08:19.033 to move it in to the 0:08:19.033,0:08:21.033 first pickup position. 0:08:21.033,0:08:23.957 I'm going to place mine at the top of the playing area. 0:08:24.678,0:08:26.678 With the game object still selected 0:08:27.122,0:08:28.555 duplicate it. 0:08:28.555,0:08:30.555 This can be done either by selecting 0:08:30.555,0:08:34.786 Edit - Duplicate, or by using the hot key combination. 0:08:35.369,0:08:37.369 This is command-D on mac, 0:08:37.369,0:08:39.369 or control-D on windows. 0:08:40.895,0:08:42.895 Now let's position the 0:08:42.895,0:08:44.895 second instance of the prefab. 0:08:47.558,0:08:49.558 Using the hot keys we will 0:08:49.558,0:08:51.558 create a few more, placing them 0:08:51.558,0:08:53.558 around the play area. 0:09:08.400,0:09:10.400 Okay, I've created 12. 0:09:11.135,0:09:13.135 Let's hit play and test. 0:09:14.522,0:09:16.522 Excellent, these pickup prefabs 0:09:16.522,0:09:18.118 are working great. 0:09:18.118,0:09:20.118 In the next assignment we'll learn how to 0:09:20.118,0:09:22.524 pick them up and to count them.