Monthly Archives: May 2014

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

Weekend Prototype 2: HideHackSteal

I have a love of stealth type games, as if that wasn’t evident already. I saw this other game made in Unity3D called Light and fell in love (even though its not out yet), so I decided to see how quickly I could mimic the mechanics. 14 hours later and here is HideHackSteal. Enjoy!

Controls:
WASD or Arrow Keys to move
F for flashlight
E to use doors, hack terminals, knockout guards.

HideHackSteal

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