Building a 2D Platformer (Harambe-themed!) in Unity

Run Harambe, Run is the second mobile game I plan to release to iOS and Android. At the time I started this article, it’s in a closed alpha release. Leave a comment if you want to get in on the early testing if it hasn’t hit App/Play stores by the time I hit publish.

It’s a 2D platformer. You play the part of Harambe trying to avoid children on his escape from the zoo, the city, and eventually returning to the jungle. Hilarious, right? Not quitting my day job anytime soon.

After Super Bouncy Box, I had a much better understanding of the release process (I’m sure there are still a lot more moving parts,) and I could focus more on game mechanics and using Unity in a more streamlined fashion. You can read this as: level building is super fun once you have a bunch of sweet prefabs built.

So, this is RHR:

Graphics

The vast majority of the graphics are from a couple different designers from Fiverr. I know enough Photoshop to be dangerous, so I decided to let people with actual, illustrative talent do the heavy lifting. The animations for Harambe and the children are each five frames. One designer actually shorted me two frames for Harambe’s running animation (so it looks a little gimpy,) because I was too dumb to see/test it before accepting delivery. Fortunately, he provided a high resolution version of all Harambe’s body parts so I could pose him in the future–a good deliverable to request in the future! Unfortunately, I didn’t think hard enough for the child sprite animations when I asked for “walk” and “fear” animations. I neglected to request a “walk and fear” animation, so that’s why I decided to make the children stop in front of and behind Harambe at a specific distance and then switch to their fear animation.

The same designer did the parallax backgrounds (one background, one transparent foreground) for all three levels in addition to some similarly-styled platforms, background objects, etc. The biggest problem I’ve had with the backgrounds is the presence of a platform in the foreground image:

So, even as I want the player to see as much of the foreground as possible (trees, rocks, fences, benches, etc,) I always have to putĀ something in front of the foreground platform. I didn’t think far enough ahead for an open air platform, so wherever the ground isn’t present, I added water–the same death effect, but I had to create new materials for each of the three different levels water, so they were at least somewhat different on a level by level basis.

Some Code

Making the various platforms throughout the game was pretty fun, and they really weren’t a ridiculous amount of code.

Here’s an example of a “pivoty” platform section in level two.

It’s a couple simple sprites: one from the aforementioned designer, and a little circle, pivoty thing from the Kenney assets. At design time, the prefab is:

  • An empty parent game object
    • The platform
      • Sprite renderer, so the platform is visible and looks cool
      • Box collider 2D, so Harambe can stand on it
      • Script component (code below)
    • The pivoty thing
      • Sprite renderer, duh
      • Circle collider 2D (this never moves)

For completeness’ sake, the platform’s child objects have small colliders with slippery physics material.

The code for this platform is easy. When Harambe touches the platform, we give the platform a RigidBody2D component and a mass so it can start wobbling and eventually fall. Unless Harambe were to land in the exact center of gravity, the platform should wobble and eventually fall.

public class PivotyPlatform : MonoBehaviour {

    public float massForPivot;

    Rigidbody2D rb;

    void OnCollisionEnter2D(Collision2D col){
        // when Harambe collides
        if(col.gameObject.tag == "Player" && rb == null){
            // give the platform a rigid body so it can move
            rb = gameObject.AddComponent<Rigidbody2D>();
            // allow custom mass
            rb.mass = massForPivot;
        }
    }
}

TheĀ massForPivot value allows a custom mass for the newly rigid platform in order to create a more or less difficult challenge. For example, a larger mass (compared to Harambe’s mass) would make the platform slower to initially start wobbling in an extreme manner, and Harambe would have a hard time stopping this momentum once started. Conversely, a comparatively smaller mass could be like dropping a ton of bricks on a seesaw. Most of the other platforms have similarly simple code.

Leave a comment, tweet me, whatever–if you want to help with testing and get in on the alpha action!

Leave a Reply