Salomonsson.se
Jun 19 2016

2D platform engine in unity part 13 - Tagging and Rushing

In tonights short coding session I added two new components.

A tagging system for entities. Unity already has a way to add a tag to gameObjects, but for me “tagging” means being able to add several tags to an object, which Unity does not support. Something can be tagged both as NPC AND a Human.

Second, I added a new (very simple) AI-component that will rush towards player or NPC if standing in front of it. It utilises the tagging system to get a list of all entities tagged as either “player” or “NPC”, check direction and distance, and start charging!

As you can see, the Hedgehog speeds up if either NPC_Joey or the player is in front of it (although the graphics does not flip correctly yet).

Jun 11 2016

2D platform engine in unity part 12 - Patrolling AI

A while back my friend Steve asked me if he could use my old platformer engine for a game he wanted to do! I was very happy to see it come to use!

The engine is open source btw, and can be found here: https://github.com/Tommislav/unity2dPhysics.

But tonights short coding session involved a fix to the physics system to detect when someone is “over a ledge”. The hedgehog enemy in the image above will switch patrol direction if either hitting a wall, or getting over a ledge.

The guy with the white hat is my old AI, who just moves in random directions for a random amount of time. In my last post he was a red box. Steve has drawn and animated him quite nice!

Mar 05 2015

2D platform engine in unity part 11 - Wall jumping

Today I made the following new features

  • Moved the project over to the newly released Unity 5
  • Played with a particle system running as part of the background
  • Started on wall jumping feature (based on public demand), this is not all done yet though!
<< Click to play >>

Moving the project over to Unity 5 went pretty painless, unless the collider offsets were messed up on my platforms, meaning I had to adjust all of them. Luckily the level isn’t that big, which meant it only took a few minutes. If I had turned the platforms into prefabs (which would have been the right thing to do) this operation would only have taken a few seconds.

And I’ve been playing with the though of adding some background particles. I wanted some slow/smooth/subtle background noise. Well… I don’t know what to think of it. I could have spent the entire night tweaking it. But instead I moved on to the wall jumping part.

I have to say I’m very satisfied with the gliding part (just need to add some particles while slowly sliding down the wall), but the actual jumping-part is horrible. But that’s something I’ll have to fix another nigth. Now it’s time for bed.

Mar 02 2015

2D platform engine in unity part 10 - Big level

Today, with the platforms finally fixed, I started expanding the level. I’m not sure how Unity optimizes what goes on behind the scenes, so the only way to figure out is to try it out. Also, it’s a good way to find out what features I feel like I’m missing. Like pick-ups, or enemies that or obstacles that will hurt you. It also gives me some room to really try out the features I already got. Like the treadmills for example.

<< Click to play >>
Mar 01 2015

2D platform engine in unity part 9 - Fixing platforms

I haven’t been coding much (on my game, that is) for the past two months. Instead I’ve spent a lot of time watching the Handmade Hero stream. It’s really fascinating and I highly recommend it, but I’ve been having some code cravings lately. So I decided to go back to my platform engine again.

Last time I coded on it I left it in a rather poor condition, with some buggy moving platforms. The problem is that the Character2dController reports to the platform that it has collided with it, and the platform keeps track of the colliding entity and reports back its change in movement to the Character2dController. But the platform only held a single instance of the colliding object, meaning it messed up if more than one entity intersected it. The problem was solved as easy as changing the variable to a list.

Something that is very useful when debugging this kind of graphical application (with several entities with different state and values) is to be able to easily read state data. For this I’ve added debug output above each Character. This debug is a stand alone component, so I could easily add it to the moving platform as well, to verify that the list of colliding GameObjects counts up and down as expected.

Apart from this bug fix there’s no new features, so I don’t provide a new build today.

Jan 15 2015

2D platform engine in unity - part 8

Last time I promised a bigger world, and starting with animations. It seems I forgot a very important feature: moving platforms!

Todays build features:

  • Moving platforms (still a bit buggy)
  • The moving platform to the left is a Solid platform, you hit your head if jumping on it from below
  • The moving platform to the right is a Cloud platform. You can jump on it from below, and jump through it with down+jump Give it a try

Known bugs (to be resolved):

  • The platform can currently only handle one colliding object. If two or more entities touch the platform very strange behaviour will occur.
  • You can fall through a platform if you fall downwards and it moves upward at larger speeds. And as a side note: I think the game looks better with Unitys debug stuff enabled. Don’t know if you can export the web player with those enabled. I think not… Next time: Platform bug fixes and then performance on big worlds and animations.
Dec 29 2014

custom 2D physics in unity - part 7

