<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tiny Tim Games &#187; Unity</title>
	<atom:link href="http://www.tinytimgames.com/tag/unity/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tinytimgames.com</link>
	<description>Just For Fun</description>
	<lastBuildDate>Thu, 02 Feb 2012 18:41:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Unity Plugins and UIApplicationDidFinishLaunchingNotifcation</title>
		<link>http://www.tinytimgames.com/2011/09/01/unity-plugins-and-uiapplicationdidfinishlaunchingnotifcation/</link>
		<comments>http://www.tinytimgames.com/2011/09/01/unity-plugins-and-uiapplicationdidfinishlaunchingnotifcation/#comments</comments>
		<pubDate>Thu, 01 Sep 2011 21:15:36 +0000</pubDate>
		<dc:creator>Jerrod Putman</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[iDevBlogADay]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://www.tinytimgames.com/?p=826</guid>
		<description><![CDATA[I&#8217;m taking a break from my Cocos2D-related posts for this #iDevBlogADay entry. With that said, while this post will be talking specifically about creating Unity plugins, the knowledge can still be used to aid in creating standalone plugins for Cocos2D as well. So you&#8217;re writing your Unity plugin to do some cool native functionality, but [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m taking a break from my <a title="Cocos2D and ARC" href="http://www.tinytimgames.com/2011/07/22/cocos2d-and-arc/" target="_blank">Cocos2D</a>-<a title="Introducing CCKit" href="http://www.tinytimgames.com/2011/08/05/introducing-cckit/" target="_blank">related</a> <a title="Extending CCNode" href="http://www.tinytimgames.com/2011/08/19/extending-ccnode/" target="_blank">posts</a> for this <a title="#iDevBlogADay" href="http://idevblogaday.com" target="_blank">#iDevBlogADay</a> entry. With that said, while this post will be talking specifically about creating Unity plugins, the knowledge can still be used to aid in creating standalone plugins for Cocos2D as well.</p>
<p>So you&#8217;re writing your Unity plugin to do some cool native functionality, but you really need it to do some work right when the app launches. There are usually two ways to solve this problem: 1) Create an &#8220;init&#8221; function that you call from Unity script when your first scene is loaded, or worse 2) modify <em>application:didFinishLaunchingWithOptions:</em> directly. Technically, there&#8217;s a third option too, involving creating a category of the AppController delegate and &#8220;overriding&#8221; <em>application:didFinishLaunchingWithOptions:</em>. The OpenFeint Unity plugin uses this method, actually, but as I mentioned in a previous post, this has some serious drawbacks, especially if you want to use the technique more than once (and yes, I realize I&#8217;m the one who originally wrote that plugin&#8230; I didn&#8217;t know!).</p>
<p>I&#8217;d like to introduce you to the <em>UIApplicationDidFinishLaunchingNotification</em> key, something which has become a good friend of mine.</p>
<p><span id="more-826"></span>It always bugged me that the <em>UIApplicationDidFinishLaunchingNotification</em> key existed. I couldn&#8217;t possibly think of a use for it. In my mind, the first place that any application code could ever run was in <em>application:didFinishLaunchingWithOptions:</em> (or its precursor, <em>applicationDidFinishLaunching:</em>), and so the key was not necessary. I assumed it was just some thing require by the OS that we weren&#8217;t really supposed to use.</p>
<p>But one day, I decided to actually investigate the uses of this key instead of basing my assumptions on pure speculation. I found the <em>NSObject</em> class method <em>load</em>. Suddenly, the key started to make sense.</p>
<p>The documentation for <em>load</em> says that the method is &#8220;invoked whenever a class or category is added to the Objective-C runtime; implement this method to perform class-specific behavior upon loading.&#8221; Essentially, what this means is that any code inside your class&#8217;s <em>load</em> class method will be executed <strong>before</strong> <em>application:didFinishLaunchingWithOptions:</em>. If you want to write any startup code, this seems like it would be the place to do it. But there is a caveat: Other classes may not have loaded themselves yet, so it&#8217;s not safe to do too much in this method.</p>
<p>Instead, you should add your class as an observer of the <em>UIApplicationDidFinishLaunchingNotification</em> key with an action method to do any initialization you want. This will allow all classes to be fully loaded, but still allow for initialization code to be run without having to have either an initialization function in Unity script or by modifying AppController&#8217;s <em>application:didFinishLaunchingWithOptions:</em>.</p>
<p>Here&#8217;s an example:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyCoolPlugin <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Class stuff.</span>
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">static</span> MyCoolPlugin <span style="color: #002200;">*</span>_sharedInstance <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
<span style="color: #a61390;">@implementation</span> MyCoolPlugin
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>load
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>createMyCoolPlugin<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span>
        name<span style="color: #002200;">:</span>UIApplicationDidFinishLaunchingNotification object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>createMyCoolPlugin<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification
<span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// This code will be called immediately after application:didFinishLaunchingWithOptions:.</span>
    <span style="color: #11740a; font-style: italic;">// You could use this to create an instance of your plugin class, like so:</span>
    _sharedInstance <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyCoolPlugin alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Your plugin methods.</span>
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// C functions to access your plugin from Unity script.</span></pre></div></div>

<p>Not only can you use <em>UIApplicationDidFinishLaunchingNotification</em>, but you can also observe keys like <em>UIApplicationDidEnterBackgroundNotification</em>. Essentially, you can plug into most of <em>UIApplicationDelegate</em>&#8216;s methods without having to modify Unity&#8217;s AppController. And naturally, if you&#8217;re creating standalone Objective-C libraries, this method is essentially required unless you want your users to modify their app delegate.</p>
<p>That&#8217;s all for this week. For my next post in two weeks, we should have some very exciting news to share about a project we&#8217;ve been working on!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tinytimgames.com/2011/09/01/unity-plugins-and-uiapplicationdidfinishlaunchingnotifcation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Diversion</title>
		<link>http://www.tinytimgames.com/2011/06/28/diversion/</link>
		<comments>http://www.tinytimgames.com/2011/06/28/diversion/#comments</comments>
		<pubDate>Tue, 28 Jun 2011 19:13:38 +0000</pubDate>
		<dc:creator>Jerrod Putman</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Sheepstacker]]></category>
		<category><![CDATA[Software Updates]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://www.tinytimgames.com/?p=815</guid>
		<description><![CDATA[So I decided to take a little time off from developing Sol Defender for a while to get a few other &#8220;quick&#8221; projects finished. The first of these projects was a prototype that I&#8217;ve been wanting to do for some time. I think the prototype was a success, and we&#8217;ll be exploring a full version [...]]]></description>
			<content:encoded><![CDATA[<p>So I decided to take a little time off from developing Sol Defender for a while to get a few other &#8220;quick&#8221; projects finished. The first of these projects was a prototype that I&#8217;ve been wanting to do for some time. I think the prototype was a success, and we&#8217;ll be exploring a full version of it in the future. But at the moment, we have far too many half-finished projects to start yet another one.</p>
<p>The other project I wanted to pursue was updating our existing games. It&#8217;s been a long time since we&#8217;ve pushed out any updates, and our games seriously needed them. As you may have seen <a title="Sheepstacker 1.4 Now Available!" href="http://www.tinytimgames.com/2011/06/27/sheepstacker-1-4-now-available/">from our news section</a>, we managed to release an updated version of Sheepstacker. I&#8217;m going to go into a bit of what changed and what we had to do to get this update out the door.</p>
<h3><span id="more-815"></span>Embracing UIKit</h3>
<p>For both Sheepstacker and Word Monkey, we used a custom-built UI solution that used Unity&#8217;s built-in GUITexture and GUIText types. While I had gotten very good at manipulating it, it was still a pain to work with and not at all flexible. For version 1.4 of Sheepstacker, I decided to do something crazy and completely rewrite the UI (all of it) completely in UIKit.</p>
<p>The first step in this process was downloading and importing <a title="Prime31 Unity plugins" href="http://www.prime31.com/unity/" target="_blank">Prime31</a>&#8216;s excellent <a title="Prime31's NativeToolkit plugin" href="http://u3d.as/content/prime31-studios/native-toolkit-unity-to-i-os-bridge/1vj" target="_blank">NativeToolkit plugin</a>, which provided a simple interface for showing UIKit view controllers whenever necessary. I had to make some modifications to the Objective-C code, however, as I wanted to have a static background view that would stick around through navigation transitions (i.e., a &#8220;paused&#8221; image and dark background are always visible while the user is paused, even if he goes to the options screen).</p>
<p>Creating the screens in UIKit was a breeze, because I was able to use Interface Builder to lay everything out. Retina display support was handled automatically (I just had to supply an @2x version of each image). And the iPad UI was dead simple to implement as well, as I simply created separate XIBs and used the same classes to power them.</p>
<p>On the whole, I enjoyed the process, but it too much longer than I had anticipated it would. This mostly had to do with the fact that we already had a fully functional game set up in a particular way that was now having all of its UI rewritten. In fact, all of the old UI was removed. The only thing that remains is in the in-game HUD, as it would have been too much of a performance hit to have that handled in UIKit (would have to send the game state to native code each frame, which is a bit expensive to do).</p>
<h3>Game Center and the End of AGON</h3>
<p>Game Center was implemented through the use of another Prime31 plugin. For the most part, this process was straightforward, with the exception of having to serialize scores and achievements when there&#8217;s a network error.</p>
<p>My favorite new feature in the update is the display of your friends&#8217; high scores when you finish a game. I saw this feature implemented in Retro Dreamer&#8217;s excellent <a title="Velocispider" href="http://retrodreamer.com/velocispider.htm" target="_blank">Velocispider</a> and though to myself, &#8220;This needs to be in Sheepstacker!&#8221; It really motivates you to keep playing the game when you see that your friend has a higher score than you, instead of some random username.</p>
<p>Another reason for pushing out this update now is that the AGON Online leaderboards and achievements platform is scheduled to shutdown on June 30th. While the AGON team gave us plenty of time to do something about this, what they didn&#8217;t provide was an updated library that worked with the latest version of Unity and Xcode. Unfortunately, this meant that we were unable to save any of our users scores or unlocked achievements.</p>
<p>While I&#8217;m personally deeply regretful about this, the fact is that we actually did make a few significant gameplay changes that might have made resetting the leaderboards a necessity anyways.</p>
<h3>Gameplay Tweaks</h3>
<p>The fact that the sheep spew forth nickels when you combo stack is really just a joke between my wife and myself. I asked what should spew out when a combo started (thinking of sparkles or something), and my wife said plainly, &#8220;Nickels.&#8221; And that was the end of that.</p>
<p>What I didn&#8217;t do a good job of was incorporating the nickels into the actual gameplay. The nickels were originally supposed to be another score type, as it was more indicative of how well you stacked instead of how high you stacked. However, most players were simply confused by their presence and referred to their stack height as their score. Even I did this.</p>
<p>In the new version, I wanted to make the nickels actually important to gameplay. We&#8217;d had this odd game rule where the user would get an extra heart every 50 sheep stacked (plus one at 25). In 1.4, that rule has been removed and extra hearts are now earned with every 250 nickels. This means that a player&#8217;s stack quality directly affects how high he can stack, because the better his stack, the more nickels he has, which means more hearts, which means more chances to keep stacking. I&#8217;m a bit irritated that I didn&#8217;t come up with the idea two years ago when we first released the game as it seems so obvious now. Nickels also add extra time to Speed Stack, again directly affecting gameplay.</p>
<p>The other tweak we made was reducing the maximum heart count from four to three. We had three hearts initially when we were developing the game, but felt that the game was too hard for newer players. After seeing more players play the game, I came to realize that the extra heart didn&#8217;t actually help that much. In fact, it seemed to have the opposite effect as players wouldn&#8217;t play as carefully as they did when there were only three hearts. The change just &#8220;feels&#8221; right. That&#8217;s really all there is to it.</p>
<h3>Now What?</h3>
<p>So Sheepstacker 1.4 is out, and I hope everyone is enjoying it. Unless necessary, the game probably won&#8217;t see another update until iOS 5 releases later this year (as we want to update the game to take advantage of some of the new Game Center features).</p>
<p>But what&#8217;s next? Back to Sol Defender? Not quite yet. We have an opportunity to be on the leading edge of iOS gaming. It&#8217;s an interesting opportunity, and it may not pan out, but we&#8217;ve got to try it. After we&#8217;re finished with that, I&#8217;ll be returning to Sol Defender, as it&#8217;s a game I&#8217;ve been wanting to make since the App Store launched.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tinytimgames.com/2011/06/28/diversion/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Shockwave</title>
		<link>http://www.tinytimgames.com/2011/04/24/shockwave/</link>
		<comments>http://www.tinytimgames.com/2011/04/24/shockwave/#comments</comments>
		<pubDate>Mon, 25 Apr 2011 06:50:01 +0000</pubDate>
		<dc:creator>Jerrod Putman</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Sol Defender]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://www.tinytimgames.com/?p=791</guid>
		<description><![CDATA[First of all, I&#8217;ve settled on a name for the game that has so far been called &#8220;Sol Defender&#8221;. You&#8217;ll hear more about that later for the official unveiling. Since my last post, a lot of work has been done in a variety of areas, but I&#8217;m only going to focus on one for this [...]]]></description>
			<content:encoded><![CDATA[<p>First of all, I&#8217;ve settled on a name for the game that has so far been called &#8220;Sol Defender&#8221;. You&#8217;ll hear more about that later for the official unveiling.</p>
<p>Since my last post, a lot of work has been done in a variety of areas, but I&#8217;m only going to focus on one for this post: the Shockbomber! If you&#8217;ve played Geometry Wars, then you know that sometimes you can just get overwhelmed by the massive amounts of enemies on screen, and sometimes you just need some help. The Shockbomber essentially fills that &#8220;screen killing&#8221; role, with a bit of a spherical world twist.</p>
<h3><span id="more-791"></span>The Visuals</h3>
<p>As mentioned earlier, the idea for the Shockbomber is to clear out a screen-full of enemies. Now the easiest way to do this would just be to flash the screen white and destroy all of the enemies on screen. But as the game takes place on a spherical world, I figured I could do something a little more interesting visually.</p>
<p><object width="600" height="363"><param name="movie" value="http://www.youtube.com/v/d2k2aPYNq7Y?version=3"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/d2k2aPYNq7Y?version=3" type="application/x-shockwave-flash" width="600" height="363" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>As you can see from the above video, the Shockbomber emits a shockwave spreading outwards from the player destroying anything in its path. You&#8217;ll notice that the shockwave actually wraps around the surface of the planet, creating a pretty cool visual effect. One bonus of creating the bomb like this is that it opens the door for enemies to &#8220;jump&#8221; over the shockwave! I probably won&#8217;t use this on standard enemies since the whole point of using the bomb is when you get overwhelmed. But it might be nice during a boss battle or something. Or hey, maybe I should add a jump button to the player&#8230; !</p>
<h3>The Code</h3>
<p>To get the ring around the planet effect, I used Unity&#8217;s Line Renderer component and set the vertex count and line width to something reasonable. The vertex positions are what give the ring its shape, so I needed a method that not only programmatically created a ring, but make the ring expand and wrap around a spherical body as it moved along its surface.</p>
<p>I decided to leverage what I already had with the bullets, whose movement code boils down to simply this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">sphericalMovement.<span style="color: #660066;">Move</span><span style="color: #009900;">&#40;</span>transform.<span style="color: #000066;">forward</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I&#8217;ve moved all <a title="Spherical" href="http://www.tinytimgames.com/2011/03/20/spherical/">spherical movement code</a> into its own script so I can simply added it as a component to anything that needs to move along a planet&#8217;s surface. Movement is then just a function call away.</p>
<p>So how do I use this to make a ring? Since bullets already move along the surface of the planet, what I needed to do was essentially fire a bullet that then emanated a ring from its position. To create the ring, I need an axis around which to create the ring:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> OnSpawn<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   axis <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>sphericalMovement.<span style="color: #660066;">GravityDirection</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>GravityDirection is a property in the SphericalMovement component script, which essentially just gives the direction of gravity on the object. Taking the negative of this gives us the vector we&#8217;re looking for. OnSpawn is called after the projectile is spawned.</p>
<p>I now have everything I need to create the ring using a Line Renderer:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> Update<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   sphericalMovement.<span style="color: #660066;">Move</span><span style="color: #009900;">&#40;</span>transform.<span style="color: #000066;">forward</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> kRingVertexCount<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> vertexPosition <span style="color: #339933;">=</span> Quaternion.<span style="color: #660066;">AxisAngle</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>360.0f <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span>kRingVertexCount <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> axis<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> transform.<span style="color: #660066;">position</span><span style="color: #339933;">;</span>
      lineRenderer.<span style="color: #660066;">SetPosition</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> vertexPosition<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And that&#8217;s really all there is to it. Collision detection is handled by doing Physics.RaycastAll() between each vertex in the line, get a list of hit GameObjects, and then calling damage functions on their appropriate components.</p>
<h3>New Language</h3>
<p>You may have noticed that this code was written in JavaScript instead of C# like my previous posts. I actually converted all of the scripts for the game to JavaScript over the weekend. While this may seem a bit insane, it was actually done for efficiency reasons.</p>
<p>I&#8217;m a bit OCD when it comes to object encapsulation and keeping everything properly private/protected/public, and C# simply gave me too much control over that. With JavaScript, most of that control is taken away from me, and I can get back to writing the game, not worrying about organization. Additionally, JavaScript is not nearly as verbose as C#, so I save some typing time too. There are a few things I can&#8217;t do in JavaScript, but I simply write those scripts in C# and place them in the Plugins folder (so they are compiled earlier, and thus exposed to JavaScript).</p>
<p>As a result, my scripts have become smaller and easier to read. Performance also hasn&#8217;t suffered since JavaScript and C# perform the same in Unity. Would I recommend this exercise to everyone that&#8217;s using C#? No. It&#8217;s purely for personal efficiency reasons, but I would recommend dipping your toe into Unity&#8217;s JavaScript from time to time as they improve the language quite a bit from version to version.</p>
<p>That&#8217;s all for now. Next post will be focusing on a very cool system I&#8217;ve been prototyping for this game&#8230; the Mod system! I&#8217;m pretty excited about this one.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tinytimgames.com/2011/04/24/shockwave/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Attacked From All Sides</title>
		<link>http://www.tinytimgames.com/2011/04/03/attacked-from-all-sides/</link>
		<comments>http://www.tinytimgames.com/2011/04/03/attacked-from-all-sides/#comments</comments>
		<pubDate>Sun, 03 Apr 2011 10:15:44 +0000</pubDate>
		<dc:creator>Jerrod Putman</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Sol Defender]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://www.tinytimgames.com/?p=784</guid>
		<description><![CDATA[It&#8217;s taken a bit longer to write this post than I initially anticipated, but I have a pretty good excuse: My wife and I welcomed our daughter Guinevere Katherine into the world on March 22nd. It&#8217;s been a bit of a whirlwind around here ever since! As I mentioned last time, enemies are next on [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s taken a bit longer to write this post than I initially anticipated, but I have a pretty good excuse: My wife and I welcomed our daughter Guinevere Katherine into the world on March 22nd. It&#8217;s been a bit of a whirlwind around here ever since!</p>
<p>As I mentioned <a title="Spherical" href="http://www.tinytimgames.com/2011/03/20/spherical/" target="_blank">last time</a>, enemies are next on the agenda. While writing enemy AI on a spherical world is mostly the same as it would be on a flat plane, the biggest difference is determining where the player is and then finding the shortest path to him. Since the planets in our game aren&#8217;t going to have any obstacles (at least, not yet), all enemies simply need a direct path to the player. But how can you get a straight path on a sphere?</p>
<h3><span id="more-784"></span>Spherical Tracking</h3>
<p>The key to spherical tracking is getting the player&#8217;s position and then calculating the direction on the sphere that the enemy needs to move in order to gain ground on its prey. Luckily, there&#8217;s a function in the Unity scripting API that will give us the exact results we want:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Vector3<span style="color: #008000;">.</span><span style="color: #0000FF;">OrthoNormalize</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> Vector3 normal, <span style="color: #0600FF; font-weight: bold;">ref</span> Vector3 tangent<span style="color: #008000;">&#41;</span></pre></div></div>

<p>Looking at the Unity documentation, we find that the function &#8220;normalizes tangent and makes sure it is orthogonal to normal (that is, angle between them is 90 degrees)&#8221;. So what does all of that mean to us?</p>
<p>What we&#8217;re looking for is a directional vector that will tell us what direction on the sphere to move. To get this, we&#8217;ll be passing in our enemy&#8217;s up transform vector as the normal. This is because we want to find a direction to move in from our current orientation.</p>
<p>The tangent will be a vector from the attractor&#8217;s center to the target (the player). When we pass this into the OrthoNormalize method, this tangent is adjusted to be orthogonal to the normal (the up vector). What this means is that we&#8217;ll end up with a vector which points (mostly) along the surface of the sphere.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Vector3 transformUp <span style="color: #008000;">=</span> transform<span style="color: #008000;">.</span><span style="color: #0000FF;">up</span><span style="color: #008000;">;</span>
Vector3 attractorToTarget <span style="color: #008000;">=</span> target<span style="color: #008000;">.</span><span style="color: #0000FF;">transform</span><span style="color: #008000;">.</span><span style="color: #0000FF;">position</span> <span style="color: #008000;">-</span> attractor<span style="color: #008000;">.</span><span style="color: #0000FF;">transform</span><span style="color: #008000;">.</span><span style="color: #0000FF;">position</span><span style="color: #008000;">;</span>
&nbsp;
Vector3<span style="color: #008000;">.</span><span style="color: #0000FF;">OrthoNormalize</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> transformUp, <span style="color: #0600FF; font-weight: bold;">ref</span> attractorToTarget<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>We now use this vector to move our enemy, which we learned how to do in my last post.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Vector3 movementDirection <span style="color: #008000;">=</span> attractorToTarget<span style="color: #008000;">;</span>
transform<span style="color: #008000;">.</span><span style="color: #0000FF;">position</span> <span style="color: #008000;">+=</span> movementDirection <span style="color: #008000;">*</span> movementVelocity <span style="color: #008000;">*</span> Time<span style="color: #008000;">.</span><span style="color: #0000FF;">deltaTime</span><span style="color: #008000;">;</span></pre></div></div>

<p>If you were to try this, you&#8217;d see the enemy following the player all over the planet with very little trouble. You can see it in action in the following video:</p>
<p><object width="500" height="306"><param name="movie" value="http://www.youtube.com/v/PAafUwqvCII?version=3"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/PAafUwqvCII?version=3" type="application/x-shockwave-flash" width="500" height="306" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<h3>Swarming</h3>
<p>One thing you&#8217;ll notice in the video is how the enemies sort of swarm all around the player even though they&#8217;re all using the basic code above. This is a neat trick called swarming that I learned way back in my <em>Ratchet &amp; Clank</em> days (in fact, we called most enemies that used this method of movement simply &#8220;swarmers&#8221;, even though there were many varieties of them throughout the Ratchet series).</p>
<p>Enemies moving in a straight line just is boring from a gameplay perspective. It also looks weird as enemies tend to bunch up on top of each other or have trouble moving if you are using collision. Swarming looks to solve this problem by having each &#8220;swarmer&#8221; randomly curve around the location of the target. This gives the impression that the enemies are trying to surround the player from all sides.</p>
<p>To do this swarming movement, add this code just above where you actually move the enemy&#8217;s transform:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">movementDirection <span style="color: #008000;">=</span> Quaternion<span style="color: #008000;">.</span><span style="color: #0000FF;">AngleAxis</span><span style="color: #008000;">&#40;</span>swarmAngle, transform<span style="color: #008000;">.</span><span style="color: #0000FF;">up</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">*</span> movementDirection<span style="color: #008000;">;</span></pre></div></div>

<p>Essentially, this creates a rotation of swarmAngle degrees, and then rotates the previously calculated movementDirection by this rotation.</p>
<p>And that&#8217;s it! No, really! As the enemy gets closer to its target, the rotation adjustment will appear to affect the movement direction less and less. But this is just how the math works out. Like I said, very simple to implement, but it adds so much to the gameplay.</p>
<p>To really get the &#8220;from all sides&#8221; effect, you&#8217;ll want to give each swarmer a random swarm angle. Additionally, don&#8217;t go too overboard on the swarm angle. After a certain point, the rotation will overpower the movement direction and the enemy will never reach its target. Just start out with something small like 5 or 10 degrees and experiment from there.</p>
<h3>Next Steps</h3>
<p>That&#8217;s all for this post. We&#8217;re going to be bulking up on enemies for a while, creating various types for the player to shoot at. We&#8217;ll then discuss setting up enemy waves so that we can have some sort of flow to the level instead of just randomly creating waves of enemies or trying to create them procedurally.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tinytimgames.com/2011/04/03/attacked-from-all-sides/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Spherical</title>
		<link>http://www.tinytimgames.com/2011/03/20/spherical/</link>
		<comments>http://www.tinytimgames.com/2011/03/20/spherical/#comments</comments>
		<pubDate>Mon, 21 Mar 2011 02:45:00 +0000</pubDate>
		<dc:creator>Jerrod Putman</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Safety First]]></category>
		<category><![CDATA[Sol Defender]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://www.tinytimgames.com/?p=729</guid>
		<description><![CDATA[Now that we&#8217;ve split the News and Blog sections of the site, I feel more at ease talking about more of the stuff we&#8217;re working on without feeling like everything is some big official announcement. Just warning you up front, the games/projects displayed on this blog may or may not ever be finished or released. [...]]]></description>
			<content:encoded><![CDATA[<p>Now that we&#8217;ve split the <a title="News" href="http://www.tinytimgames.com/news/" target="_blank">News</a> and <a title="Blog" href="http://www.tinytimgames.com/blog/" target="_blank">Blog</a> sections of the site, I feel more at ease talking about more of the stuff we&#8217;re working on without feeling like everything is some big official announcement. Just warning you up front, the games/projects displayed on this blog may or may not ever be finished or released. Think of it as a development diary, where you&#8217;ll see the creative process in action.</p>
<p>As <a title="Best Intentions" href="http://www.tinytimgames.com/2011/03/16/best-intentions/">promised</a>, here is the first in a series of posts talking about another of our games that we currently have in development. This one doesn&#8217;t have an official name yet. We&#8217;ve been calling it &#8220;Sol Defender&#8221; for the time being, though it may not ship with that name. Here&#8217;s a very early screen with some temp art to show you what we&#8217;re shooting for:</p>
<div id="attachment_781" class="wp-caption aligncenter" style="width: 586px"><a href="http://www.tinytimgames.com/wp-content/uploads/2011/03/Screen-shot-2011-03-20-at-1.22.10-PM-e1300652642364.png"><img class="size-full wp-image-781 " title="Sol Defender first screen" src="http://www.tinytimgames.com/wp-content/uploads/2011/03/Screen-shot-2011-03-20-at-1.22.10-PM-e1300652642364.png" alt="" width="576" height="386" /></a><p class="wp-caption-text">Click to enlarge</p></div>
<p>As you can probably tell, it&#8217;s a dual-stick space shooter (stay with me) set on spherical worlds (like Super Stardust HD, Super Mario Galaxy, or Ratchet &amp; Clank). We&#8217;ve got an interesting gameplay twist that we&#8217;re working on that uses the spherical worlds in a neat way, but I&#8217;ll save that for another post. For now, I want to give a little background on the project itself, along with some code that shows how we&#8217;re pulling off the spherical world effect.</p>
<h3><span id="more-729"></span>Spherical Dreams</h3>
<p>I first encountered the concept of &#8220;spherical worlds&#8221; in games while playing<em> Ratchet &amp; Clank: Going Commando</em> way back in early 2004, and immediately fell in love with the concept. Indeed, I fell in love with the rest of the game as well, so much so that I ended up working at Insomniac Games (the developers) for over two years. Spherical worlds made an appearance in Up Your Arsenal (but not in Deadlocked, sadly).</p>
<p>I&#8217;ve basically been enamored with the concept of spherical worlds in games ever since. I understand the physical impossibility of a small planet-like body not being able to sustain any meaningful gravitational pull, but I&#8217;m not in the games business to make simulations!</p>
<p>After the release of Sheepstacker, I began developing a prototype for a tower defense-ish game set on a spherical world. Then ngmoco released Star Defense. Irritating. The gameplay was actually going to be significantly different between the two games, but I decided to move on anyways.</p>
<p>I then began working on a prototype I called &#8220;iArena&#8221;. It was going to be a dual-stick shooter with fully animated characters running around on spherical worlds blowing things up, with full online multiplayer. We had a successful prototype and even showed it off to a few people, including publisher Chillingo. However, the game was just too much for us to pull off at the time, and probably still is. Additionally, I&#8217;m not sure the iOS devices at the time could have handled the full game anyways.</p>
<p>Finally, we scaled the spherical world concept all the way back to space ships waging war around a planet. In the interest of worrying more about the technical aspects of the game rather than the artistic aspects like I had for our previous two games, I bought a big pack of low-poly ship models online and some skyboxes. Here&#8217;s a test video of what we currently have (no enemies in this particular video):</p>
<p><iframe title="YouTube video player" src="http://www.youtube.com/embed/WBTnYx3VHRI" frameborder="0" width="600" height="450"></iframe></p>
<h3>Spherical Movement</h3>
<p>Now that you have the story thus far, I&#8217;d like to delve a bit into how we&#8217;re handling the spherical movement aspect of the game. The concept is actually fairly simple: The gravity vector <em>for all objects</em> in a normal game world points downwards, and all physics simulations are based on this single vector. On a spherical world, however, the gravity vector <em>for each object</em> is a vector from the object to the center of the <em>spherical attractor</em> (e.g., a planet).</p>
<p>For our game, we were able to simplify the simulation. As all ships and objects will be at a constant distance from the surface of the planet, we don&#8217;t need to actually apply a gravity force toward the center of the attractor for each object. We simply need to orient the object to the gravity vector and lock the object to a specific distance from the center of the attractor.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Vector3 gravityDirection <span style="color: #008000;">=</span> Vector3<span style="color: #008000;">.</span><span style="color: #0000FF;">Normalize</span><span style="color: #008000;">&#40;</span>attractor<span style="color: #008000;">.</span><span style="color: #0000FF;">transform</span><span style="color: #008000;">.</span><span style="color: #0000FF;">position</span> <span style="color: #008000;">-</span> transform<span style="color: #008000;">.</span><span style="color: #0000FF;">position</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
transform<span style="color: #008000;">.</span><span style="color: #0000FF;">rotation</span> <span style="color: #008000;">=</span> Quaternion<span style="color: #008000;">.</span><span style="color: #0000FF;">FromToRotation</span><span style="color: #008000;">&#40;</span>transform<span style="color: #008000;">.</span><span style="color: #0000FF;">up</span>, <span style="color: #008000;">-</span>gravityDirection<span style="color: #008000;">&#41;</span> <span style="color: #008000;">*</span> transform<span style="color: #008000;">.</span><span style="color: #0000FF;">rotation</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// &quot;kDistanceFromAttractorCenter&quot; is a float marking the radius of the &quot;sphere&quot;</span>
<span style="color: #008080; font-style: italic;">// that the ships move on top of.</span>
transform<span style="color: #008000;">.</span><span style="color: #0000FF;">position</span> <span style="color: #008000;">=</span> attractor<span style="color: #008000;">.</span><span style="color: #0000FF;">transform</span><span style="color: #008000;">.</span><span style="color: #0000FF;">position</span> <span style="color: #008000;">+</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">-</span>gravityDirection <span style="color: #008000;">*</span> kDistanceFromAttractorCenter<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>That&#8217;ll handle the positioning and orientation of any object in the game, including ships, bullets, and anything else. For movement, take your standard movement vector and multiply it by the object&#8217;s rotation:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// &quot;movementVector&quot; is a vector whose z will move the ship forward along</span>
<span style="color: #008080; font-style: italic;">// the sphere and whose x will move the ship side-to-side.</span>
transform<span style="color: #008000;">.</span><span style="color: #0000FF;">position</span> <span style="color: #008000;">+=</span> movementVector <span style="color: #008000;">*</span> transform<span style="color: #008000;">.</span><span style="color: #0000FF;">rotation</span> <span style="color: #008000;">*</span> Time<span style="color: #008000;">.</span><span style="color: #0000FF;">deltaTime</span><span style="color: #008000;">;</span></pre></div></div>

<p>While the math here isn&#8217;t entirely accurate, it&#8217;s good enough for our purposes. And that&#8217;s pretty much all we need for the spherical effect.</p>
<p>In my next post about this game, I&#8217;ll go into the development of some of our enemy ships. It should illuminate just how enemies can track a player when they&#8217;re on a spherical world (it&#8217;s much easier than you might think).</p>
<h3>In Other News</h3>
<p>In my previous post, I mentioned that we would be putting Safety First on the backburner for a while due to some level design issues. Well, level designer (and good friend of mine) Larry Charles from GoPlay Games contacted me to take a stab at it, so it&#8217;s back on! I&#8217;ll be posting updates on that game as well as development progresses on it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tinytimgames.com/2011/03/20/spherical/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

