{"__v":2,"_id":"554f015d1bc7d20d00458588","category":{"__v":8,"_id":"543b9ef065bf840e00b473e0","project":"543b9b0065bf840e00b473d5","version":"543b9b0065bf840e00b473d8","pages":["543b9f11b1479b1400c42f58","548a78adb77bb70b00ac8c10","55082e75c79a211900a8de1b","55083e4e31eeba2d00d66a2d","550bfa9022ccb01700a79466","550c312a5fdefb19003d1201","554f015d1bc7d20d00458588","554f03de1bc7d20d0045858c"],"reference":false,"createdAt":"2014-10-13T09:44:16.284Z","from_sync":false,"order":4,"slug":"pvp-battles","title":"PvP Battles"},"project":"543b9b0065bf840e00b473d5","user":"543b9aa865bf840e00b473d1","version":{"__v":11,"_id":"543b9b0065bf840e00b473d8","project":"543b9b0065bf840e00b473d5","createdAt":"2014-10-13T09:27:28.467Z","releaseDate":"2014-10-13T09:27:28.467Z","categories":["543b9b0065bf840e00b473d9","543b9ef065bf840e00b473e0","54890012f291f61400c02d36","54890902f291f61400c02d3e","54890c43f291f61400c02d44","54890d71c178b40b00aa3086","5508125c0c4d8c19008a5f83","55094050961f17170070abbd","550945111c38c50d006118ad","550a4c2e42fff40d00ae6049","55221c074801a40d00a77610"],"is_hidden":false,"is_beta":false,"is_stable":false,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"createdAt":"2015-05-10T06:57:33.200Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"required","params":[],"url":""},"order":2,"body":"This page is for those who want to create specific defensive buildings that work like the cannons or mortars from Clash of Clans. Read on if you're not sure where CannonController.cs in instantiated within every single building. \n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://www.filepicker.io/api/file/qWN9jDvZSVKlryraNm9l\",\n        \"cannon.jpg\",\n        \"800\",\n        \"600\",\n        \"#617f96\",\n        \"\"\n      ],\n      \"caption\": \"Defensive cannon demo included in the kit\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Where would I instantiate it within only the cannons?\"\n}\n[/block]\nThe cannons are instantiated in SaveLoadBattle - the method:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"ProcessCannon(int grassIndex)\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Is there an example showing how to make the cannon rotate to point at its target?\"\n}\n[/block]\nOur cannon is currently a 3D demo over the 2D gameboard. To convert to a 2D sprite cannon, this is what you'll need to do:\n\nWe assume that you have the sprites for a rotating cannon (usually they are 3d renders from different angles)\n\nCreate the rotate animations both ways – clockwise and counter-clockwise, fire, etc. Use unity GUI buttons to test the animations – the animations will have to be synchronized with moving parts.You will have to use sprite sheets, because the rotations must be done in place, and it has to be very accurate. \n\nHere is a good tutorial:\n[block:html]\n{\n  \"html\": \"<iframe width=\\\"560\\\" height=\\\"315\\\" src=\\\"https://www.youtube.com/embed/cRE2G96591E?rel=0&amp;showinfo=0\\\" frameborder=\\\"0\\\" allowfullscreen></iframe>\"\n}\n[/block]\nNext:\n\nYou will need a rotor and a sextant(start from our turret as reference), who will compare the angles to an array – use your cannon animation turned at all angles to prepare the angle list. At some point (170 degrees I think) Unity will switch the angle to 0 and negative values, so take this into account, if necessary\n\nOnce the angle is established, the animated cannon must be able to execute a FireAtFrame command - and you will achieve this by adding events to the cannon barrel animation frames, and keeping track of the current frame – the event passes the frame number, and other relevant info – it can be set in the 2DToolkit animation.\n\nOne thing you must pay attention to – once the animated cannon prefab is ready, it must be instantiated in-game – putting a few cannons on the map and launching the game will not work. For some reason, the simplest switching animations in 2DToolkit don't work properly, unless the prefabs are instantiated/loaded in-game. So remember to instantiate the cannon prefab before you use them.","excerpt":"","slug":"building-cannons","type":"basic","title":"Cannons"}

Cannons


This page is for those who want to create specific defensive buildings that work like the cannons or mortars from Clash of Clans. Read on if you're not sure where CannonController.cs in instantiated within every single building. [block:image] { "images": [ { "image": [ "https://www.filepicker.io/api/file/qWN9jDvZSVKlryraNm9l", "cannon.jpg", "800", "600", "#617f96", "" ], "caption": "Defensive cannon demo included in the kit" } ] } [/block] [block:api-header] { "type": "basic", "title": "Where would I instantiate it within only the cannons?" } [/block] The cannons are instantiated in SaveLoadBattle - the method: [block:code] { "codes": [ { "code": "ProcessCannon(int grassIndex)", "language": "csharp" } ] } [/block] [block:api-header] { "type": "basic", "title": "Is there an example showing how to make the cannon rotate to point at its target?" } [/block] Our cannon is currently a 3D demo over the 2D gameboard. To convert to a 2D sprite cannon, this is what you'll need to do: We assume that you have the sprites for a rotating cannon (usually they are 3d renders from different angles) Create the rotate animations both ways – clockwise and counter-clockwise, fire, etc. Use unity GUI buttons to test the animations – the animations will have to be synchronized with moving parts.You will have to use sprite sheets, because the rotations must be done in place, and it has to be very accurate. Here is a good tutorial: [block:html] { "html": "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/cRE2G96591E?rel=0&amp;showinfo=0\" frameborder=\"0\" allowfullscreen></iframe>" } [/block] Next: You will need a rotor and a sextant(start from our turret as reference), who will compare the angles to an array – use your cannon animation turned at all angles to prepare the angle list. At some point (170 degrees I think) Unity will switch the angle to 0 and negative values, so take this into account, if necessary Once the angle is established, the animated cannon must be able to execute a FireAtFrame command - and you will achieve this by adding events to the cannon barrel animation frames, and keeping track of the current frame – the event passes the frame number, and other relevant info – it can be set in the 2DToolkit animation. One thing you must pay attention to – once the animated cannon prefab is ready, it must be instantiated in-game – putting a few cannons on the map and launching the game will not work. For some reason, the simplest switching animations in 2DToolkit don't work properly, unless the prefabs are instantiated/loaded in-game. So remember to instantiate the cannon prefab before you use them.