Unity 4 – Looking Forward

September 28th, 2012 by Anthony Capobianchi

Here at Infrared5, a good portion of our projects are based in Unity 3D. Needless to say, with the introduction of Unity 4, I was very interested in what had changed about the coming engine, and why people should make the upgrade. This post will look at few of the features of Unity 4 that I am most excited about.

The New GUI

The first time I sat down with Unity almost a year ago to work on Brass Monkey’s Monkey Dodgeball game, I knew practically nothing about the engine. That didn’t stop me from being almost immediately annoyed with Unity’s built in GUI system. Positioning elements from the OnGUI was a task of trial and error, grouping objects together was a pain, and all the draw calls that it produced made it inefficient to boot. At that time, I was unaware of the better solutions to Unity’s GUI that were developed by third party developers, but after I was made aware, I was confused as to why such a robust development tool such as Unity didn’t have these already built in.

Though the new GUI system is not a launch feature for Unity 4, Unity is building an impressive system for user interface that will allow for some really interesting aesthetics for our games. From the looks of it, the new system seems to derive from Unity’s current vein of GUIText and GUITexture objects. The difference is in the animation capabilities of each element that is created. You are now allowed to efficiently have multiple elements make up your GUI objects such as buttons, health bars, etc. Unity then allows you to animate those elements individually. Not to mention that editing text styles in the GUI is now as easy as marking it up with HTML.

One of the coolest additions is the ability to position and resize any UI element with transform grabbers that anyone who has used an Adobe product would be familiar with. This also allows for the creation of rotating elements in 3D space, which allows for creating a GUI with a sense of space and depth to it. This can lead to some really interesting effects.

The new GUI system will come packaged with pre-built controls, though there is no word as to whether or not those controls will be customizable. Unity lists one new control as a “finely tuned thumbstick [controls] for mobile games”.  A couple of months ago, I developed my own thumbstick like controls to maneuver in 3D space, and it was a pain. Hopefully these new controls will make it a lot easier. You can also easily create your own controls by extending from the GUIBehavior script. Developers should have no problem creating controls that handle the specifics of their own games.

Every image that you use to create your elements gets atlases automatically. This is a huge bonus over the old GUI system. The biggest problem Unity’s GUI system has right now is the amount of draw calls it makes to render all those elements. Third party tools like EZGUI and iGUI rely on creating UI objects that atlas images to reduce draw calls. It will be nice to have that kind of functionality in a built in system. I’ve spent a lot of time developing user interfaces in Unity over the past few months, so it makes me really excited to see that Unity is trying to correct some of their flaws for creating a component that is so important to games.

Unity’s current animation system is pretty basic- add animations to an object and trigger those animations based on any input or conditions that are needed. The animation blending was useful but could have been better. With Unity 4, it is better. Introducing the Mecanim: an animation blending system that uses blending trees for models with ridged bones to fluidly move from one animation to another. One of the biggest hurdles that we as developers need to overcome in projects that deal with a lot of animations is transitioning from those animations as seamlessly as possible. Not always easy!

Along with blending the animations, Mecanim allows you to edit your animations similar to how you would edit a film clip to create animations loops. Mecanim also supports IK, so for example it can change the position of a characters feet on uneven surfaces, bend hands around corners, etc. A couple of years ago I was fascinated by Natural Motion’s Endorphin engine for animation blending. Mecanim may not be as sophisticated as Endorphin, and only supports biped skeletons, but it seems like an incredible system that comes built in to Unity.

The best part is about this is that once you create a blend tree for your animations, you can drag and drop it onto another rigged model, and it will work even if the new model is a different size or proportion.

The Mobile Platform

The mobile scene is really where Unity shines. Most of the Unity projects I have worked on for Infrared5 have had some sort of mobile component to them. The mobile platform is going to get even better with Unity 4. The most interesting thing from a developer’s standpoint is the profiling system, which allows you to view your game’s GPU performance to determine where it runs smoothly, and where it needs more optimization. The addition of real-time shadows for mobile is a nice added bonus. It will definitely add a lot of aesthetic value to the products we make.

Unity 4 is going to hit the industry with amazing force. I, for one, cannot wait to get my hands on this engine and am already filled with ideas on how I want to utilize these new tools. My favorite part is going to be the mobile optimization. Mobile development is huge, and it’s not going anywhere anytime soon. With all the new capabilities of Unity’s mobile content, I should be kept interested for quite a while.

, , , ,

Boid Flocking and Pathfinding in Unity, Part 3

July 23rd, 2012 by Anthony Capobianchi

In this final installment, I will explore how to set up a ray caster to determine a destination object for the Boids, and how to organize a number of different destination points for your Boids so that they do not pile on top of each other.

Organizing the Destinations -

The idea is to create a marker for every Boid that will be placed near the destination, defined by the ray caster. This keeps Boids from running past each other or pushing each other off track.

For each Boid in the scene, a new Destination object will be created and managed. My Destination.cs script looks like this:

