Adding Jumping To My Unity 2D Game

Adding Jumping To My Unity 2D Game

My endless runner is shaping up nicely. I added some simple behaviors to the player now. He can jump over obstacles and when one hits him, he gets pushed off the screen. Also the player can land on an obstacle which will stop the running animation and also carry him off the screen. The next steps will be to add some zombies at the left hand side of the screen, so when the player gets pushed offscreen he is eaten by a mob of...
My 2D Sprite Pipeline for Unity

My 2D Sprite Pipeline for Unity

At this point I have the perfect pipeline setup for building 2D games in Unity. I start with a PhotoShop template that has all my sprite layers in it. In this case the player is made up of details (the topmost layer), body and legs. This lets me change the color of any layer on the fly in Unity plus I can switch out between weapons for the body animation as well. Here is what that template looks like: From here I use a script I found on GitHub that exports the layers to pngs. This script is a lot faster than the one that ships with PS and also lets me use the layer name as the file name which cuts out a step of renaming my files. From here I use Texture Packer to combine all the individual sprites into a texture atlas. Then I use the free Texture Packer Unity plugin which automatically converts the texture atlas into sprites whenever you update the file outside of Unity. And when I’m done I get all my sprites cut up and named for me automatically. The best part is that I can continue to update the sprites as I tweak all the artwork throughout my dev cycle and everything stays in sync. Then at the end, I can use Unity Pro’s packer feature to turn all my individual texture atlases into more optimized ones for better performance when I publish my...
Simple Pooling for GameObjects in Unity

Simple Pooling for GameObjects in Unity

Turns out adding in Object Pooling isn’t as easy as I would have hoped. Most of my issues turned out to revolve around not being able to access interfaces directly on GameObjects. Here is a quick sample of my final object pooling running: As you can see I am now spawning objects and each one is created from scratch, assuming there isn’t an instance already in the scene. The key here is that when a GameObject runs off the screen I disable it by looking for the following script: using UnityEngine; using System.Collections;   public class RecycleGameObject : MonoBehaviour, IRecycle {   public virtual void Reset(){ gameObject.SetActive(true); }   public virtual void Dissable(){ gameObject.SetActive(false); } }using UnityEngine; using System.Collections; public class RecycleGameObject : MonoBehaviour, IRecycle { public virtual void Reset(){ gameObject.SetActive(true); } public virtual void Dissable(){ gameObject.SetActive(false); } } Now my Objects imply implement this script and override the two methods Reset and Dissable to achieve the desired results. Now when an object goes offscreen, I request the RecycleGameObject script like so: void OnOutOfBounds(){   var recycleScript = GetComponent<RecycleGameObject> ();   if (recycleScript == null) { Destroy (gameObject); } else { recycleScript.Dissable(); } }void OnOutOfBounds(){ var recycleScript = GetComponent<RecycleGameObject> (); if (recycleScript == null) { Destroy (gameObject); } else { recycleScript.Dissable(); } } To my surprise, looking for RecycleGameObject components also return any component that extends it so now I can easily shut down an object offscreen and my pool class returns any disabled instances from its...
Changing BoxCollider2D Size Based On Sprite Size

Changing BoxCollider2D Size Based On Sprite Size

Now that I have some objects randomly being generated I need to make sure that the BoxCollider2D matches the size of the sprite. I am cheating a little bit since all of my objects are boxes but here you can see how I go from the desk: To the dresser: To the crate: And they are all the same exact prefab, randomly picking a sprite from this array: The code is really simple, it’s actually only a few lines: var renderer = GetComponent<spriterenderer> (); renderer.sprite = sprites [Random.Range(0, sprites.Length)];   // Change collider to match the size of the sprite var collider = GetComponent<boxcollider2d> (); collider.size = renderer.bounds.size;var renderer = GetComponent<spriterenderer> (); renderer.sprite = sprites [Random.Range(0, sprites.Length)]; // Change collider to match the size of the sprite var collider = GetComponent<boxcollider2d> (); collider.size =...
Spawning Random Objects

Spawning Random Objects

Playing around with spawning random objects to go with my new scrolling background technique. I have a collection of different objects the player is going to have to jump over while they try to escape from the zombies. As you can see I have each object randomly picking from an array of sprites, scrolling in sync with the floor texture and disappearing when going out of the camera’s view. Here you can see how the layers are working (I exaggerated the Z index of the ground to make it easier to see. Basically, I have a spawner offscreen and it’s aligned with the ground. Each of the objects has a Rigidbody2D and when it’s created I simply apply a constant force to it so it move in sync with the ground animation. One last thing, I noticed some bad stuttering when the objects scrolled across the screen. To fix this I set the Interpolate setting on the prefab’s Rigidbody2D component in the...
Playing Around With Quads And Repeating Textures

Playing Around With Quads And Repeating Textures

I’m working on a new endless runner example in Unity. Going for as simple of an engine as possible. The solution is to use quads for the foreground and background with repeating textures: I created a little script that will take the quad and stretch it out to fill the screen and I can also control if it repeats horizontally or vertically which is how I did the floor. Then I have another script which manages the speed at which I scroll the texture’s offset so it looks like the background is moving. I can also set the speed of the background and foreground to get a parallax scrolling effect like this: With only a few lines of code and primitive 3d objects I’m able to achieve some impressive...

Subscribe To My Mailing List

Want to learn how to make a game? Not sure where to start? Even if you are a seasoned game maker there is still a lot you can learn from my mailing list. I'll be covering tips and tricks for how to build, release and market games each month.

Simply sign up for my mailing list and also get access to a 50% off discount code for my eBooks and other content. I promise to not spam your inbox!

Join Now