{"assets":[],"author_link":"author\/trialbyfun\/","author_name":"trialbyfun","cat":"LD #21","categories":["LD #21"],"comments":[],"epoch":1314246720,"event":"LD21","likes":12,"metadata":{"p_key":"77457","p_author":"trialbyfun","p_authorkey":"0","p_urlkey":"286308","p_title":"Making of \u201cMountain above the clouds\u201d \u2013 Part 1 \u2013 Collision","p_cat":"LD #21","p_event":"LD21","p_time":"1314246720","p_likes":"12","p_comments":"0","p_status":"WAYBACK","us_key":null,"us_name":null,"us_username":null,"event_start":"1313712000","event_key":"7","event_name":"LD21"},"source_url":"2011\/08\/25\/making-of-mountain-above-the-clouds-part-1-collision\/","text":"<p>Hi LD48ers,<\/p>\n<p>I\u2019ve had a couple of people ask me about how I put together my 2D game in unity and seeing as the techniques might be relevant in other games I thought I\u2019d take some time to explain some of the things i did in my game, in the hopes that it helps someone out.<\/p>\n<p>like my <a href=\"http:\/\/www.ludumdare.com\/compo\/ludum-dare-20\/?action=preview&amp;uid=4202\">LD20<\/a> game <a href=\"http:\/\/www.ludumdare.com\/compo\/ludum-dare-20\/?action=preview&amp;uid=4202\">Drum Hammer Dig<\/a> I chose not to use any external sprite libraries (such as ex2D or SpriteManager), everything game related was written from scratch and there\u2019s of course a link to the source in my compo entry should you want to check it out. ( As you\u2019d expect its all over the place but it got the job done ;))<\/p>\n<p><a href=\"http:\/\/www.ludumdare.com\/compo\/ludum-dare-21\/?action=rate&amp;uid=4202\">http:\/\/www.ludumdare.com\/compo\/ludum-dare-21\/?action=rate&amp;uid=4202<\/a><\/p>\n<p>To start with, i\u2019ll talk about how I handled landscape collision.<\/p>\n<p>One thing I knew early on was that the level shapes themselves were probably going to change a lot as I put the game together.\u00a0Collision wise I also knew that this game would be 2D, but I didn\u2019t have the time ( or skill ) to write a custom 2d collision system.<\/p>\n<div class=\"wp-caption alignnone\" style=\"width: 410px\"><img alt=\"\" height=\"395\" src=\"http:\/\/www.trialbyfun.com\/LD21\/LudumDate_21_TrialByFun\/HowTo\/levels.jpg\" width=\"400\"\/><p class=\"wp-caption-text\">Editor shot of the 9 areas<\/p><\/div>\n<p>With that in mind I went with the \u201cwork with what you have\u201d approach. In this case, Unity fortunatly has a great built in 3D Physics system that i wanted to utilise.<\/p>\n<p>I wanted was to harness that system, in a way that allowed me to quickly create and layout collision without relying on an external 3d package, I also didn\u2019t want to mess around placing lots of box colliders down for each level and then having to move or change them when the level changed.<\/p>\n<p>I chose to write a simple mesh creator in the editor, and then used the generated geometry as a mesh collider. This gave me the best of both worlds. Fast turn around, and all the physics collisions I needed.<\/p>\n<p>Its dirt simple but really effective;<\/p>\n<p>1) First I have an array of points in space ( arranged in a clockwise order )<\/p>\n<div class=\"wp-caption alignnone\" style=\"width: 410px\"><img alt=\"\" height=\"300\" src=\"http:\/\/www.trialbyfun.com\/LD21\/LudumDate_21_TrialByFun\/HowTo\/pointsA.jpg\" width=\"400\"\/><p class=\"wp-caption-text\">Points in space<\/p><\/div>\n<p>2 ) A set of Vertices is created from these points, extending forward and backwards on the z axis.<\/p>\n<div class=\"wp-caption alignnone\" style=\"width: 410px\"><img alt=\"\" height=\"300\" src=\"http:\/\/www.trialbyfun.com\/LD21\/LudumDate_21_TrialByFun\/HowTo\/pointsB.jpg\" width=\"400\"\/><p class=\"wp-caption-text\">Vertex positions based on previous points<\/p><\/div>\n<p>3) triangles are created from the vertices<\/p>\n<div class=\"wp-caption alignnone\" style=\"width: 410px\"><img alt=\"\" height=\"300\" src=\"http:\/\/www.trialbyfun.com\/LD21\/LudumDate_21_TrialByFun\/HowTo\/pointsC.jpg\" width=\"400\"\/><p class=\"wp-caption-text\">Triangles are created from the vertices<\/p><\/div>\n<p>Translated into unity, I have a\u00a0GameObject for each area in the game which has a \u201cMeshHitcheck\u201d component that contains array of positions (laid out in a clockwise direction)<\/p>\n<p>Using the steps above a collider mesh is generated at runtime from those points.<\/p>\n<div class=\"wp-caption alignnone\" style=\"width: 410px\"><img alt=\"\" height=\"248\" src=\"http:\/\/www.trialbyfun.com\/LD21\/LudumDate_21_TrialByFun\/HowTo\/Img1.jpg\" width=\"400\"\/><p class=\"wp-caption-text\">Background painting on a single sprite<\/p><\/div>\n<div class=\"wp-caption alignnone\" style=\"width: 410px\"><img alt=\"\" height=\"248\" src=\"http:\/\/www.trialbyfun.com\/LD21\/LudumDate_21_TrialByFun\/HowTo\/Img2.jpg\" width=\"400\"\/><p class=\"wp-caption-text\">Clockwise layout of points with Gizmo draw in editor to show edges<\/p><\/div>\n<div class=\"wp-caption alignnone\" style=\"width: 410px\"><img alt=\"\" height=\"248\" src=\"http:\/\/www.trialbyfun.com\/LD21\/LudumDate_21_TrialByFun\/HowTo\/Img3.jpg\" width=\"400\"\/><p class=\"wp-caption-text\">Hitcheck mesh generated at startup<\/p><\/div>\n<p>2D Collision Done!<\/p>\n<div class=\"wp-caption alignnone\" style=\"width: 410px\"><img alt=\"\" height=\"290\" src=\"http:\/\/www.trialbyfun.com\/LD21\/LudumDate_21_TrialByFun\/HowTo\/result.jpg\" width=\"400\"\/><p class=\"wp-caption-text\">Same area shot from in game<\/p><\/div>\n<p>\u00a0<\/p>\n<p><strong>Editor Extra\u2019s<\/strong><\/p>\n<p>Hand editing position values in an array isn\u2019t exactly fast so to make editing and layout of the positions array as quick as possible, I created quick little editor helper script that basically draws position handles for each point, and a button to add new points into the array. Its not a perfect solution and there a number of ways to improve it, but it saved me plenty of precious time.<\/p>\n<div class=\"wp-caption alignnone\" style=\"width: 410px\"><img alt=\"\" height=\"248\" src=\"http:\/\/www.trialbyfun.com\/LD21\/LudumDate_21_TrialByFun\/HowTo\/Img4.jpg\" width=\"400\"\/><p class=\"wp-caption-text\">Quick editing of points inside the editor<\/p><\/div>\n<p>I should also note that the seed character in game is simply a rigid-body sphere with a limit on the z axis, all other objects are sphere or box trigger colliders that don\u2019t move.<\/p>\n<p>That\u2019s all for today, I hope this was of some use.<br\/>\nIf you have any questions or want explanations on some of the other things in game, let me know.<\/p>\n<p>Thanks for reading.<\/p>\n<p>Steven Burgess<\/p>\n<p>\u00a0<\/p>","time":"August 25th, 2011 4:32 am","title":"Making of \u201cMountain above the clouds\u201d \u2013 Part 1 \u2013 Collision","title_was_empty":false}