{"id":15722,"date":"2022-10-12T18:07:01","date_gmt":"2022-10-12T12:37:01","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/"},"modified":"2022-10-12T18:07:01","modified_gmt":"2022-10-12T12:37:01","slug":"solved-unity-doublejump-in-c","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/","title":{"rendered":"[Solved] Unity Doublejump in C#"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-26105485\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"26105485\" data-parentid=\"26104822\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Haven&#8217;t tested your code, but the following should work:<\/p>\n<pre><code>using UnityEngine;\n\npublic class PlatformerCharacter2D : MonoBehaviour \n{\n    bool facingRight = true;                            \/\/ For determining which way the player is currently facing.\n\n    [SerializeField] float maxSpeed = 10f;              \/\/ The fastest the player can travel in the x axis.\n    [SerializeField] float jumpForce = 400f;            \/\/ Amount of force added when the player jumps. \n\n    [Range(0, 1)]\n    [SerializeField] float crouchSpeed = .36f;          \/\/ Amount of maxSpeed applied to crouching movement. 1 = 100%\n\n    [SerializeField] bool airControl = false;           \/\/ Whether or not a player can steer while jumping;\n    [SerializeField] LayerMask whatIsGround;            \/\/ A mask determining what is ground to the character\n\n    Transform groundCheck;                              \/\/ A position marking where to check if the player is grounded.\n    float groundedRadius = .2f;                         \/\/ Radius of the overlap circle to determine if grounded\n    bool grounded = false;                              \/\/ Whether or not the player is grounded.\n    Transform ceilingCheck;                             \/\/ A position marking where to check for ceilings\n    float ceilingRadius = .01f;                         \/\/ Radius of the overlap circle to determine if the player can stand up\n    Animator anim;                                      \/\/ Reference to the player's animator component.\n\n    int maxNumberOfAirJumps = 1;                        \/\/ Number of times the player can jump in the air\n    int numberOfAirJumpsLeft = 0;\n\n\n    void Awake()\n    {\n        \/\/ Setting up references.\n        groundCheck = transform.Find(\"GroundCheck\");\n        ceilingCheck = transform.Find(\"CeilingCheck\");\n        anim = GetComponent&lt;Animator&gt;();\n    }\n\n\n    void FixedUpdate()\n    {\n        \/\/ The player is grounded if a circlecast to the groundcheck position hits anything designated as ground\n        grounded = Physics2D.OverlapCircle(groundCheck.position, groundedRadius, whatIsGround);\n        anim.SetBool(\"Ground\", grounded);\n\n        \/\/ Set the vertical animation\n        anim.SetFloat(\"vSpeed\", rigidbody2D.velocity.y);\n    }\n\n\n    public void Move(float move, bool crouch, bool jump)\n    {    \n        \/\/ If crouching, check to see if the character can stand up\n        if(!crouch &amp;&amp; anim.GetBool(\"Crouch\"))\n        {\n            \/\/ If the character has a ceiling preventing them from standing up, keep them crouching\n            if( Physics2D.OverlapCircle(ceilingCheck.position, ceilingRadius, whatIsGround))\n                crouch = true;\n        }\n\n        \/\/ Set whether or not the character is crouching in the animator\n        anim.SetBool(\"Crouch\", crouch);\n\n        \/\/only control the player if grounded or airControl is turned on\n        if(grounded || airControl)\n        {\n            \/\/ Reduce the speed if crouching by the crouchSpeed multiplier\n            move = (crouch ? move * crouchSpeed : move);\n\n            \/\/ The Speed animator parameter is set to the absolute value of the horizontal input.\n            anim.SetFloat(\"Speed\", Mathf.Abs(move));\n\n            \/\/ Move the character\n            rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);\n\n            \/\/ If the input is moving the player right and the player is facing left...\n            if(move &gt; 0 &amp;&amp; !facingRight)\n                \/\/ ... flip the player.\n                Flip();\n            \/\/ Otherwise if the input is moving the player left and the player is facing right...\n            else if(move &lt; 0 &amp;&amp; facingRight)\n                \/\/ ... flip the player.\n                Flip();\n        }\n\n        \/\/ If the player should jump...\n        if (grounded &amp;&amp; jump) {\n            \/\/ Add a vertical force to the player.\n            anim.SetBool (\"Ground\", false);\n            rigidbody2D.AddForce (new Vector2 (0f, jumpForce));\n            numberOfAirJumpsLeft = maxNumberOfAirJumps; \/\/Reset the air jumps when the player jumps\n        }\n        else if (!grounded &amp;&amp; jump &amp;&amp; numberOfAirJumpsLeft  &gt; 0)\n        {\n            rigidbody2D.AddForce (new Vector2 (0f, jumpForce));\n            numberOfAirJumpsLeft--; \/\/One less jump \n        }\n    }\n\n\n    void Flip ()\n    {\n        \/\/ Switch the way the player is labelled as facing.\n        facingRight = !facingRight;\n\n        \/\/ Multiply the player's x local scale by -1.\n        Vector3 theScale = transform.localScale;\n        theScale.x *= -1;\n        transform.localScale = theScale;\n    }\n}\n<\/code><\/pre>\n<p>also all the <code>[SerializeField]<\/code> are probably unnessacery<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Unity Doublejump in C# <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Haven&#8217;t tested your code, but the following should work: using UnityEngine; public class PlatformerCharacter2D : MonoBehaviour { bool facingRight = true; \/\/ For determining which way the player is currently facing. [SerializeField] float maxSpeed = 10f; \/\/ The fastest the player can travel in the x axis. [SerializeField] float jumpForce = 400f; \/\/ Amount &#8230; <a title=\"[Solved] Unity Doublejump in C#\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/\" aria-label=\"More on [Solved] Unity Doublejump in C#\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,538],"class_list":["post-15722","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-unity3d"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Unity Doublejump in C# - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Unity Doublejump in C# - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Haven&#8217;t tested your code, but the following should work: using UnityEngine; public class PlatformerCharacter2D : MonoBehaviour { bool facingRight = true; \/\/ For determining which way the player is currently facing. [SerializeField] float maxSpeed = 10f; \/\/ The fastest the player can travel in the x axis. [SerializeField] float jumpForce = 400f; \/\/ Amount ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-12T12:37:01+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Unity Doublejump in C#\",\"datePublished\":\"2022-10-12T12:37:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/\"},\"wordCount\":28,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"unity3d\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/\",\"name\":\"[Solved] Unity Doublejump in C# - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-12T12:37:01+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Unity Doublejump in C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Unity Doublejump in C# - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Unity Doublejump in C# - JassWeb","og_description":"[ad_1] Haven&#8217;t tested your code, but the following should work: using UnityEngine; public class PlatformerCharacter2D : MonoBehaviour { bool facingRight = true; \/\/ For determining which way the player is currently facing. [SerializeField] float maxSpeed = 10f; \/\/ The fastest the player can travel in the x axis. [SerializeField] float jumpForce = 400f; \/\/ Amount ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/","og_site_name":"JassWeb","article_published_time":"2022-10-12T12:37:01+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Unity Doublejump in C#","datePublished":"2022-10-12T12:37:01+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/"},"wordCount":28,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","unity3d"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/","url":"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/","name":"[Solved] Unity Doublejump in C# - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-12T12:37:01+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-unity-doublejump-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Unity Doublejump in C#"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/15722","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=15722"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/15722\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=15722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=15722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=15722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}