This is very similar to the Boid behaviors we set up in Boid.cs. We create coherency and separation vectors just as before, except this time we use a rigid body that has the two vectors being applied to it. I am using rigid body’s velocity property to determine when the destination objects are finished moving into position.

Positioning and Managing the Destinations -

Now we create a script that handles instantiating all the destination objects we need for our Boids, placing each one in relation to a Boid, and using each destination’s Boid behaviors to organize them  I created a script called DestinationManager.cs where this will be housed.

First off we need to set up our script:

We need to create our ray caster that will tell the scene where to place the origin of our placement nodes. Mine looks like this:

The ray caster shoots a ray from the camera’s position to the ground, setting the Boid’s destination where it hits.

Next, we take the destinations that were created and move them together using the Boid behaviors we gave them.

The Boid system is primarily used for the positioning of the Destination objects. This method ensures that the Boid system will not push your objects off of their paths, confusing any pathfinding you may be using.

, , , ,

Boid Flocking and Pathfinding in Unity, Part 2

July 5th, 2012 by Anthony Capobianchi

In my last post, we worked through the steps to create a Boid system that will keep objects together in a coherent way, and a radar class that will allow the Boids to detect each other. Our next step is to figure out how to get these objects from point “A” to point “B”, by setting a destination point.

Pathfinding -

For this example, I used Aron Granberg’s A* Pathfinding Project to handle my pathfinding. It works great and uses Unity’s CharacterController to handle movement, which helps with this example. A link to download this library can be found at http://www.arongranberg.com/unity/a-pathfinding/download/ and a guide to help you set it up in your scene can be found here: http://www.arongranberg.com/astar/docs/getstarted.php

In Boid.cs I have my path finding scripts as such:

Applying the Forces -

Once we have the calculated force of the Boid behaviors and the path finder, we have to put those together and apply that to the character controller. We use Unity’s Update function on Boid.cs to constantly apply the forces.

In my next post, we will look at using a ray caster to set a destination point in the scene for the Boids, as well as how to organize a number of different destination points for the Boids to keep them from piling on top of each other.

Read part one here.

Boid Flocking and Pathfinding in Unity

June 20th, 2012 by Anthony Capobianchi

The quest for creating believable, seemingly intelligent movement among groups of characters is a challenge many game developers encounter. A while back, I had an idea for an app that required units of moveable objects to be able to coordinate and get from point ‘A’ to point ‘B’., I’ve never built anything like an RTS before, so this concept was something new to me. I searched forums and articles looking for the answers on how people achieve this sort of behavior. The majority of the help I could find was in a system referred to as “Boid” or “Flocking”, A Boid system can be set to simulate flocks or herds, allowing moving units to animate independently, giving the illusion of artificial intelligence. Over the next three blog posts, I will outline solutions to the following problems:

  1. Boid System – Creating a system to keep objects together in a coherent way.
  2. Radar Class – Creating a class that will detect if any Boids are within a certain distance from another Boid.
  3. Path and Obstacle Avoidance – Getting an object to follow a path as well as avoid obstacles that keep them from getting to their destination.
  4. Ray Caster – Setting up a ray caster that will be used to place a destination object in the scene for the Boids.
  5. Destination Points – Organizing a number of different destination points for the Boids that will prevent   them from piling on top of each other.


Normally, I would split up the functions into different scripts depending on their function, for instance, a script for calculating the Boid behavior force, a script for the radar, and a script for  path calculating. However, to keep the count down and to avoid possible confusion of not always knowing which script a function goes into, I consolidated it into only a few scripts -

I.    Boid.cs
II.    Destination.cs
III.    DestinationManager.cs

NOTE: All properties and variables should go at the top of your classes, in my code examples I am putting the properties above the methods to show you which properties you need in your script and why.

•    Boids

The Boid system is accomplished by creating a script (Which was named Boid.cs) that controls the behaviors of their basic movement. This included coherency, which is the will to stick together, and also separation, which is the will to keep apart. If you are unfamiliar with the concept of Boids or flocking, a great article about it can be found at http://www.vergenet.net/~conrad/boids/pseudocode.html and a C# Unity example can be found here: http://virtualmore.org/wiki/index.php?title=SimpleBoids

To set up my Boid.cs script, I set up these properties:

My Boid behaviors are set up like this:


We need a way for every Boid to know if there are other Boid objects surrounding it within a certain radius. In order to create this effect, we will create functions that will handle radar scans and what the scanner is looking for. The radar will be called to scan a few times every second. It is not using the Update function to get called. If it was, every frame would be making a collision check using Physics.OverlapSphere. This could cause frame rates to drop, especially if you have a lot of Boids in the scene.

In my Boid.cs script my Radar functions are set up like this:

In my next post, I will explain how I solved the problem of getting an object to follow a path while avoiding obstacles.In addition, I will explain what will be needed to apply the forces that are calculated by the Boid and pathfinding systems to get our characters moving.
Anthony Capobianchi

, , , ,