{"id":2647,"date":"2019-12-17T11:01:42","date_gmt":"2019-12-17T02:01:42","guid":{"rendered":"https:\/\/blog.criware.com\/?p=2647"},"modified":"2019-12-17T12:10:28","modified_gmt":"2019-12-17T03:10:28","slug":"charging-up-your-weapons-in-unity-using-adx2","status":"publish","type":"post","link":"https:\/\/blog.criware.com\/index.php\/2019\/12\/17\/charging-up-your-weapons-in-unity-using-adx2\/","title":{"rendered":"Charging up Your Weapons in Unity using ADX2"},"content":{"rendered":"<h2 style=\"font-size: 150%; font-weight: bold; margin-top: 40px;\">Introduction<\/h2>\n<p>Modern games use all sorts of weapon types to offer up a fun and engaging gameplay. From single shot weapons that fire once per trigger, to continuously firing weapons like flamethrowers. Another popular firing mechanic is the charged shot. For this, the player must hold the trigger for an amount of time, after which, a charged-up shot will fire. This usually does more damage and might even come with some added visual flair. This quick blog will walk through setting this up.<\/p>\n<p><!-- video and download start --><\/p>\n<div style=\"max-width: 800px; margin: 0 auto; margin-bottom: 20px;\">\n<div style=\"width: 800px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-2647-1\" width=\"800\" height=\"450\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/blog.criware.com\/wp-content\/uploads\/2019\/12\/1912_ChargedShot.mp4?_=1\" \/><a href=\"https:\/\/blog.criware.com\/wp-content\/uploads\/2019\/12\/1912_ChargedShot.mp4\">https:\/\/blog.criware.com\/wp-content\/uploads\/2019\/12\/1912_ChargedShot.mp4<\/a><\/video><\/div>\n<\/div>\n<p><!-- download start --><\/p>\n<div style=\"max-width: 800px; margin: 0 auto; margin-bottom: 20px; text-align:center;\">\n<a style=\"display: inline-block; border: 1px solid #ccc; padding: 20px;\" href=\"https:\/\/blog.criware.com\/wp-content\/uploads\/2019\/12\/1912_ChargedShot_AtomCraftProject.zip\"><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1813\" style=\"display: inline-block;\" src=\"https:\/\/blog.criware.com\/wp-content\/uploads\/2018\/06\/zip.png\" alt=\"zip\" width=\"80\" height=\"78\" style=\"    max-width: 900px;\n    display: block;\n    margin: 0 auto;\"><br \/>\n1912_ChargedShot _AtomCraftProject.zip<\/a>\n<\/div>\n<p><!-- video and download end --><\/p>\n<p><em>This project was created using CRIWARE SDK for Unity V2.99.00 and Unity V2019.2.10f1 <\/em><\/p>\n<h2 style=\"font-size: 150%; font-weight: bold; margin-top: 40px;\">Atom Craft<\/h2>\n<p>We need:<\/p>\n<ul>\n<li>Rename AisacControl_00 to \u201cAISAC_ChargedShot\u201d<\/li>\n<li>Create a Polyphonic Cue named \u201cCue_Charging\u201d\n<ul>\n<li>Import corresponding Material onto new Track<\/li>\n<\/ul>\n<\/li>\n<li>Create a Polyphonic Cue named \u201cCue_Shot\u201d\n<ul>\n<li>Import 3 Materials of increasing intensity to separate Tracks<\/li>\n<li>Add an AISAC to each Track set to the following:\n<ul>\n<li>ASAIC Name \u2013 \u201cdyn1\u201d, \u201cdyn2\u201d, \u201cdyn3\u201d respectively<\/li>\n<li>AISAC Control \u2013 AISAC_ChargedShot<\/li>\n<li>AISAC Graph Type \u2013 Volume<\/li>\n<\/ul>\n<\/li>\n<li>Finesse curves to create a natural transition<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.criware.com\/wp-content\/uploads\/2019\/12\/23-1-1.png\" alt=\"23-1\" width=\"1401\" height=\"294\" class=\"alignnone size-full wp-image-2654\" srcset=\"https:\/\/blog.criware.com\/wp-content\/uploads\/2019\/12\/23-1-1.png 1401w, https:\/\/blog.criware.com\/wp-content\/uploads\/2019\/12\/23-1-1-300x63.png 300w, https:\/\/blog.criware.com\/wp-content\/uploads\/2019\/12\/23-1-1-768x161.png 768w, https:\/\/blog.criware.com\/wp-content\/uploads\/2019\/12\/23-1-1-1024x215.png 1024w\" sizes=\"auto, (max-width: 1401px) 100vw, 1401px\" \/><\/p>\n<p>This is all will need to prototype our charged shot system. We can build our project now, making sure to generate the necessary Unity Assets.<\/p>\n<h2 style=\"font-size: 150%; font-weight: bold; margin-top: 40px;\">Unity<\/h2>\n<p>To create our charged shot, we will need to start a counter when input is received, and then calculate how much time has passed since then. We can then use this to scale our audio and even our visual effects. The length of time is arbitrary and will depend on the gameplay design. Since we\u2019re only designing the sound, we can decide on any amount of Charge Time, so let\u2019s go with 1.5 seconds (the sound will need to be designed to fit this length).<\/p>\n<pre><code class=\"hljs php\">using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\npublic class PowerShot : MonoBehaviour\r\n{\r\n    public ParticleSystem particles;\r\n    public float chargeTime = 1.5f;\r\n    private float counter;\r\n    bool isCharging = false;\r\n\r\n    CriAtomSource source;\r\n\r\n    void Start()\r\n    {\r\n        source = gameObject.GetComponent<CriAtomSource>();\r\n    }\r\n\r\n    \/\/ Update is called once per frame\r\n    void Update()\r\n    {\r\n        if (Input.GetKeyDown(\"space\"))\r\n        {\r\n            isCharging = true;\r\n            source.Play(\"Cue_Charging\");\r\n            counter = Time.time;\r\n        }\r\n\r\n\r\n        if (Input.GetKeyUp(\"space\") || (Time.time - counter) > chargeTime)\r\n            if (isCharging)\r\n            {\r\n                isCharging = false;\r\n                float chargeAmount = Time.time - counter;\r\n\r\n                \/\/ Dynamic Audio\r\n                source.SetAisacControl(\"AISAC_ChargedShot\", chargeAmount \/ chargeTime);\r\n                source.Stop();\r\n                source.Play(\"Cue_Shot\");\r\n\r\n                \/\/ Dynamic Particles\r\n                var main = particles.main;\r\n                main.startSpeedMultiplier = chargeAmount * 60;\r\n                var light = particles.lights;\r\n                light.intensityMultiplier = chargeAmount \/ chargeTime;\r\n                particles.Play();\r\n            }\r\n    }\r\n}\r\n<\/code>\r\n<\/pre>\n<p>As we can see, there isn\u2019t much code required to control our audio. To facilitate our goals, the script above is doing the following:<\/p>\n<p>First, it checks if the \u201cspace\u201d key is down. If it is, it:<\/p>\n<ol>\n<li>Sets the Boolean state of <em>isCharging to true<\/em><\/li>\n<li>Plays the appropriate Cue<\/li>\n<li>Resets the <em>counter<\/em> variable<\/li>\n<\/ol>\n<p>The script then checks if the \u201cspace\u201d key is released, or if our counter has expired (chargeTime). We then check that it\u2019s true that our gun <em>isCharging<\/em>; otherwise, the code will trigger a \u201cCue_Shot\u201d Cue (even though we might not be charging anything at that time). If all the above conditions are met, our script then:<\/p>\n<ol>\n<li>Sets the Boolean state of <em>isCharging <\/em>to false<\/li>\n<li>Calculates how much time has passed since \u201cspace\u201d was pressed<\/li>\n<li>Sets our AISAC_ChargedShot value relative to how long we\u2019ve charged our shot<\/li>\n<li>Stops the \u201cCue_Charging\u201d Cue<\/li>\n<li>Plays the \u201cCue_Shot\u201d Cue<\/li>\n<\/ol>\n<p>The latter part of the code is some fun visual manipulation that changes the light intensity and overall speed of some particles in the Scene based on the elapsed charge time. Playing around with visuals can help to give a better-contextualised sense of the appropriateness of the sound. Overall, ADX2 makes it easy to create these sorts of responsive user-controlled audio systems.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Modern games use all sorts of weapon types to offer up a fun and engaging gameplay. From single shot<\/p>\n","protected":false},"author":2,"featured_media":2650,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[5,7],"tags":[],"class_list":["post-2647","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-adx","category-tutorials"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/posts\/2647","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/comments?post=2647"}],"version-history":[{"count":10,"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/posts\/2647\/revisions"}],"predecessor-version":[{"id":2684,"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/posts\/2647\/revisions\/2684"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/media\/2650"}],"wp:attachment":[{"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/media?parent=2647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/categories?post=2647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/tags?post=2647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}