{"id":2643,"date":"2013-08-13T13:53:59","date_gmt":"2013-08-13T12:53:59","guid":{"rendered":"http:\/\/t-machine.org\/?p=2643"},"modified":"2013-10-06T10:04:27","modified_gmt":"2013-10-06T09:04:27","slug":"the-perfect-jump-testing-techniques-for-jumping-in-unity3d","status":"publish","type":"post","link":"http:\/\/new.t-machine.org\/index.php\/2013\/08\/13\/the-perfect-jump-testing-techniques-for-jumping-in-unity3d\/","title":{"rendered":"The perfect jump: testing techniques for Jumping in Unity3D"},"content":{"rendered":"<p>In a physics-based engine, there&#8217;s many ways to make a character &#8220;jump&#8221;. For my <a href=\"http:\/\/7dfps.com\">7-day FPS<\/a> entry, I&#8217;m trying to work out the perfect jump for my gameplay. Easier said than done. I&#8217;m struggling&#8230;<\/p>\n<p>Before I go looking for feedback and suggestions, I thought it would help to make a demo showing the things I&#8217;ve been trying-out.<\/p>\n<blockquote><p>\n<a href=\"http:\/\/t-machine.org\/web\/WebPlayer-jumptest\/WebPlayer.html\">Unity webplayer link to try the examples below<\/a><\/p>\n<p>Instructions: wait till you land on skyscraper, then hit a number key to select a jump type, and get jumping (spacebar).<\/p>\n<p>Note: if you fall, no problem, it&#8217;ll auto-respawn you after a couple of seconds of falling\n<\/p><\/blockquote>\n<p><!--more--><\/p>\n<h2>Option 1: Increase rigidbody.velocity, up<\/h2>\n<p>[csharp]<br \/>\ngameObject.rididBody.velocity += new Vector3(0,10,0);<br \/>\n[\/csharp]<\/p>\n<p>Pros:<\/p>\n<ol>\n<li>Trivial &#8211; 1 line of code\n<li>Compatible with force-based movement\n<\/ol>\n<p>Cons:<\/p>\n<ol>\n<li>Sometimes doesn&#8217;t work with position-based movement (updating position instead of updating rigidbody velocity)\n<li>Very little control over what happens (you apply the velocity, but very hard to predict where it lands, or how soon, etc)\n<\/ol>\n<h2>Option 2: Increase rigidbody.velocity, forwards<\/h2>\n<p>[csharp]<br \/>\ngameObject.rididBody.velocity += new Vector3(0,10,0) + (3.0f * gameObject.transform.forward);<br \/>\n[\/csharp]<\/p>\n<p>Pros:<\/p>\n<ol>\n<li>Easy &#8211; one longer line of code\n<li>More realistic: you &#8220;leap&#8221; in the direction you&#8217;re looking. Tilt head up when jumping to jump higher, tilt head more forwards to jump further\n<li>WYLIWYL (&#8220;willy-will&#8221;? &#8220;wiley-while&#8221;?): Where You Look Is Where You Leap\n<li>Allows a variety of jump heights and lengths, with only one button: high obstacles you have to step close to and jump UP; long gaps you have to run towards and jump FORWARDS\n<\/ol>\n<p>Cons:<\/p>\n<ol>\n<li>Not always obvious to the player what&#8217;s going to happen (&#8220;don&#8217;t look down when you jump!&#8221;)\n<li>Provides a speed-boost forwards, encouraging &#8220;bunny-leaping&#8221; (like FPS bunny hops, but less stupid)\n<li>Much harder for the player to target their landing-position (sideways: slightly hard, distance: very hard)\n<\/ol>\n<h2>Option 3: Impulse up (i.e. a one-off Force \/ Acceleration)<\/h2>\n<p>[csharp]<br \/>\ngameObject.rididBody.AddForce( new Vector3(0,10,0), ForceMode.VelocityChange );<\/p>\n<p>\/\/ &#8230; or:<\/p>\n<p>gameObject.rididBody.AddForce( new Vector3(0,10 * gameObject.rigidBody.mass,0), ForceMode.Impulse);<br \/>\n[\/csharp]<\/p>\n<p>Pros:<\/p>\n<ol>\n<li>Easy &#8211; one (long) line of code\n<li>Slightly more predictable than increasing velocity, maybe &#8211; only because you know it&#8217;s working in real-world physical units (add a force of (0,9.81,0) to make a character &#8220;hover&#8221; in mid-air)\n<\/ol>\n<p>Cons:<\/p>\n<ol>\n<li>As far as I can tell, this is identical to increasing the velocity, but uses more typing?\n<li>&#8230;but requires slightly more code\n<\/ol>\n<h2>Option 4: Impulse forwards (i.e. a one-off Force \/ Acceleration)<\/h2>\n<p>[csharp]<br \/>\ngameObject.rididBody.AddForce( 10.0f * gameObject.transform.forward, ForceMode.VelocityChange);<\/p>\n<p>\/\/ &#8230; or:<\/p>\n<p>gameObject.rididBody.AddForce( 10.0f * gameObject.transform.forward * gameObject.rigidBody.mass, ForceMode.Impulse);<br \/>\n[\/csharp]<\/p>\n<p>Cons:<\/p>\n<ol>\n<li>Again, as far as I can tell &#8230; identical to setting velocity forwards\/up, but requires more code?\n<\/ol>\n<h2>Option 5: Constant force (short)<\/h2>\n<p>[csharp]<\/p>\n<p>\/\/ secondsLeft initially to 0.5<\/p>\n<p>void FixedUpdate()<br \/>\n{<br \/>\n   while( secondsLeft &gt; 0 )<br \/>\n   {<br \/>\n      secondsLeft -= Time.deltaTime;<br \/>\n      player.gameObject.rigidBody.AddForce( new Vector(0,10,0), ForceMode.Acceleration );<\/p>\n<p>\/\/ OR: &#8230;<\/p>\n<p>      player.gameObject.rigidBody.AddForce( new Vector(0,10 * player.gameObject.rigidBody.mass,0), ForceMode.Force );<br \/>\n   }<br \/>\n}<br \/>\n[\/csharp]<\/p>\n<p>Pros:<\/p>\n<ol>\n<li>Player can target the landing-point better\n<li>Feels like a jetpack (it IS a jetpack! just an invisible one)\n<li>Slow take off, but long hang-time\n<\/ol>\n<p>Cons:<\/p>\n<ol>\n<\/ol>\n<h2>Option 6: Constant force (long)<\/h2>\n<p>[csharp]<\/p>\n<p>\/\/ secondsLeft initially to 2.0, force is reduced to make up<\/p>\n<p>void FixedUpdate()<br \/>\n{<br \/>\n   while( secondsLeft &gt; 0 )<br \/>\n   {<br \/>\n      secondsLeft -= Time.deltaTime;<br \/>\n      player.gameObject.rigidBody.AddForce( new Vector(0,7,0), ForceMode.Acceleration );<\/p>\n<p>\/\/ OR: &#8230;<\/p>\n<p>      player.gameObject.rigidBody.AddForce( new Vector(0,7 * player.gameObject.rigidBody.mass,0), ForceMode.Force );<br \/>\n   }<br \/>\n}<br \/>\n[\/csharp]<\/p>\n<p>Pros:<\/p>\n<ol>\n<li>Player can target the landing-point better\n<li>Feels like a jetpack (it IS a jetpack! just an invisible one)\n<li>Even more floaty \/ long hangtime than previous option\n<\/ol>\n<p>Cons:<\/p>\n<ol>\n<\/ol>\n<h2>Option 7: Impulse up AND forwards<\/h2>\n<p>[csharp]<br \/>\ngameObject.rididBody.AddForce( 10.0f * gameObject.transform.forward + new Vector3(0,4,0), ForceMode.VelocityChange);<\/p>\n<p>[\/csharp]<\/p>\n<p>Pros:<\/p>\n<ol>\n<li>Unlike Impulse-forwards, you can jump up without always glancing at the sky first\n<li>Inherits the &#8220;height control&#8221; aspect of Impulse Forwards\n<\/ol>\n<p>Cons:<\/p>\n<ol>Bunny-leaping is even easier &#8211; just point forwards and keep spamming the space-bar\n<\/ol>\n<h2>Option A: Trajectory arc<\/h2>\n<p>(not implemented yet)<\/p>\n<p>Pros:<\/p>\n<ol>\n<li>Player can precisely target the landing-point\n<li>Precisely re-windable, pausable, speed-up\/slow-down (bullet time) &#8211; (because it&#8217;s parametric)\n<\/ol>\n<p>Cons:<\/p>\n<ol>\n<li>No mid-air steering for players\n<li>Code is much more complex: requires a whole GUI\/HUD system for targetting, on top of the calculations for the jump\n<li>Code is highly dependent on other code: any &#8220;external&#8221; force or collision that happens while in-air has to be dealt with as special case\n<\/ol>\n<h2>Option B: The FPS Controller<\/h2>\n<p>(not implemented yet)<\/p>\n<p>Pros:<\/p>\n<ol>\n<li>Built-in to Unity\n<li>Less debugging\n<\/ol>\n<p>Cons:<\/p>\n<ol>\n<li>Lots of code, quite complex, not easy to customize\n<li>Only has one model of jumping &#8211; works, but depending on your game might be unrealistic  and not fun\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>In a physics-based engine, there&#8217;s many ways to make a character &#8220;jump&#8221;. For my 7-day FPS entry, I&#8217;m trying to work out the perfect jump for my gameplay. Easier said than done. I&#8217;m struggling&#8230; Before I go looking for feedback and suggestions, I thought it would help to make a demo showing the things I&#8217;ve [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70,9],"tags":[],"_links":{"self":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts\/2643"}],"collection":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/comments?post=2643"}],"version-history":[{"count":5,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts\/2643\/revisions"}],"predecessor-version":[{"id":3037,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts\/2643\/revisions\/3037"}],"wp:attachment":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/media?parent=2643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/categories?post=2643"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/tags?post=2643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}