{"id":27,"date":"2023-05-17T13:24:34","date_gmt":"2023-05-17T17:24:34","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/53-353\/m2023\/?p=27"},"modified":"2023-05-17T12:24:48","modified_gmt":"2023-05-17T16:24:48","slug":"day-3-code-basics-hard-pong","status":"publish","type":"post","link":"https:\/\/courses.ideate.cmu.edu\/53-353\/m2023\/?p=27","title":{"rendered":"Day 3: Code Basics &#038; Hard Pong"},"content":{"rendered":"\n<p>In class today we covered some of the basic rules and concepts of code, such as how to declare a variable, the importance of type, and why you should never confuse = (assignment) with == (is equal to).<\/p>\n\n\n\n<p>Next we started out making our own version of Pong. We used Unity primitives to create a playing field, changed the color of the playing field with a material, and set the camera position to give us a top down view.<\/p>\n\n\n\n<p>(Slides and Zipped project from today&#8217;s session will be available on the course Box folder)<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Setting up the Objects<\/h2>\n\n\n\n<p>The first thing we need is a field of play.&nbsp; We decided on a rectangle that is 100 units wide by 50 units high.&nbsp; We build this by creating a plane by selecting&nbsp;<strong>GameObject &gt; 3D Object &gt; Plane<\/strong>.&nbsp; Make sure our plane is at the origin by clicking the gear icon in the upper right corner of the Transform component and select&nbsp;<strong>Reset<\/strong>.&nbsp; This will set your plane\u2019s (x,y,z) position to (0,0,0), reset all rotations to 0, and the scale to 1.&nbsp; Now set your plane\u2019s scale to X:100 | Y:1 | Z: 50, and the position to X:0 | Y:-1 | Z: 0.  (This -1 value ensures that the walls do not intersect with the plane, as the pivot point of Unity cube is at its center.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/courses.ideate.cmu.edu\/53-353\/m2020\/wp-content\/uploads\/2020\/05\/image.png\" alt=\"This image has an empty alt attribute; its file name is image.png\"\/><figcaption class=\"wp-element-caption\">The TRANSFORM component from the Inspector, with the final settings for the plane<\/figcaption><\/figure><\/div>\n\n\n<p><strong>Note:&nbsp; <\/strong>Unity plane objects are 10 units by 10 units at their natural scale, so we only need to scale our x to 10 and our z to 5.<\/p>\n\n\n\n<p>Next we create a wall by adding a Cube (<strong>GameObject &gt; 3D Object &gt; Cube<\/strong>) and setting the scale to X: 100 | Y: 2 | Z: 2. We move this wall to the side (a position of X:-26 | Y: 0 | Z: 0 should work) duplicate it, and place the copy at the other side wall. Do the same for the shorter walls, scaling your cubes to fit.<\/p>\n\n\n\n<p>Set your camera\u2019s&nbsp;<strong>Position<\/strong>&nbsp;to (0, 35, 0), and&nbsp;<strong>Rotation<\/strong>&nbsp;to (90, 0, 0).&nbsp; Adjust theheight until you can see the entire board in your Game view.&nbsp;<\/p>\n\n\n\n<p>At this point, the Plane and the Walls are becoming difficult to discern from one another, as everything is currently a bright white color. Let&#8217;s change this.<\/p>\n\n\n\n<p>We are going to create a new materials. You can do this by right-clicking your mouse in the <strong>Asset<\/strong> window and selecting <strong>Create &gt; Material<\/strong>. You will see a new material pop up in the Asset window. Give this a name, and select it. In the Inspector, look for the <strong>Albedo <\/strong>setting, and click the color swatch next to it. This will bring up a color picker that lets you choose a setting. Next, change the <strong>Smoothness<\/strong> setting to 0. This will give our material a matte finish. Assign the material to the Plane by clicking and dragging the icon from the Asset window onto the Plane. (You will see objects temporarily assuming this material when the mouse is over them, indicating which object you will affect.<\/p>\n\n\n\n<p>Next, we need a ball.&nbsp; Create a sphere (<strong>GameObject &gt; 3D Object &gt; Sphere<\/strong>).&nbsp; Reset the transformation to put it at the origin, and set the XYZ scale to 3.<\/p>\n\n\n\n<p><strong>Note:&nbsp; <\/strong>In Unity, the \u201cY\u201d axis moves up\/down.&nbsp; This differs from many modeling programs that treat \u201cZ\u201d as the up axis.&nbsp; \u201cX\u201d is still left to right, so if you want to make something move on the ground, as we do here, you are moving along the XZ plane.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"925\" height=\"447\" src=\"https:\/\/courses.ideate.cmu.edu\/53-353\/m2023\/wp-content\/uploads\/2022\/05\/image.png\" alt=\"\" class=\"wp-image-30\" srcset=\"https:\/\/courses.ideate.cmu.edu\/53-353\/m2023\/wp-content\/uploads\/2022\/05\/image.png 925w, https:\/\/courses.ideate.cmu.edu\/53-353\/m2023\/wp-content\/uploads\/2022\/05\/image-300x145.png 300w, https:\/\/courses.ideate.cmu.edu\/53-353\/m2023\/wp-content\/uploads\/2022\/05\/image-768x371.png 768w\" sizes=\"(max-width: 925px) 100vw, 925px\" \/><\/figure>\n\n\n\n<p>Tomorrow we will start making things move, and then on Friday we will make them bounce! <\/p>\n","protected":false},"excerpt":{"rendered":"<p><a class=\"more-link\"  href=\"https:\/\/courses.ideate.cmu.edu\/53-353\/m2023\/?p=27\"><span class=\"more-text\"><\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"greenlet_layout":[]},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/courses.ideate.cmu.edu\/53-353\/m2023\/index.php?rest_route=\/wp\/v2\/posts\/27"}],"collection":[{"href":"https:\/\/courses.ideate.cmu.edu\/53-353\/m2023\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courses.ideate.cmu.edu\/53-353\/m2023\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/53-353\/m2023\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/53-353\/m2023\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=27"}],"version-history":[{"count":4,"href":"https:\/\/courses.ideate.cmu.edu\/53-353\/m2023\/index.php?rest_route=\/wp\/v2\/posts\/27\/revisions"}],"predecessor-version":[{"id":154,"href":"https:\/\/courses.ideate.cmu.edu\/53-353\/m2023\/index.php?rest_route=\/wp\/v2\/posts\/27\/revisions\/154"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/53-353\/m2023\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/53-353\/m2023\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/53-353\/m2023\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}