<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[Ondrej Paska Blog]]></provider_name><provider_url><![CDATA[https://fishtrone.wordpress.com]]></provider_url><author_name><![CDATA[Ondrej Paska]]></author_name><author_url><![CDATA[https://fishtrone.wordpress.com/author/randalfien/]]></author_url><title><![CDATA[Swarm of Fireflies in&nbsp;Unity]]></title><type><![CDATA[link]]></type><html><![CDATA[<p>I&#8217;d like to show you how to code the movement of an organic looking swarm of flies in Unity.</p>
<p>The result looks like this:</p>
<p><img loading="lazy" data-attachment-id="63" data-permalink="https://fishtrone.wordpress.com/2020/03/11/swarm-of-fireflies-in-unity/roj1/" data-orig-file="https://fishtrone.files.wordpress.com/2020/03/roj1.gif" data-orig-size="565,375" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="roj1" data-image-description="" data-image-caption="" data-medium-file="https://fishtrone.files.wordpress.com/2020/03/roj1.gif?w=300" data-large-file="https://fishtrone.files.wordpress.com/2020/03/roj1.gif?w=565" class="alignnone size-full wp-image-63" src="https://fishtrone.files.wordpress.com/2020/03/roj1.gif?w=565&#038;h=375" alt="roj1" width="565" height="375" /></p>
<p>Let&#8217;s start simple. We can make one fly move in a circle. First we need to define the Radius and Speed, then we make use of the Sine and Cosine functions (check <a href="https://twitter.com/AlanZucconi/status/861562001012731904?s=20" target="_blank" rel="noopener">here</a> if you need a refresher).<!-- HTML generated using hilite.me --></p>
<div style="background:#ffffff;overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin:0;line-height:125%;"><span style="color:#008800;font-weight:bold;">private</span> <span style="color:#333399;font-weight:bold;">float</span> t;
<span style="color:#008800;font-weight:bold;">public</span> <span style="color:#333399;font-weight:bold;">float</span> Radius;
<span style="color:#008800;font-weight:bold;">public</span> <span style="color:#333399;font-weight:bold;">float</span> Speed;
<span style="color:#888888;">// Update is called once per frame</span>
<span style="color:#008800;font-weight:bold;">void</span> <span style="color:#0066bb;font-weight:bold;">Update</span>()
{
    t += Time.deltaTime;
    transform.position = <span style="color:#008800;font-weight:bold;">new</span> Vector3(
       Radius * Mathf.Cos(t*Speed), 
       Radius * Mathf.Sin(t*Speed),
       <span style="color:#6600ee;font-weight:bold;">0</span>);
</pre>
</div>
<p>The result is very regular circular movement.</p>
<p><img loading="lazy" data-attachment-id="66" data-permalink="https://fishtrone.wordpress.com/2020/03/11/swarm-of-fireflies-in-unity/circle/" data-orig-file="https://fishtrone.files.wordpress.com/2020/03/circle.gif" data-orig-size="450,261" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="circle" data-image-description="" data-image-caption="" data-medium-file="https://fishtrone.files.wordpress.com/2020/03/circle.gif?w=300" data-large-file="https://fishtrone.files.wordpress.com/2020/03/circle.gif?w=450" class="alignnone size-full wp-image-66" src="https://fishtrone.files.wordpress.com/2020/03/circle.gif?w=450&#038;h=261" alt="circle" width="450" height="261" /></p>
<p>If you try putting a different radius for X then for Y, you will notice the circle becomes an ellipse.</p>
<p>What if we kept the radius for X steady and changed the radius for Y with an interesting periodic function?</p>
<p>First I used the great <a href="https://www.desmos.com/calculator">Desmos </a> graphics calculator to play around with different functions, until I found one that looks natural.</p>
<p><img loading="lazy" data-attachment-id="67" data-permalink="https://fishtrone.wordpress.com/2020/03/11/swarm-of-fireflies-in-unity/image-7/" data-orig-file="https://fishtrone.files.wordpress.com/2020/03/image-7.png" data-orig-size="1088,408" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="image (7)" data-image-description="" data-image-caption="" data-medium-file="https://fishtrone.files.wordpress.com/2020/03/image-7.png?w=300" data-large-file="https://fishtrone.files.wordpress.com/2020/03/image-7.png?w=1024" class="alignnone size-full wp-image-67" src="https://fishtrone.files.wordpress.com/2020/03/image-7.png?w=1088&#038;h=408" alt="image (7)" width="1088" height="408" srcset="https://fishtrone.files.wordpress.com/2020/03/image-7.png 1088w, https://fishtrone.files.wordpress.com/2020/03/image-7.png?w=150&amp;h=56 150w, https://fishtrone.files.wordpress.com/2020/03/image-7.png?w=300&amp;h=113 300w, https://fishtrone.files.wordpress.com/2020/03/image-7.png?w=768&amp;h=288 768w, https://fishtrone.files.wordpress.com/2020/03/image-7.png?w=1024&amp;h=384 1024w" sizes="(max-width: 1088px) 100vw, 1088px" /></p>
<p>Plugging in the formula for radius Y  as a fraction of Radius:</p>
<div style="background:#ffffff;overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin:0;line-height:125%;"><span style="color:#333399;font-weight:bold;">var</span> radiusY = Radius *
                      (<span style="color:#6600ee;font-weight:bold;">0.5f</span> + <span style="color:#6600ee;font-weight:bold;">0.5f</span> * (
                           Mathf.Sin(t * <span style="color:#6600ee;font-weight:bold;">0.3f</span>) +
                           <span style="color:#6600ee;font-weight:bold;">0.3</span> * Mathf.Sin(<span style="color:#6600ee;font-weight:bold;">2</span> * t + <span style="color:#6600ee;font-weight:bold;">0.8f</span>) +
                           <span style="color:#6600ee;font-weight:bold;">0.26</span> * Mathf.Sin(<span style="color:#6600ee;font-weight:bold;">3</span> * t + <span style="color:#6600ee;font-weight:bold;">0.8f</span>)));
        
transform.position = <span style="color:#008800;font-weight:bold;">new</span> Vector3(Radius*Mathf.Cos(t*Speed), radiusY*Mathf.Sin(t*Speed), <span style="color:#6600ee;font-weight:bold;">0</span>);
</pre>
</div>
<p>&nbsp;</p>
<p>and the result (with the path traced with a gizmo) looks very natural to me.</p>
<p><img loading="lazy" data-attachment-id="69" data-permalink="https://fishtrone.wordpress.com/2020/03/11/swarm-of-fireflies-in-unity/single/" data-orig-file="https://fishtrone.files.wordpress.com/2020/03/single.gif" data-orig-size="347,217" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="single" data-image-description="" data-image-caption="" data-medium-file="https://fishtrone.files.wordpress.com/2020/03/single.gif?w=300" data-large-file="https://fishtrone.files.wordpress.com/2020/03/single.gif?w=347" class="alignnone size-full wp-image-69" src="https://fishtrone.files.wordpress.com/2020/03/single.gif?w=347&#038;h=217" alt="single" width="347" height="217" /></p>
<p>The rest is just spawning more flies and make their radius and speed random. Also negative speed can make some of them fly in the other direction.</p>
<p><img loading="lazy" data-attachment-id="70" data-permalink="https://fishtrone.wordpress.com/2020/03/11/swarm-of-fireflies-in-unity/ten/" data-orig-file="https://fishtrone.files.wordpress.com/2020/03/ten.gif" data-orig-size="350,250" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="ten" data-image-description="" data-image-caption="" data-medium-file="https://fishtrone.files.wordpress.com/2020/03/ten.gif?w=300" data-large-file="https://fishtrone.files.wordpress.com/2020/03/ten.gif?w=350" class="alignnone size-full wp-image-70" src="https://fishtrone.files.wordpress.com/2020/03/ten.gif?w=350&#038;h=250" alt="ten" width="350" height="250" /></p>
]]></html><thumbnail_url><![CDATA[https://fishtrone.files.wordpress.com/2020/03/ten.gif?fit=440%2C330]]></thumbnail_url><thumbnail_width><![CDATA[350]]></thumbnail_width><thumbnail_height><![CDATA[250]]></thumbnail_height></oembed>