Category Archives: Tutorials & Snippets

Leave a reply

Unity3D – Fez Style Camera Pan

Put the following script on your camera, setup your 4 waypoints on each “side” of your map, press space and there you go! Make sure to make the camera Orthographic to get the desired effect.

using UnityEngine;
using System.Collections;

public class RotateCamera : MonoBehaviour {

  public float smoothTime = 0.5f;
  public GameObject[] waypoints;
  public int currentWaypoint = 0;

  private bool isChanging = false;

  
  // Update is called once per frame
  void Update () {

    if(currentWaypoint > 3) {
      currentWaypoint = 0;
    }

    if(Input.GetKeyDown(KeyCode.Space) ) {
      if(isChanging) {
        return;
      } else {
        isChanging = true;
        StartCoroutine(DoIt());
        currentWaypoint++;
      }
    }
  }

  IEnumerator DoIt () {
    float i = 0;
    while(i <= 1) {
      i += Time.deltaTime * smoothTime;
      transform.position = Vector3.Lerp(transform.position, waypoints[currentWaypoint].transform.position, i);
      switch(currentWaypoint) {
      case 0:
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, 0), i);
        break;
      case 1:
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, -90, 0), i);
        break;
      case 2:
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 180, 0), i);
        break;
      case 3:
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 90, 0), i);
        break;
      }
      yield return null;
    }
    isChanging = false;
  }
}

facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a reply

Unity3D – In-game Tile Editor Framework

I took some time today and wrote a small bare bones framework that will allow you to create tilemaps in game, save them, and load them later. This is very very basic and not fully featured, but it should be enough to get you started. It was a good learning exercise for me utilizing 2D arrays and keeping them consistent. You can find the code as well as a Unity package containing the project at the link below. Enjoy!

http://xertz.net/unity3d-in-game-tilemap-editor-basic-framework/

facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a reply

Unity3D – Calling Coroutines From Static Functions

While rebuilding my messaging system I needed a way to call coroutines from static functions. I have built a static coroutine singleton class that will allow you to do this, as well as have complete control over the coroutine firing ability. The code for this can be found here:
http://xertz.net/static-coroutine-singleton-class/

From there we can easily call coroutines from static functions simply by doing:
StaticCoroutine.DoCoroutine(MyCoroutine());

Enjoy!

facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a reply

Math Is Fun.

I was working out a better way to handle light detection when the player is within range of multiple lights. I decided to dump all values into a giant list but needed some helper methods to pull specific information out of the list to determine the amount of light on the player. Below is a simple class I wrote that will allow you to find the Mean of all values in a list as well as the Average of all values in a list. This is setup for doubles and floats only, you could easily make it work with int’s though.

just call it like:

double meanFloat = MathIsFun.Mean(theListYouWant);
double meanDbl = MathIsFun.Mean(theListYouWant);
float average = MathIsFun.Average(theListYouWant);

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MathIsFun {

  public static double Mean(System.Collections.Generic.List<float> Values) {
    if(Values.Count == 0)
      return 0.0;
    double ReturnValue = 0.0;
    for(int x = 0; x < Values.Count; x++) {
      ReturnValue += Values[x];
    }
    return ReturnValue / (float)Values.Count;
  }

  public static double Mean(System.Collections.Generic.List<double> Values) {
    if(Values.Count == 0)
      return 0.0;
    double ReturnValue = 0.0;
    for(int x = 0; x < Values.Count; x++) {
      ReturnValue += Values[x];
    }
    return ReturnValue / (double)Values.Count;
  }

  public static float Average(System.Collections.Generic.List<float> Values) {
    if(Values.Count == 0) {
      return 0.0f;
    }
    float ReturnValue = 0.0f;
    for(int x = 0; x < Values.Count; x++) {
      ReturnValue += Values[x];
    }
    return ReturnValue / (float)Values.Count;
  }

  public static float Sum(System.Collections.Generic.List<float> Values) {
    if(Values.Count == 0) {
      return 0.0f;
    }
    float ReturnValue = 0.0f;
    for(int x = 0; x < Values.Count; x++) {
      ReturnValue += Values[x];
    }
    return ReturnValue;
  }
}

facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a reply

Unity3D – External Map Editor – Text Based

The biggest downside to Unity3D so far for me has been the lack of mod support for your projects. I wouldn’t have jumped into this game if it wasn’t for Doom WAD and Quake C modding when I was younger. I spent some time today and wrote a script that will read a text file of a specific name and then generate a tiled map based on the strings in the file. This one only supports 2 types of tiles but you can add as many as you want by adding new if/switch statements in the BuildMap() function. This script will read the text file line by line from top to bottom, fill a 2D array with the parsed text data (as INT), then instantiate the GameObjects you have assigned. There was some trickery to get the tiles to line up correctly as you’ll see in the BuildMap() function. This is due to lack of knowledge I have on flipping arrays and/or reading text files top to bottom. You could easily take this to another extreme and build a map editor either in your game or an external one for your players to use. Enjoy!

http://xertz.net/unity3d-text-based-map-importer/

facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a reply

Tutorial – Game State Machine – How/Why You Should Use Them

Like a lot of people, I first started programming because I liked video games. Of course, back when I was younger we made DOS games in QBasic that were nothing more than simple multiple choice text adventures. The code back then was a lot like this:

Print "A wild GOBLIN appears"
Print "(A)ttack. (R)un"
Input a$
if a$ = "a" GOTO 6
if a$ = "r" GOTO 7
Print "You ATTACK the GOBLIN"
Print "You RUN from the GOBLIN"

That may not be exactly how the code was written, but it’s been 20 years since I wrote in QBasic.

Lets examine this code for a moment:
Line 1 Prints a string to the screen
Line 2 Prints a string to the screen
Line 3 Records user input
Line 4 and 5 tell the application to do something based on the input supplied
Line 6 and 7 are the result of the input supplied

Games these days are not much different from this exact scheme. You give the user a situation and a means to work with that situation and then display the result of the player’s choice. It doesn’t matter if it’s a World of Warcraft boss or a wild GOBLIN appearing out of no-where. Now you can imagine writing something as complex as a WoW boss fight using QBasic would be less fun than swallowing glass, but the idea is the same.

pseudocode:

if(bossHealth >= 75.0f) {
    bossState = State.PhaseOne;
  } else if(bossHealth < 75.0f && bossHealth > 50.0f) {
    bossState = State.PhaseTwo;
  } else if(bossHealth < 50.0f && bossHealth > 0.0f) {
    bossState = State.PhaseThree;
  }

  if(bossState == State.PhaseOne) {
    // Hurl a fireball
  }

  if(bossState == State.PhaseTwo) {
    // AOE room with lava
  }

  if(bossState == State.PhaseOne) {
    // Hurl a fireball
    // AOE room with lava
    // Immune to taunt
  }

As you can see a boss fight can be made up of several different states, and the game can change depending on which state the boss is in. I used a very simple concept here just to explain what exactly a game state machine can do.

Lets take a look at a real example of a game state system. We will be using Unity3D and C# for this example but the idea should be portable enough to use whatever you are familiar with.
(more…)

facebooktwittergoogle_plusredditpinterestlinkedinmail

1 Reply

Unity3D – In-Game Debug Console and Messaging System

I have added two new pages to the Tutorials and Snippets section of this page. One is an In-Game Debug Console and the other is a Messaging System. Both are designed to work specifically with the NGUI library but you could easily modify them both to work with any other framework or even a simple GUIText.

In-Game Debug Console

Messaging System

facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a reply

I Hate Quaternions – Enemy Waypoint Patrol and Chase Script

Below is a link to a snippet of code I built this morning. Putting this code onto a GameObject will cycle through an array of waypoints and smoothly walk towards each one after the other until the current waypoint variable is larger than the length of the array, at which point it will reset the current waypoint back to value 0 of the array. If the boolean isAlert is true, it will chase a the Player. This script will also smoothly Slerp the rotation to face the object it is moving towards (using Quaternions).

Check the script HERE.

facebooktwittergoogle_plusredditpinterestlinkedinmail