{"id":1431,"date":"2020-05-25T16:04:42","date_gmt":"2020-05-25T07:04:42","guid":{"rendered":"https:\/\/www.munchou.com\/biwa-chan\/?p=1431"},"modified":"2020-05-25T16:04:42","modified_gmt":"2020-05-25T07:04:42","slug":"my-first-game-in-unity-part4-slowing-down-the-car-smoothly","status":"publish","type":"post","link":"https:\/\/www.munchou.com\/biwa-chan\/?p=1431","title":{"rendered":"My first game in Unity part4 \u2013 Slowing down the car smoothly"},"content":{"rendered":"<p>Alrightyyyyy!<\/p>\n<p>Each time I post, I learn something new (I mean, prior to posting, or else I&#8217;ll be keeping posting just to get [random?] knowledge, that&#8217;d be awesome!), and my script is getting a little more complex. Actually, this time, we&#8217;re going to create a new script, and it&#8217;s going to be linked to the script attached to the car. How cool is that?<\/p>\n<p>You&#8217;ll have probably noticed I&#8217;ve made a new vehicle (not finished yet). That&#8217;s the one I plan to use for that game: a (small) truck. I won&#8217;t give more details for now. To the script! If you&#8217;ve followed most tutorials, your vehicle must be going quite much, even after releasing the accelerator, as if there was nothing to slow it down. However, motorized vehicles (all?) have what&#8217;s often called &#8220;engine braking&#8221; (NOT &#8220;jake brake&#8221;, that&#8217;s different). If the driver isn&#8217;t accelerating, the vehicle&#8217;s engine will slow the whole down.<br \/><em>Engine braking is braking that uses the resistance of engine rotation.<\/em><br \/>Although it&#8217;s not very true on automatic transmission cars (I didn&#8217;t know that before I moved to Japan, as cars in Europe are generally MT, AT being way more expensive &#8211; the opposite here), but that&#8217;s very noticeable on MT cars and motorcycles (even scooters). You&#8217;ve perhaps seen road signs asking to use engine brakes, especially in mountains.<br \/><br \/><\/p>\n<p>In my case, because I don&#8217;t have enough knowledge about how to affect the motor&#8217;s rotation (if that&#8217;s even possible), I found what&#8217;s called <em>Drag<\/em>.<br \/>https:\/\/docs.unity3d.com\/ScriptReference\/Rigidbody2D-drag.html<br \/>&#8220;<em>Drag<\/em> is the tendency of an object to slow down due to friction with the air or water that surrounds it.&#8221;<br \/>It&#8217;s not the same as engine braking, but that will do for now.<\/p>\n<p><strong>Activated only when NOT pressing a key<\/strong><br \/>If just set without any conditions, drag will constantly apply a force that will go against our vehicle&#8217;s velocity. So I wanted it to be OFF when I was pressing the up\/down arrow keys.<br \/>Here is what I did (I blurred\/cut the unneeded code on purpose):<br \/><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1435\" src=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200525b.png\" alt=\"\" width=\"577\" height=\"536\" srcset=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200525b.png 577w, https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200525b-300x279.png 300w\" sizes=\"(max-width: 577px) 100vw, 577px\" \/><\/p>\n<p>I added only 3 things:<br \/>&#8211; <em>public float dragForce = 0.5f;<\/em> That will appear among the script component to make it easily editable. Note that 0.5 is quite high, I then changed it to 0.2 (and the truck&#8217;s mass to 1200)<br \/>&#8211; In <em>void FixedUpdate()<\/em>, <em>rb.drag = dragForce;<\/em> The drag value of our <em>RigidBody2D<\/em> (<em>rb<\/em>) is set to 0.5 every frame by default.<br \/>&#8211; Then, to bypass the default drag value, I added <em>rb.drag = 0f;<\/em> when both up and down arrow keys are pressed, regardless of the speed.<\/p>\n<p>Testing aaaaand&#8230; it works! Nice! But wait a minute, I just noticed something peculiar while jumping. Yes! The car slows down the same way in the air that it does on the ground! (when I don&#8217;t press any keys)<br \/>Argh!<\/p>\n<p><strong>Why are you slowing down that much in the air?!!<br \/><\/strong>Yes, there comes quite a troublesome issue, especially when an object has a big mass and a relatively low speed (way lower than its <a href=\"https:\/\/en.wikipedia.org\/wiki\/Terminal_velocity\">terminal velocity<\/a>), it is not supposed to be slowed down that much!<br \/>Hm&#8230;let&#8217;s think about it for a few seconds. I want my vehicle to slow down thanks to its engine brake, so the conditions are&#8230; it should be happening when the front wheels are touching the ground AND when I&#8217;m not pressing the up\/down arrow keys.<br \/>So my idea was to create a new wheel collider that will detect if it is touching another specific collider: the ground. Easier said than done&#8230; Although I figured out almost everything by myself, calling stuff from a script to use it in another was hard. Let&#8217;s do it in the right order, shall we?<\/p>\n<p>Step 1:<br \/>First, I created an empty object inside the <em>FrontWheel<\/em> object, so that I get the precise coordinates. I called it <em>GroundDetector<\/em>, and I created 2 components: a script that I named <em>GoundCollisionDetector<\/em> (you name your stuff the way you want, I like when it&#8217;s clear so I write in full) and a <em>Circle Collider 2D<\/em> that has the exact same radius as my wheels colliders, because it is supposed to act like an invisible wheel. I checked <em>Is Trigger<\/em>, because it will be used as a&#8230; trigger, yes.<br \/><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1440\" src=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200525c.png\" alt=\"\" width=\"508\" height=\"684\" srcset=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200525c.png 508w, https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200525c-223x300.png 223w\" sizes=\"(max-width: 508px) 100vw, 508px\" \/><\/p>\n<p>Step 2:<br \/>Because I find assigning colliders to every object supposed to act as the ground tedious, I created an empty object for the ground collider. That way, even if I have to delete a piece of my level, it won&#8217;t delete my actual &#8220;track&#8221; that I can easily edit. So you do as you wish, but it&#8217;s how I did, it looks more logical to me (for now). And to this object that I called <em>Ground_Collision<\/em>, I created and assigned a new tag I named <em>Ground<\/em>.<\/p>\n<p>Step 3:<br \/>Time to get to the script! It&#8217;s a straightforward script, I created a <em>boolean<\/em> (a condition that is either true or false), and if the whatever that collides with the wheel has the <em>Ground<\/em> tag, the boolean <em>GroundCollision<\/em> is true.<br \/><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1442\" src=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200525d.png\" alt=\"\" width=\"529\" height=\"441\" srcset=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200525d.png 529w, https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200525d-300x250.png 300w\" sizes=\"(max-width: 529px) 100vw, 529px\" \/>What is in green is another way to interact: instead of using a tag, one can use the name of an object. The thing is if I do so, I&#8217;ll have to edit the script every time (I think?) I add something acting as the ground. With a tag, I simply need to assign the tag to any object.<br \/>The Debug.Log\u00a0 is here to check that it is properly working or not (it shows the text in the inspector). So in the end, the clean code should be:<\/p>\n<p><em>public class GroundCollisionDetector : MonoBehaviour{<\/em><br \/><em>public bool GroundCollision;<\/em><br \/><em>public void OnTriggerStay2D (Collider2D col){<\/em><br \/><em>if (col.gameObject.tag == &#8220;Ground&#8221;){<\/em><br \/><em>GroundCollision = true;}<\/em><br \/><em>else if (col.gameObject.tag != &#8220;Ground&#8221;){<\/em><br \/><em>GroundCollision = false;}}}<\/em><\/p>\n<p>That&#8217;s it for the wheel&#8217;s script!<\/p>\n<p>Step 4:<br \/>Now to call that script from the car&#8217;s script. It&#8217;s actually very simple.<br \/>I added:<br \/><em>public GroundCollisionDetector GroundColScript;<br \/><\/em>And I moved the <em>rb.drag = dragForce;<\/em> from the <em>FixedUpdate<\/em> to my new condition: it can only happen if up\/down arrow keys aren&#8217;t pressed, which means if <em>movement == 0f<\/em><br \/>So it became:<br \/><em>if (movement == 0f){<\/em><br \/><em>rearWheel.useMotor = false;<\/em><br \/><em>frontWheel.useMotor = false;<\/em><br \/><em>if (GroundColScript.GroundCollision){<\/em><br \/><em>rb.drag = dragForce;}}<\/em><br \/><br \/><em>if (GroundColScript.GroundCollision)<\/em> is what calls the boolean (<em>GroundCollision<\/em>) from the other script!<br \/><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1443\" src=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200525e.png\" alt=\"\" width=\"560\" height=\"272\" srcset=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200525e.png 560w, https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200525e-300x146.png 300w\" sizes=\"(max-width: 560px) 100vw, 560px\" \/><\/p>\n<p>Step 5:<br \/>Finally, and it&#8217;s a very important step or else it won&#8217;t work, select your vehicle, scroll down to your script component, find <em>Ground Col Script<\/em> (or whatever name you chose) and drag the wheel object in its space.<br \/><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1444\" src=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200525f.png\" alt=\"\" width=\"391\" height=\"256\" srcset=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200525f.png 391w, https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200525f-300x196.png 300w, https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2020\/05\/20200525f-345x225.png 345w\" sizes=\"(max-width: 391px) 100vw, 391px\" \/><\/p>\n<p>Play, et voil\u00e0!<\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Alrightyyyyy! Each time I post, I learn something new (I mean, prior to posting, or else I&#8217;ll be keeping posting just to get [random?] knowledge, that&#8217;d be awesome!), and my script is getting a little more complex. Actually, this time, we&#8217;re going to create a new script, and it&#8217;s going to be linked to the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1432,"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\/1431"}],"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=1431"}],"version-history":[{"count":8,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/posts\/1431\/revisions"}],"predecessor-version":[{"id":1445,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/posts\/1431\/revisions\/1445"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/media\/1432"}],"wp:attachment":[{"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}