Yes, I just spent two hours trying to get some output from Log4Net in Unity3D. The main problem was of course in front of the computer but I also blame the log4net online docs for not having a quick start guide that outputs to a file. I had three problems:

  • I didn’t know what the default working directory of a Unity3D app running in the editor is. (hint: It’s the root of the project folder) So I was looking all over the place for that log file to appear.
  • I copy pasted example configuration snippets from here: log4net Config Examples. I didn’t realize these are not complete config files, but just parts that need to be inserted into a real log4net config.
  • log4net does not report errors regarding to log4net in any way by default. Of course, I didn’t know this either, since who reads the complete documentation to a simple logging API right?
So the result was that log4net was unhappy with my config file but I didn’t notice it since it doesn’t report any errors. Instead, I thought that maybe Unity3D is sandboxing file system access on some level and was chasing around the usual places in the filesystem looking for my newly created log file. Not finding it I tweaked the log4net config file and went looking again. Rinse, repeat. Only after I realized that log4net was not reporting internal errors and then changing this behavior I was notified that my config file was ill formatted. From there getting it to finally work was easy.
I also made some progress with the game design.
 

As the title suggests, I finally managed to add wide corridors in the world generator for my Unity 3D game prototype. Instead of spending a huge amount of time in tweaking and debugging the A* star algorithm that creates the corridors, I now just call it multiple times per door to door connection and encourage it to lay the paths side by side by adjusting the weights in the grid. It leaves some artifacts sometimes but these are easily removed by passing a few times over the tilemap and removing all wall tiles that are surrounded by 3 or more corridor tiles (excluding diagonals). Almost like Conway’s Game of Life.

 

Yesterday I wanted to improve the corridor generation by actually using a path finding algorithm instead of the hard coded mess I had before. While looking for path finding solutions in the asset store, I found a whole bunch of AI solutions – not exactly what I need right now but they looked interesting so I spent two hours looking at them. The most interesting seem to be Rain{one} and the A* Pathfinding Project. Both feature path finding and recast based nav-mesh generation. Rain{one} also comes with environment sensing, behavior trees and integrated animation control while the path finding project seems to be more optimized if you just need path finding. But, as I don’t have a game concept yet and I don’t need AI or navigation meshes right now, I ended up using an old-school open source C# A* implementation for grids. It is well designed and has many options for tweaking. Having the source to the implementation is a big bonus, too,  since I later want to make corridors that are several tiles wide and no standard A* implementation can generate wide paths.

So, equipped with this new tool I can now connect rooms that are not right next to each other – the path finder is able to map out corridors between any two points on the map. I forbid the finder to use diagonal movements and I enabled the direction change penalty which makes it prefer generating straight lines instead of zigzag corridors that it generated to substitute for the lack of diagonals. Another set of tweaks was added to the weights in the grid. Basically, I told it to avoid making corridors directly next to a room’s wall and to prefer to make corridors right next to existing corridors. The last tweak tends to generate some wider corridors in areas where a lot of rooms are next to each other. As long as I can’t just outright tell it to make corridors with three tiles width, this is the next best thing.

While I was at it, I changed the look of the rooms’ walls and added automatic doors to the room exits.

 

No screenshots today. But I restructured the map generation code so that it can cope with multiple z-levels. Right now it just generates the same maze type layout you could see in earlier posts, layered on top of each other. This enables me to generate multistory rooms later. I’m not sure if I will make use of multiple z levels in the final game but it’s cool to have it available for experimenting. In case this game gets a top down Dungeon Keeper style interface, the mouse wheel could be used to move between z-levels. In case it will be first / 3rd person, I will need to add elevators or ramps. I could also just use it to generate background scenery for rooms with transparent floors. The possibilities are endless!

 

While running around in 3rd person perspective I got lost and decided to quickly implement a minimap. Can’t be that hard in Unity3D right? Well, two hours later I finally pulled it off.

The hard part wasn’t to get the map to show, but to draw the location of the player as little red dot onto that map in the correct position. I looked at various tutorials but I they only either dealt with the aspect of actually rendering the world in the minimap or how to draw blips on a radar like screen but without rendering the world. The first aspect required a second camera positioned above the world to actually render the minimap. But the radar only style maps don’t make use of a second camera, they just blit a few textures into a rectangle somewhere on the screen without using a camera. This sounds similar, and yes, it is similar, but not similar enough to take the code from the wiki and have it work.

