Posts Tagged "programming"

Configurable Input in Unity

Joe Strout —

I have long thought that Unity did not really support configurable input out of the box — at least not without using the ugly default Graphics/Input configuration dialog (which no polished game would ever inflict on its players). Particularly if you wanted to support hot-swappable joysticks or gamepads, I always believed you had to use some third-party plug-in.

But I recently discovered that this is not true! Making configurable, hot-swappable input in Unity can be done without any plug-ins, and it's not even all that difficult. Read on to see how.

read full post

Fading the Music on a Scene Change

Joe Strout —

A lot of games in Unity are organized into several scenes, most notably a title scene and a play scene.  If your game has background music, you're likely to want different background music for each scene.

Just sticking a music clip on an AudioSource in the scene would accomplish that, but the music would cut off abruptly when you change scenes, which is jarring and unprofessional.  Much better to fade it out over several seconds.  That requires not letting the object be destroyed when the scene changes, and handling the scene-change event — which thanks to recent changes in the Unity API, is not as easy as it used to be.

read full post

Arcing Projectiles in Unity

Joe Strout —

A frequently asked question in both the Unity forums and on Unity Answers is: How do I make a projectile arc to its target, like an arrow shot from a bow?  I've seen (and given) lots of different answers to this question, and honestly, most of them are unjustified hacks.

The right (and easy!) way to do this is: just add a bit of arc to your standard movement.  Objects in freefall (ignoring air resistance) follow a parabolic arc, and the equation for a parabola is very simple.  So, we can just use that equation to compute how must extra height we should have, and simply add it to our Y position, and the job is done.

read full post

Implementing Cheat Codes

Joe Strout —

Cheat codes are secret ways to alter the functionality of a game.  It's a term that makes me cringe as a parent, since we teach our kids never to cheat — but once a game starts to grow complex, cheat codes are absolutely essential testing tools.  They let you bypass minutes or hours of gameplay you already know is working, in order to get to the part you're trying to fix.

So, how do you actually implement them?  This post explains how we do it in our Unity games.

read full post

Use a PID loop to control Unity game objects

Joe Strout —

One of the most general and common tricks ever to come out of industrial control theory is the proportional–integral–derivative controller (PID controller), sometimes known as a "PID loop."  It is a simple equation that you can use to control any one-dimensional variable, such as a throttle, a motorized hinge, etc.  All it needs for an input is an "error" signal — that is, a measure of how far off something is from where you want it to be, plus three constants.  Most importantly, it does not need any model of how the value it's controlling relates to the output.

In this post, I'll give you well-encapsulated code for a PID controller in C#, and show how to use this in Unity to control the throttle of a hovercar.  The hovercar physics involves momentum, drag, and controls that don't respond instantly, just like a real hovercar!  But the PID controller doesn't care about any of that; once you have the three constants tuned, and hook up the error signal (in this case, the difference between the current altitude and the altitude you want), it does the rest.

read full post

Announcing GRFON, a kinder, gentler serialization format

Joe Strout —

For our High Frontier video game, we wanted a data file format that would be easy for people to read and write.  This is partly for our own use — we would be hand-editing files defining various parts of the game internally — and partly for mod authors, who would be creating new content for the game.

The big players in the serialization space are JSON and XML.  XML is, to be blunt, horrible.  It's fine for data that is only ever looked at by computer programs, but for anything that might be read or written by a human, it's just awful.  JSON is much better, and it's what we've often used in the past.  But it too has more syntax than feels necessary.  And at the same time, it lacks some features we really wanted, like comments.

So we have designed a new serialization format: GRFON (General Recursive Format Object Notation).  We've been using it for over a year now, and loving every minute of it.  And today, we are making it available for everyone else to use too, under the permissive MIT open-source license.

read full post

Showing the build date in a Unity app

Joe Strout —

When doing rapid iterations of an app (a cornerstone of agile development), testers sometimes find themselves with an older version of the app.  This could be because they didn't get the update email, or they clicked the wrong link, or DropBox didn't sync like it's supposed to, or something got cached along the way... whatever the reason, there are few things less helpful than getting a bug report for something you've already fixed.

In C/C++, it's easy enough to use the standard __DATE__ macro, which the C preprocessor fills in with the actual build date.  You can just display this on the title screen or wherever, and testers can easily see whether they're running the latest build.  But, surprisingly, C# doesn't have any such feature.  This makes displaying the build date in a Unity app a bit harder.

Fortunately, it can be done.  Here is the arcane magic you need.

read full post

Triangulating 3D Polygons in Unity

Joe Strout —

I'm currently doing a job where I need to take 3D polygon data and display it in Unity.  These polygons are planar, but oriented arbitrarily in 3D space.  Moreover, they can contain holes (possibly multiple holes).  Think of a building wall with window cut-outs, and you'll get the idea.

This turns out to be a surprisingly thorny problem in Unity.  There is a simple script on the Unity wiki called Triangulator, but it only works with Vector2D and doesn't support holes.  I found a blog post on Advanced Triangulation in Unity, but it was neither sufficiently advanced (only works in the XY plane) nor actually in Unity (it wrote each polygon out to a file, invoked an external command-line tool to do the triangulation, and then read the result back in).

