{"id":1403,"date":"2020-05-23T10:49:42","date_gmt":"2020-05-23T01:49:42","guid":{"rendered":"https:\/\/www.munchou.com\/biwa-chan\/?p=1403"},"modified":"2020-05-23T10:49:42","modified_gmt":"2020-05-23T01:49:42","slug":"my-first-game-in-unity-part3-when-coding-isnt-innate","status":"publish","type":"post","link":"https:\/\/www.munchou.com\/biwa-chan\/?p=1403","title":{"rendered":"My first game in Unity part3 &#8211; When coding isn&#8217;t innate"},"content":{"rendered":"<p><strong>Hi!<\/strong><\/p>\n<p><strong>Third part of my first steps in Unity.<br \/><\/strong>I guess you&#8217;re here because you want to know what I did to make my car brake? I&#8217;m about to show you my first code after hours (like, a lot of hours) of struggle to figure out how I can do thing and why the holy hell so many things I tried just didn&#8217;t work (although in those cases, I&#8217;m still not sure why).<br \/>Please remember I&#8217;m beginning, and that code is certainly not what you&#8217;d get from a pro.<\/p>\n<p>First of all, here is the complete code (so far), click the picture to get it full:<br \/><a href=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200516a.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-1415\" src=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200516a-654x1024.png\" alt=\"\" width=\"654\" height=\"1024\" srcset=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200516a-654x1024.png 654w, https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200516a-192x300.png 192w, https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200516a-768x1203.png 768w, https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200516a-981x1536.png 981w, https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200516a.png 1003w\" sizes=\"(max-width: 654px) 100vw, 654px\" \/><\/a><\/p>\n<p>Now I&#8217;m going to describe each part the best way I can (hum&#8230;), the way I understood all of that.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1416\" src=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200516b.png\" alt=\"\" width=\"450\" height=\"245\" srcset=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200516b.png 450w, https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200516b-300x163.png 300w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/p>\n<p>These are the functions (functions? &#8211; I don&#8217;t know yet how they are called) that are going to be used.<br \/><em>[SerializeField] private Vector3 _centerOfMass<\/em>: used to show and modify the center of mass (although I haven&#8217;t tried it yet in 2D mode, but it works in 3D physics) in the Inspector.<em><br \/><\/em>(<em>public float<\/em> means that we can change the value in the Inspector.)<br \/><em>public float speed<\/em>: used for the speed of the motor.<br \/><em>public float rotationSpeed<\/em>: used for the rotation of the car when in the air.<br \/><em>public WheelJoint2D<\/em>: used in the Inspector to assign the wheels&#8217; <em>Wheel Joint 2D<\/em> components.<br \/><em>private Rigidbody2D rb<\/em>: used as &#8220;rigid body&#8221;, to control the center of mass and rotation of the car.<br \/><em>private float movement<\/em>: used to assign the up and down-arrow keys, and trigger the movement of the car.<br \/><em>private float rotation<\/em>: used to assign the right and left-arrow keys, and trigger the rotation of the car in the air.<br \/><em>private float brake<\/em>: used to assign the up and down-arrow keys, and trigger the brakes.<br \/><em>private float nospeed<\/em>: used with brakes, to set the motor speed to 0.<br \/><em>private bool moveForward<\/em>: used to check whether the up-arrow key is pressed.<br \/><em>private bool moveBackward<\/em>: used to check whether the down-arrow is pressed.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1419\" src=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200516c.png\" alt=\"\" width=\"490\" height=\"226\" srcset=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200516c.png 490w, https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200516c-300x138.png 300w\" sizes=\"(max-width: 490px) 100vw, 490px\" \/><\/p>\n<p><em>private void Start<\/em><br \/><em>rb = GetComponent&lt;Rigidbody2D&gt;()<\/em>: assigned the component Ridigbody2D (which is the car) to &#8220;rb&#8221;.<br \/><em>rb.centerOfMass = _centerOfMass<\/em>: now that <em>rb<\/em> is understood as the car, we get its center of mass.<br \/><em>void Update()<\/em><br \/><em>movement = -Input.GetAxisRaw(&#8220;Vertical&#8221;) * speed<\/em>: the value of <em>movement<\/em> will be the one of <em>speed<\/em> (that I set to 4000) multiplied by 1 or -1, depending on which arrow key is pressed (up or down, considered as the vertical axis input).<br \/><em>brake = -Input.GetAxisRaw(&#8220;Vertical&#8221;) * nospeed<\/em>: same as <em>movement<\/em>, but <em>nospeed<\/em> is set to 0.1.<br \/><em>rotation = Input.GetAxisRaw(&#8220;Horizontal&#8221;)<\/em>: used to rotate the car when pressing the left\/right arrow keys (hence the &#8220;horizontal&#8221;).<br \/><em>moveForward = (Input.GetKey(KeyCode.UpArrow))<\/em>: set the value of <em>moveForward<\/em> to the up arrow key.<br \/><em>moveBackward = (Input.GetKey(KeyCode.DownArrow))<\/em>: set the value of <em>moveBackward<\/em> to the down arrow key.<\/p>\n<p>Okaaaay, we&#8217;re all set, now time to affect the car&#8217;s behavior!<br \/>First, when to activate the motor to make the car move.<\/p>\n<p><a href=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200523a.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-1424\" src=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200523a-300x97.png\" alt=\"\" width=\"300\" height=\"97\" srcset=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200523a-300x97.png 300w, https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200523a-768x248.png 768w, https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200523a.png 952w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><em>rb.AddTorque(-rotation * rotationSpeed * Time.fixedDeltaTime);<\/em>: the &#8220;formula&#8221; to make the car rotate when pressing right\/left arrow keys.<br \/><em>if (movement == 0f) {frontWheel.useMotor = false;}<\/em>: It means that when we&#8217;re NOT pressing the up\/down arrow keys, the motor is off.<br \/><br \/><\/p>\n<p>After that, it comes to what happens when the up and down arrow keys are pressed. In each, there are 2 possibilities, depending on the speed on the car (<em>velocity<\/em>).<br \/>The problem here is that it&#8217;s for sure <strong>the wrong way to do it<\/strong>, it&#8217;s only a workaround as I have no idea what to use to do it properly.<br \/>Ideally, the conditions should be set on the rotation speed of the front wheels, but so far I haven&#8217;t found anything. Whereas in my case, it&#8217;s the speed of the whole car as an object. Moreover, it&#8217;s very approximate, as you can see, because I set values to the velocity (-1, -0.5, 0.5, 1) that I chose randomly, but it works! As a beginner, I can&#8217;t complain, but I&#8217;m aware I&#8217;ve done it wrong.<\/p>\n<p>So depending on the velocity, the <em>motorSpeed<\/em> changes either to normal (=driving), or to 0 (=braking). Again, it is faaaaar from being good: the motor&#8217;s speed is either at its maximum, or at 0, there is no smooth acceleration nor deceleration. Got to fix that too&#8230;<\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi! Third part of my first steps in Unity.I guess you&#8217;re here because you want to know what I did to make my car brake? I&#8217;m about to show you my first code after hours (like, a lot of hours) of struggle to figure out how I can do thing and why the holy hell [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1405,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[],"_links":{"self":[{"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/posts\/1403"}],"collection":[{"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1403"}],"version-history":[{"count":13,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/posts\/1403\/revisions"}],"predecessor-version":[{"id":1427,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/posts\/1403\/revisions\/1427"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/media\/1405"}],"wp:attachment":[{"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}