{"id":3751,"date":"2017-08-25T14:38:46","date_gmt":"2017-08-25T13:38:46","guid":{"rendered":"http:\/\/www.holyfingers.co.uk\/main\/?p=3751"},"modified":"2020-03-14T16:22:27","modified_gmt":"2020-03-14T16:22:27","slug":"super-smashing-great","status":"publish","type":"post","link":"https:\/\/www.holyfingers.co.uk\/main\/blog\/2017\/08\/25\/super-smashing-great\/","title":{"rendered":"Super, smashing, great&#8230;"},"content":{"rendered":"<p>For the last few days I&#8217;ve been experimenting with smashable things in Unity and thought I&#8217;d share a few notes on how I&#8217;m going about it&#8230;<\/p>\n<p><!--more--><\/p>\n<p>I started off putting a few bits and pieces together in my 3d package; there&#8217;s a keyframe-animated flower, a pot with a simulated shatter effect and a particle system with 1000 rigidbodies for soil.<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/imgur.com\/jT4VvVY\"><img decoding=\"async\" title=\"source: imgur.com\" src=\"https:\/\/i.imgur.com\/jT4VvVY.gif\"><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>A couple of things have to be addressed before recreating this in Unity. Firstly the shatter effect, in this particular instance I was able to bake the animation into each separate piece of pot, I could run the simulation several times until I was happy and then export the pieces with standard animation clips that Unity would understand.<\/p>\n<p>For the soil I used the interior of the pot as an emitter and just increased the number of particles to create a solid looking mass. I then used Polygonizer to &#8220;skin&#8221; the particles and created several frozen meshes at key times from which I could create blend shapes. This meant I could use a fraction of the number of particles in Unity, and use the blend shapes to fill in the space.<\/p>\n<p>I&#8217;ve previously had some difficulty getting animation clips on multiple objects to sync up properly but Unity&#8217;s new timeline seems to work really well. I just dragged the various clips on and used the recorder to set key frames to hide\/reveal different elements on cue and set the blend weights.&nbsp;<\/p>\n<p>As happy as I was with the baked animation, I also wanted to try something a bit more interactive that would respond to player input and react to the environment, so I decided to try and recreate the shatter simulation using Unity&#8217;s physics system. For simplicity I&#8217;ve set aside the pot&#8217;s contents for the moment to focus on just getting the broken pot working.<\/p>\nngg_shortcode_0_placeholder\n<p>The <em>Pot Parent<\/em> is an empty object, set up with a collider and rigidbody components, the&nbsp;child objects are the original solid pot mesh and the multiple, pre-cracked pot-pieces I generated from that mesh, now without their animation clips.&nbsp;<\/p>\n<p><em>Pot_Parent<\/em> has two very simple Playmaker FSMs attached:<\/p>\nngg_shortcode_1_placeholder\n<p>The first detects the mouse axis direction as a vector3 and applies this as a corresponding impulse force to its own rigidbody.<\/p>\nngg_shortcode_2_placeholder\n<p>The second detects a collision, sends a Smash event to all child objects, detaches them and then destroys itself. I&#8217;ve used a Collide Tag to limit collisions to specific objects, this means the <em>Parent_Pot<\/em> can happily sit on the shelf collider without immediately shattering.<\/p>\n<p>The <em>Solid_Pot<\/em>&nbsp;is just a mesh, it has no collider or rigidbody, it simply waits for a <strong>Smash<\/strong> event and destroys itself.<\/p>\n<p>The <em>Pot_bits<\/em> all start off with their mesh-renderers set to inactive, they have rigidbodies and convex mesh colliders, set to kinematic and inactive respectively to begin with. Each individual <em>Pot_Bit<\/em> gets an identical FSM so I&#8217;m using a template:<\/p>\nngg_shortcode_3_placeholder\n<p>Firstly, each part stores its own various components as variables; the rigidbody, the collider and the mesh-renderer, then the <em>Pot Parent<\/em>&#8216;s. The parts then sit, invisibly, waiting for a <em>Smash<\/em> event.<\/p>\n<p>Once the smash event is sent the rigidbody is set to non-kinematic, gravity is enabled and the mesh renderer is turned on. We also get the current velocity and angular-velocity from the parent object and set that locally, you can see the difference that makes here:&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"ngg-singlepic ngg-none\" src=\"https:\/\/www.holyfingers.co.uk\/main\/wp-content\/gallery\/smashing\/Broken_Pot_7.gif\" alt=\"Broken_Pot_7\"><\/p>\n<p>&nbsp;<\/p>\n<p>As ever, if you have any questions, crits or corrections, leave them in the comments below.<\/p>\n<p>You can also follow along on <a href=\"http:\/\/twitter.com\/holyfingers\">Twitter<\/a> and <a href=\"http:\/\/artoftoryanse.tumblr.com\/\">Tumblr<\/a> for more regular updates.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For the last few days I&#8217;ve been experimenting with smashable things in Unity and thought I&#8217;d share a few notes on how I&#8217;m going about it&#8230;<\/p>\n","protected":false},"author":2,"featured_media":3752,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[55,12],"tags":[43,44,75,102,76,57,46],"class_list":["post-3751","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-game-development","category-tutorials","tag-3d","tag-animation","tag-playmaker","tag-timeline","tag-toryanse","tag-unity","tag-wip"],"_links":{"self":[{"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/posts\/3751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/comments?post=3751"}],"version-history":[{"count":44,"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/posts\/3751\/revisions"}],"predecessor-version":[{"id":4040,"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/posts\/3751\/revisions\/4040"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/media\/3752"}],"wp:attachment":[{"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/media?parent=3751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/categories?post=3751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/tags?post=3751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}