Category Archives: General

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

1 Reply

Procedural Maze Generation W/ Pathfinding

I have a weird obsession with procedural generation. Not just levels, but anything. I think it has something to do with my other obsession, math. Either way, I took the procedural maze generation library that I was working on and decided to see if I could get A* pathfinding up and running on it. After hours of complete failure I decided to see what current libraries are out there. I found Simply A*, which was built for the engine I am using. Works perfectly. I did have to do some modification due to not writing this library myself, but so far I’m happy with it. Due to how the path grid is generated, I had to move all of my procedural map generation into the Awake() function so it would fire first, then generate the grid in Start(). For the enemy movement I dumped all “path” objects into an array and pulled a random Vector3 transform from this array every 5 seconds and pushed to the movement script, which is just an extension of the Pathfinding class from Simply A*. If the enemy reaches it’s desired path point before 5 seconds is up, it will randomly pick another so there is constant movement. You can view my proof of concept below. Keep in mind this isn’t a playable game, just a PoC.

http://xertz.net/ProceduralPathfinding/ProcedualPathfinding.html

Enemy movement script:

using UnityEngine;
using System.Collections;

public class SearchPathAI : Pathfinding
{
    public int SearchesPerSecond;
    public float MinDistanceToPlayer = 0f;
    public float AISpeed = 20.0f;
    public float AIPivotPointYValueOverGround = 0.0f;

    private Transform _player;
  public GameObject randomPositionPoint;
  public GameObject[] allPaths;

    void Start()
    {
        GameObject player = GameObject.FindGameObjectWithTag("Player");
    allPaths = GameObject.FindGameObjectsWithTag("Ground");
    InvokeRepeating("RandomPosition", 1, 5);
        if (player != null)
        {
            _player = player.transform;
            StartCoroutine(FindPathTimer());
        }
        else
            Debug.Log("Error: No gameobject found with the tag Player!");
    }

    void FixedUpdate()
    {

        if (Path.Count > 0)
        {
            MoveSearchingAI();
        }
    if(Path.Count <= 0) {
      RandomPosition();
    }
    }

    //A test move function, can easily be replaced
    public void MoveSearchingAI()
    {
        
    if (Vector3.Distance(transform.position, randomPositionPoint.transform.position) > MinDistanceToPlayer)
        {
            transform.position = Vector3.MoveTowards(transform.position, Path[0] + new Vector3(0, AIPivotPointYValueOverGround, 0), Time.deltaTime * AISpeed);
            if (Vector3.Distance(transform.position, Path[0]) < 0.4F)
            {
                Path.RemoveAt(0);
            }
        }
    }

    IEnumerator FindPathTimer()
    {
        //FindPath(transform.position, _player.position);
    FindPath(transform.position, randomPositionPoint.transform.position);
    Debug.Log(randomPositionPoint.transform.position);
        yield return new WaitForSeconds((float)(1.0f / SearchesPerSecond));
        StartCoroutine(FindPathTimer());
    }

  void RandomPosition () {
    int index = Random.Range(0, allPaths.Length);
    randomPositionPoint = allPaths[index];
    StartCoroutine(FindPathTimer());
  }
}

facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a reply

Dev Blog Time!

Hello and welcome to my space on the web!  I have been making video game prototypes over the last 2 years now and with recently losing my job I’ve decided to start a development blog showing off my projects and documenting my journey through the next year in independent game development.  Feel free to browse around and have a look at my work.

facebooktwittergoogle_plusredditpinterestlinkedinmail