First of all, a disclaimer: I have decided to change the name from physics to platform engine from this point. Not sure how much more physics I will implement right now. I think from now on it’s time to focus more on trying to make this into something playable.

Todays build features the following:

  • Bugfix for Treadmills reaction to player
  • FPS Counter (in top left corner)
  • Xbox 360 Controller support (at least on Windows), not sure if it works in web player…
  • NPC:s with same physics component as player. Very simple AI controller

Give it a try

Finally the work started to pay off with decoupled physics. The input is also decoupled, meaning that I could just duplicate the player logic, remove the input script and attach a simple AI controller instead. Now we have a whole bunch of red NPC:s running and jumping around.

I think next step might be to build a larger world to be able to see if performance is doing ok in a larger setting. Otherwise I think that it’s time to start looking into graphics and animations.

Dec 27 2014

custom 2D physics in unity - part 6

Try the current build

News in this build:

  • Refactored character controller system (more on that below)
  • Jump button is now space bar instead of up arrow (conflicted with ladders)
  • Disable gravity while on ladders (was buggy before)
  • Cloud platforms are disabled while climbing ladders (can climb down through them)
  • Down+Jump while standing on a cloud platform and you jump down through it
  • Removed that silly fist

Been some while since I last wrote now. Been doing some other stuff than Unity (some c++, and some updates to Ripple and its level editor). But now it’s time to get back to this little project again.

Something that bothered me a lot in the past was how fast it gets messy when implementing platform mechanics. You need need to keep track of if you are jumping, falling, standing on ground, climbing, ducking, sliding, hurting, attacking. And these states gets intertwined into each other. For example: what is a valid state for a charater to jump? He should be standing on the ground – unless if he’s climbing a ladder… or if he’s hurt or dead. And if the player pressed the jump button and the character lands on the ground without releasing the button then we shouldn’t automatically jump again until the button is released and pressed again.

My very small piece of code had already started to get messy this early on, so for a few days I’ve been refactoring it into smaller, more independant and stand-alone modules. This new version is completely rewritten, and all the previous functionality has been ported. Along with a lot of bugfixes. Still the physics feels a little weird, so some more tweaking is needed. I guess next step is to implement some simple enemies with the same character controller…

Nov 03 2014

custom 2D physics in unity - part 5

Late and tired tonight as well. Just a very short, and very unserious session tonight. Been thinking about using Adobe Kuler (seems to have been rebranded to Adobe Color CC) to get a better palette of colors to use when working with programmer art as I currently do. The result wasn’t very impressive so I guess it needs a little bit more tweaking. I also added a punching fist a la Alex Kidd, but the player doesn’t have anything to hit yet. And the ladders are still broken (will fix that in my next session).

Here’s a short demo of the punching fist

Oct 30 2014

custom 2D physics in unity - part 4

Started on ladders today. But it’s late and I’m really tired so I’ll have to continue some other night. Still happy with the result so far though! This will be awesome once I’ve tweaked it enough.

Try my ladder here

Oh, and by the way: the source code for this can be found here in case you wonder how I did stuff: https://github.com/Tommislav/unity2dPhysics

Oct 29 2014

custom 2D physics in unity - part 3

Been a while since last time I sat with this, but tonight I managed to get cloud platforms working.

So right now I have: Solid blocks, Cloud blocks and Treadmills. Next up: Ladders and Elevators! Planned after that (maybe): Gravity shift. Perhaps slopes… not sure yet.

Try it yourself!!!

Oct 20 2014

custom 2D physics in unity - part 2

Not much to see here yet… still working with boxes

Been working a bit here and there on tweaking the scripts to the way I prefer them. Not really getting any visual progress, more just shuffling code around.

Takes some time since I’m not used to how to work with Unity. I know on a conceptual level how I want my code to work. But it’s starting to feel nice. Soon time to add treadmill-colliders =D

Oct 14 2014

custom 2D physics in unity - part 1

I have found a fantastic tutorial series on 2d in Unity which can be found here (it’s several hours in length). Not only do they build a complete game (instead of the usual one-screen-tutorials most others do), they also tackle complex subjects such as slopes and moving platforms. And they do it with really beautiful code! It’s very obvious that the programmer is extremely talanted. He manages to keep the code very clean and readable, and keep the abstraction levels very well separated. This is one of the best game programming tutorials I’ve seen in a very long time! I highly recommend it!

And after watching through it I started writing my own custom 2d physics. Why? Because I don’t like the built in for 2d games. You cannot do obvious things such as cloud tile collisions for example.

<< Click to play >> (move with arrow keys, press shift to move faster)