Playing around with audio in Unity

When I was putting audio into 10 Seconds in 10 Seconds, I found it annoying that certain sounds would only play partially. This was happening because the sounds in question were being played by objects that existed for a very short amount of time.

When a GameObject gets destroyed, it’s AudioSource also gets destroyed. If that AudioSource was playing a sound, it will stop immediately when destroyed.

The usual solution to this problem would be to create a new GameObject just to play the sound you want and destroy it when done playing. There are a few problems with this solution:

  1. It’s creating and deallocating objects too often, which can cause memory fragmentation.
  2. From my experience, it’s not flexible because I could only get it to work if I made a different prefab for every sound to play.
  3. Implementing sound effects in this matter might confuse other programmers who are expecting a call to play audio as opposed to a call to instantiate a prefab.

What I wanted to do in for audio playback in Unity was to play sounds in multiple channels in the same way an audio work station used mixer software to mix audio from multiple channels. The bad news: Unity does not give you this kind of control (directly anyway). The good news, Unity can mix audio together, as long as they are being played from multiple instances of AudioSource.

With this in mind, I wanted to make a mixer that can be called from anywhere to play sound to a specific channel. The idea is you can make a call to this audio mixer to play a sound in a specific channel and label the sound as a specific audio type. The audio types would include sound, music, jingle, and voice. I also wanted playback controls based on a specific channel, all channels, or channels label with a specific audio type.

After messing around for a few days, I would like to present the AudioMixer. It’s a Unity script that allows mixing of sound in multiple channels. This can be very useful for playing reoccurring  sounds, loop music, or other audio tasks.

You can read about the AudioMixer on the AudioMixer page, or you can go straight to Github and get it. Documentation and instructions on how to use AudioMixer are provided on Github.

Advertisements

The Motivation behind XboxCtrlrInput

XboxControllerThe motivation to make XboxCtrlrInput came about from a problem I’ve encountered a few times in Unity3D.

It started with my team capstone project, Fortress Fiasco. One of the important features the team wanted to implement was local co-op play with multiple Xbox 360 controllers. I remember the programmer in our team who was implementing Xbox input was complaining about how awful Unity is about handling multiple joystick input. And he’s not the only one. I looked into his input code and realized that Unity was probably treating joystick input management as an afterthought. Of course I love using Unity, but I think Unity should improve the handling of multiple joysticks.

Flash forward to my Ludum Dare entry 10 Seconds In 10 Seconds, I wasn’t thinking about multiplayer at the beginning of its development. I did implement joystick input with an Xbox 360 controller, but my focus at the time was to make a single player game. Joystick input with just one joystick isn’t terribly hard with Unity. Towards the middle of development, I realized that it was possible to make a multiplayer version of the game because it became a top-down duel. I had to decide between either an AI opponent or a human-controlled second player. I didn’t have two Xbox controllers at the time so I chose to tackle with AI.

When I submitted my game on Ludum Dare, I got a lot of feedback saying that the game would be great with multiplayer, and I agree with them. Considering the control scheme of 10 Seconds In 10 Seconds, I thought using multiple Xbox controllers would be the best way to go, instead of having two players share the keyboard. Since I released a Mac, Windows, and Linux version of the game, it only made sense to me that a post-Ludum-Dare version of the game should continue to work on all those platforms with multiple Xbox controllers.

I know it sounds a little naïve, but I always figured that one of the goals of Unity was to allow people to develop games they can release on multiple platforms. Ideally everything should work on multiply platforms, including joystick input. It’s obviously not as simple as it sounds.

There are two major problems you have to face when implementing such a system. First, you have to get button mapping and axis mapping to “just work” on every operating system. For that, I won’t put much blame on Unity, since that’s a platform-dependent ordeal. But I will put much blame on Unity for the second problem: multiple joystick input support is lacking and difficult work with.

There appears to be a few attempts that exist out there on the internet to try to solve this problem, but none of them are perfect. Somebody with the domain name “uberdruck” showcased a web build that seems to be pretty good at handling multiple controllers, but uberdruck does not explain how the demo’s input system works. It’s only useful as an observation for what is possible. Deciduous Games made a C# input wrapper for Xbox controller input. It’s similar to XboxCtrlrInput in terms of being a static class, but the input mapping was implemented with Windows in mind, and thus will not work on other platforms without tweaking it. The last and most popular solution was a Github-hosted project called XInputDotNet, which is an XInput wrapper to allow XInput to work on .NET and Mono frameworks. This allows XInput to be used in Unity, which will allow for much better Xbox controller input. Only problem is that XInputDotNet relies on DirectX, so it will only work on Windows.

My goal with XboxCtrlrInput is to make a C# wrapper for Unity that makes it easy to get input from Xbox 360 controllers on multiple platforms (Mac/Windows/Linux). The included C# class XCI should be interfaced in the same way as Unity’s Input class. It sounds like a lot, but I’ve made fair progress. I invite anyone to contribute to the project on Github here. It’s free, open-source, and public domain, because I think Xbox controller input should “just work” with Unity. If you want to know the current issues about getting the input to work, be sure to take a look at XboxCtrlrInput’s Issues page on Github. If you don’t use Github, you can download a zip file containing the latest code from Github. Demo project included requires Unity 4.2 because I make use of text asset serialization (which became a free feature in version 4.2).

Game Maker: Gradually rotating an object towards a target

You can get the GML script here. This post will explain the code.

When I was developing enemies in Yxi, I was trying to get turrets to aim at you, but I didn’t want them  to possess “instant aim”. I wanted them to rotate towards you given a maximum rotation speed. If you move too fast, it should turn towards you gradually, not instantly.

It seems that it wasn’t as simple as I thought it was. I had the basic algorithm down, but because of the way Game Maker handled angles, the turrets would behave strangely when you would hover around the 0°/360° point with respect to the turret.

I figured a tutorial would be necessary for something that isn’t so obvious to implement. This only pertains to Game Maker and thus uses the GML language.

There are three steps involved when gradually rotating an object (let’s call it a turret for now) towards a target object:

  1. Calculate the target’s direction with respect to the turret’s position.
  2. Calculate the angle difference between the direction derived from step 1 (let’s call “target direction”) and the turret’s facing direction.
  3. Apply angular rotation towards the target so that the target direction is the facing direction.

Continue reading

Yxi – Alpha Demo Video

Here’s my progress on Yxi so far. I lot of work has gone into getting physics right and also making bosses with random parts.

As of yet there aren’t that many boss parts, but the system will randomly choose parts to build the boss. When the player blasts one of the arms, only that arm gets destroyed; the rest of the boss is still alive.

Other things this video demonstrates are how the player gets pushed back upon contact with the boss, how coins follow the player, what player death currently looks like, how shooting the main body will kill the whole boss, and how turrets slowly turn towards your direction.

Yes the graphics are very ugly, and the boss is currently boring. It’s a work in progress, so this will hopefully turn into something fun. 🙂

Continue reading