The actual problem was to determine where the red dot has to be drawn on the minimap so that it covers the position where the player happens to be. You might think that a function called

camera.WorldToScreenPoint

would do the trick, but it doesn’t seem to take the viewport position and size of the camera into account. That alone took a while to realize. After I found that out I just needed to fiddle with the viewport values of the camera to get the projection correct.

So here’s the C# code:

using UnityEngine;
using System.Collections;
 
public class Minimap : MonoBehaviour
{
    public Texture2D BlipTex;
 
    public GameObject Player;
 
    private void OnGUI()
    {
        var objPos = camera.WorldToViewportPoint(Player.transform.position);
 
        GUI.DrawTexture(new Rect(
            Screen.width * (camera.rect.x + (objPos.x*camera.rect.width)) - 2,
            Screen.height * (1-(camera.rect.y + (objPos.y*camera.rect.height))) - 2,
            4, 4), BlipTex);
    }
}

Make a second camera, attach the script to it. Set the camera depth to >0, projection to orthographic. Set the x-axis rotation of the camera to 90 degrees and position it above your world. Drag a texture into the BlipTex field and it should work.

 

Also, the rooms look a lot more random now due to a change in the placing code. Eventually I also want to have it generate non rectangular rooms. Next up is making sure that all rooms are interconnected so that they are all reachable.

Corridors!

 

Finally, after a very, very long time, I’m back into game development. I started working on a game prototype in Unity a few weeks ago and just finished my 3rd session on it. Yeah I know, this game is not going anywhere with a development pace like this, however, this is what I got so far:

So what do you see here? It’s a tilemap. With rooms and (still broken) corridors that connect the rooms. The map is generated procedurally. Ignore the graphics and the textures. These are all placeholders while I’m working on the map generator.

What kind of game is it you ask? I have no idea. I have no firm concept, just a list of urges I want to satisfy. I’m not sure how many others are working this way and if anyone managed to finish a (good) game with a methodology like this, but this is what I am doing now.  I have this desire to combine elements from Nethack, Dungeon Keeper, Dwarf Fortress, The Settlers and Majesty with an Old School Arcade feeling. This list is not exhaustive and might change when I play something else that rocks my world, even if it’s only momentarily. So, I want to make a simulation where stuff is produced and minions are roaming around doing stuff to the stuff. Fights will be had. You have to manage resources, explore, design a base. Maybe. I like games where things happen without the player doing anything. I don’t know yet if this will be a 1st person, 3rd person, RTS, Tower Defense or Tycoon game. I don’t know if it will be multiplayer. I don’t know if a game session will take 10 minutes, 10 hours or 10 weeks. What I do know is that there is no game out there that feels like what I have in mind. So there, now that you know exactly where I’m going with this, let’s talk tech.

I initially wanted to make this a Flashgame with Flixl or FlashPunk, both very awesome frameworks for developing tile-based games. I evaluated both and liked what I saw. Then I looked at Unity and was immediately hooked.  Multiplatform, Rapid Application Development, C# scripting (yes!), a large development community, the asset store. Wow. The time it takes to get something to screen is really short, so this is the perfect framework for prototyping, even though it’s my first project using Unity. I’m learning a lot of basics as I go and I think I’m about 6 hours in now. Feats accomplished so far:

  • Generate non-overlapping rooms in 2D space (brute force randomize new room until it doesn’t overlap), tends to get very slow. I should really just use some randomized, recursive partitioning instead.
  • Detect neighboring rooms and and build a connection graph.
  • Fail to generate corridors according to said connection graph.
  • Convert the generated 2D tile map into a 3D mesh.
  • Add a player character from the Angry Bots Unity demo project and control it with both an overhead shooter control scheme and a Dead Space like over the shoulder camera (not at the same time).

This project has no name, yet and thus will be known as the project that has no name, yet or TPTHNNY for short which paradoxically gives it a name.

© 2011 Jörg Rüppel | Impressum Suffusion theme by Sayontan Sinha