The utility referenced in that blog post is called Triangle, which is widely regarded as a very good triangle library.  It's open-source C code, so one could make a Unity plugin out of it.  But it's not licensed for commercial use, which is a problem for this project.  Also, making a native plugin means setting up a build chain for every platform you want to support.  For both reasons, I kept looking — I really wanted something in pure C#.

read full post

2D Animation Methods in Unity

Joe Strout —

Unity has provided a built-in state machine editor for managing animations since version 4.  This is the officially recommended approach to animating a game character.  However, it often leads to game logic being divided between code and the animator state machine.  I would prefer to have all my game logic in one place, to simplify development and debugging.  Moreover, in some cases — especially simple 2D sprite games — the Animator can seem like more trouble than it's worth.

To help clarify the pros and cons, I built a 2D game character using three different approaches:

  1. A simple home-grown animation system that eschews Unity's built-in animation support completely.
  2. Use of Unity animations, but without using the Animator state machine; instead each animation is invoked directly from code.
  3. Full use of the built-in Unity components, with all game logic in the state machine, and only minimal supporting code.

For the rest of the story, see the full article on Gamasutra!

read full post

The Real Power of UnityEvent

Joe Strout —

Unity had a "Live Training" demo called Events: Creating a Simple Messaging System.  This is often pointed to as an example of using the UnityEvent class that was introduced in Unity 4.6.

While this was a fun and educational video, it uses UnityEvents in a very odd way, wrapping them in a string-based notification system and hooking everything up in code. Honestly, you could have done exactly the same thing using standard C# events, and it wouldn't have made any difference. (Indeed, prior to UnityEvent, many Unity developers — myself included — did exactly that.)

So I decided to make my own video, accomplishing the same demo but using UnityEvent the way nature intended. The result is a lot less code, easier to maintain, and harder to screw up.

read full post

Interactive Mono C#

Joe Strout —

I've  been teaching my son C# programming lately.  This is a great choice, because it's the best language to use with Unity, and with Unity, he can make games for virtually every platform under the sun. With some luck, he'll be able to make enough money in the various app stores to pay his way through college (or at least pay his own car insurance).  And as a bonus, when you know C#, you can also develop native apps for Mac, Windows, iOS, and Android, using various toolkits (such as MonoTouch for example).

However, the code-run-test-debug cycle is slower in Unity than I would like for his purposes.  Learning a language is easier when you can play around with it interactively.  This is especially true when you're working through small programming problems like those at Project Euler.

read full post

Unity Coroutine Confusion

Joe Strout —

We've been delving into Coroutines in Unity lately, and ran into a strange behavior today that took all morning to sort out. In brief, a routine we thought we were calling never got called at all -- a Debug.Log as the very first line of the function never logged anything.

read full post

Better to be lazy or greedy?

Joe Strout —

Last week, we gave a sneak peek of BOSS, a new approach to string searching. We mentioned a "bit of magic" with regard to the repetition modifiers, * (0 or more) and + (one or more): these would do a lazy match, except at the end of the search pattern, in which case they would be greedy.

We expect this to be the most controversial feature of the whole BOSS design, so it's worth some time to explain all the considerations behind it, why we made this decision, and what the heck "lazy" and "greedy" mean when it comes to string searching anyway.

read full post

BOSS: Sneak Peek

Joe Strout —

In the last couple of blog posts, we first reviewed some of the shortcomings of regular expressions (RegEx). We then took a look at parsing expression grammars (PEGs), which are a new formalism that has a lot of advantages for defining (and more importantly, parsing) computer languages. But while they're great for that, using them directly for string searching is a bit of a square-peg-round-hole situation.

So, we at Luminary Apps have begun work on a string matching library that combines the best features of PEG and RegEx. This blog post is the first public discussion of that library. It's called BOSS, and I think you're going to love it.

read full post

Better Text Searching with PEG

Joe Strout —

In our last entry, I bemoaned the shortcomings of regular expressions for complex tasks. (This was after spending a day wrestling with a three-page-long RegEx pattern for finding functions in a C# TextWrangler language module.) I sketched out what I thought an ideal string-matching system would look like.

Well, that was three weeks ago. I've had time to do some more serious research, and it turns out that there is some modern work that is very relevant. It almost fits exactly what we were looking for — but not quite. It's a new construct called Parsing Expression Grammar.

read full post

Time for an alternative to RegEx?

Joe Strout —

RegEx is handy.  I use it all the time.  For simple tasks, it's quite pleasant to use.  For intermediate-sized tasks, it's acceptable.  But for complex tasks, it is a nightmare to write, read, and maintain.

So, I'd like to suggest that it's time to design an alternative -- something that works just as well on complex tasks as it does for simple ones, and stays readable and maintainable.  I agree with not reinventing the wheel... except when our current wheel is square and lumpy.

read full post
 

All blog posts