{"id":1332,"date":"2020-01-15T06:51:14","date_gmt":"2020-01-15T06:51:14","guid":{"rendered":"https:\/\/www.danielparente.net\/en\/2020\/01\/15\/acceleration-in-c-unity-game-development-tutorial-game-development-blog\/"},"modified":"2020-01-15T06:51:14","modified_gmt":"2020-01-15T06:51:14","slug":"acceleration-in-c-unity-game-development-tutorial-game-development-blog","status":"publish","type":"post","link":"https:\/\/www.danielparente.net\/en\/2020\/01\/15\/acceleration-in-c-unity-game-development-tutorial-game-development-blog\/","title":{"rendered":"Acceleration in C# \u2013 Unity Game Development Tutorial \u2013 Game Development Blog"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div>\n<p>\u201cAcceleration is pressing his eyeballs out of their right shape. High tech astigmatism.\u201d<br \/>\u2015\u00a0<strong>James S.A. Corey,\u00a0<\/strong><a href=\"https:\/\/www.goodreads.com\/work\/quotes\/45783442\" target=\"_blank\" rel=\"noopener\">Drive<\/a><\/p>\n<p><span id=\"more-1053\"\/><\/p>\n<blockquote class=\"wp-block-coblocks-click-to-tweet\">\n<p class=\"wp-block-coblocks-click-to-tweet__text\">In this free tutorial by @MammothCompany: learn fundamental C# skills to implement acceleration in a Unity game!<\/p>\n<p><a class=\"wp-block-coblocks-click-to-tweet__twitter-btn\" href=\"http:\/\/twitter.com\/share?&amp;text=In%20this%20free%20tutorial%20by%20%40MammothCompany%3A%20learn%20fundamental%20C%23%20skills%20to%20implement%20acceleration%20in%20a%20Unity%20game!&amp;url=http:\/\/www.gamedevelopmentblog.com\/2019\/12\/acceleration-in-c-sharp-unity-game-development-tutorial\/\" target=\"_blank\" rel=\"noopener noreferrer\">Tweet<\/a><\/p><\/blockquote>\n<p>In this tutorial:<\/p>\n<blockquote class=\"wp-block-quote is-style-default\">\n<p>Acceleration is a term used to define the change in velocity. In other words, acceleration is how quickly an object speeds up or slows down. <\/p>\n<\/blockquote>\n<h2 id=\"setup\">Part 1: Setup<\/h2>\n<p>To follow along with this tutorial, you\u2019re going to need a Unity game open with a player and a velocity. If you don\u2019t have this yet, use our previous tutorial to set up exactly that!<\/p>\n<h2 id=\"define\">Part 2: Defining Acceleration with C#<\/h2>\n<p>In your Player script, Player.cs, create a float variable named acceleration. This variable will represent the player\u2019s acceleration. <\/p>\n<p>Give the variable the initial value 2.5f.\u00a0<\/p>\n<p><code>public float acceleration = 2.5f;<\/code><\/p>\n<p>With this code, we have implemented that, with each second of the game, the player\u2019s speed will increase by 2.5 until it reaches a maximum speed of 4, the value of movementSpeed. <\/p>\n<h2 id=\"speed\">Part 3: Defining Speed<\/h2>\n<p>Next, declare and initialize a private float variable named speed. Assign this variable to contain Player\u2019s initial speed, 0.<\/p>\n<p>In the update method, define the velocity on the X axis to equal the value of the speed variable.<\/p>\n<p><code>void Update () {<br \/>GetComponent&lt;Rigidbody&gt; ().velocity = new Vector3(<\/code><br \/><code><strong>speed<\/strong>,<br \/>GetComponent&lt;Rigidbody&gt; ().velocity.y,<br \/>GetComponent&lt;Rigidbody&gt; ().velocity.z<\/code><br \/><code>);<br \/>}<\/code><\/p>\n<p>Let\u2019s test the acceleration and speed variables we just created! <\/p>\n<p>Save the script, and open Unity. Press Play. <\/p>\n<p>The cube will fall. It will not move to the right because its speed is currently 0.<\/p>\n<p>Let\u2019s fix that! <\/p>\n<h2 id=\"change\">Part 4: Using Acceleration to Change Speed<\/h2>\n<p>We will use acceleration to increase speed until speed\u2019s value reaches movementSpeed. In Update, use the following code to add the value of acceleration to speed every second of the game.<\/p>\n<p><code>void Update () {<\/code><br \/><strong><code>speed = speed + acceleration;<br \/><\/code><\/strong><code><br \/>GetComponent&lt;Rigidbody&gt; ().velocity = new Vector3(<\/code><br \/><code>speed,<br \/>GetComponent&lt;Rigidbody&gt; ().velocity.y,<br \/>GetComponent&lt;Rigidbody&gt; ().velocity.z<\/code><br \/><code>);<br \/>}<\/code><\/p>\n<p>Multiply acceleration by Time.deltaTime so that the speed changes at the same rate regardless of the processing speed of your user\u2019s device.<\/p>\n<p><code>speed = speed + acceleration <\/code><strong><code>* Time.deltaTime;<\/code><\/strong><\/p>\n<h2 id=\"if\">Part 5: Using If Statements<\/h2>\n<p>Next we need to ensure that speed will never be greater than movementSpeed. Otherwise, the acceleration will keep increasing the speed to infinity.<\/p>\n<p>You can use an if block to implement a condition.<\/p>\n<p>An if block runs code if a certain condition passes. Use the following format to set up an if block in the Update method.<\/p>\n<p><code>speed = speed + acceleration <\/code><strong><code>* <\/code><\/strong><code>Time.deltaTime;<\/code><br \/><strong><code><br \/>if () {<\/code><\/strong><\/p>\n<p>}<\/p>\n<p>The if block\u2019s condition goes in between the parentheses. If the condition passes, the code between the curly brackets gets executed. <\/p>\n<p>For our game, the code that needs to be executed is that speed should be limited to movementSpeed. Set this up within the curly brackets of the if block.<\/p>\n<p>WIthin the parentheses of hte if block, set up the condition required for that code to run. For our game, the condition is when speed surpasses movementSpeed.<\/p>\n<p>In our case, the condition is that With the following code, when speed surpasses movementSpeed, set speed to equal movementSpeed.<\/p>\n<p><strong><code>if (speed &gt; movementSpeed) {<br \/>speed = movementSpeed;<br \/>}<\/code><\/strong><\/p>\n<h2 id=\"test\">Part 6: Testing Acceleration<\/h2>\n<p>Let\u2019s test this code to see if it is functioning properly! <\/p>\n<p>Save the script, and open Unity. Press Play. The player will move slowly at first, but its speed will increase. Press Stop. Feel free to change the value of Acceleration to a different value, such as 4.\u00a0<\/p>\n<p>Note: You can increase the X Scale value of Plane so that the player does not fall off the floor.<\/p>\n<h2>Review<\/h2>\n<p><em>\u2014Team Mammoth from Mammoth Interactive INC. Tutorial by Glauco Pires and Transcribing by Alexandra Kropova<\/em><\/p>\n<h2>More resources on this topic:<\/h2>\n<\/div>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><br \/>\n<br \/>[ad_2]<br \/>\n<br \/><a href=\"http:\/\/www.gamedevelopmentblog.com\/2019\/12\/acceleration-in-c-sharp-unity-game-development-tutorial\/\" target=\"_blank\" rel=\"noopener\">Source link <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] \u201cAcceleration is pressing his eyeballs out of their right shape. High tech astigmatism.\u201d\u2015\u00a0James S.A. Corey,\u00a0Drive In this free tutorial by @MammothCompany: learn fundamental C# skills to implement acceleration in a Unity game! Tweet In this tutorial: Acceleration is a term used to define the change in velocity. In other words, acceleration is how quickly [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_post_was_ever_published":false},"categories":[18],"tags":[],"class_list":["post-1332","post","type-post","status-publish","format-standard","hentry","category-gamedev"],"blocksy_meta":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2TFCd-lu","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/posts\/1332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/comments?post=1332"}],"version-history":[{"count":0,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/posts\/1332\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/media?parent=1332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/categories?post=1332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/tags?post=1332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}