AStar for 2D Pathfinding in Unity

June 8, 2019

Unity 2D Pathfinding

Using AStar

Youtube – 2D PATHFINDING – Enemy AI in Unity

By: Brackeys

This tutorial shows how to setup the general AStar package in a 2D Unity project. It goes over the basics of setting up an initial grid and getting a basic enemy AI that will make the enemy follow a target. It also has an example to setup your own simple AStar enemy AI script using AStar’s Pathfinding package.

This tutorial also cover the basics of setting up an enemy object with a graphics child object, flipping that graphic when the enemy should be going another direction, and just moving an enemy with physics and forces in general, instead of just updating position.

Tower Defense Tutorial – Brackeys – Ep. 01, 02, 03

January 23, 2019

Tower Defense Tutorial

Episode 01 – 02 – 03

Youtube – How to make a Tower Defense Game (E01) – Unity Tutorial
Youtube – How to make a Tower Defense Game (E02 Enemy AI) – Unity Tutorial
Youtube – How to make a Tower Defense Game (E03 Wave Spawner) – Unity Tutorial

By: Brackeys
Ep. 01

Created all the nodes in grid that make up the map. Created the ground out of scaled cube objects as well. Added start and end location as simple cubes for now.

Ep. 02

Creating the enemy AI with waypoints. A static transform array was created to contain all of the locations of the waypoints. There was an issue where we got an “index out of range” error because the enemy was looking for another waypoint to go to when it was supposed to be destroyed. It was being destroyed, but since that can take the computer some time to do, it was continuing into the next lines of code before the object was completely destroyed. To solve this, a return; line was added in the if statement for destroying the object to ensure that process finished before doing the rest of the code.

Ep. 03

This was creating the wave spawner for the enemies. The spawner needs a timer to control how often and when it releases waves. The approach used here was a countdownTimer that was reduced by time.deltatime in the update method.

Enemies however were being spawned directly on top of each other all at once for each wave. Coroutines were the choice to resolve this issue. Coroutines are useful to run functions separately and side by side with the main functionality of the code.

Recap – Week of January 13 to January 20

January 20, 2019

Recap – Week of January 13 to January 20

Thesis Work

Continue to search for some research on the general term of “game mechanics” to solidify the use of its definition as well as determine the components that make for interesting variable mechanics.

Terminology to Look Into – Programming

Structs

Structs were a big part of the field of vision tutorials, and I still don’t fully understand how to utilize them. They are structured data containers that can hold many types of variables to use over and over, but I’m not sure if there’s more to them to help utilize them, especially for games. Could help to look into a tutorial where there are characters or enemies with stats (i.e. RPGs), or a pokemon-like tutorial (well, also under RPG).

Field of Vision Recap

Youtube – Field of view visualisation (E03: stencil shader)

By: Sebastian Lague

To start, I just wanted to include that there is a 3rd part to this tutorial series I would like to get to. The field of view (FoV) tutorial was really useful for learning several aspects of Unity programming overall. I learned more about creating Editor elements for making your scripts into more designer friendly tools. I was still getting some weird interactions with the GetAxisRaw command I need to look into. This was also a nice refresher on generating meshes within script, but the extra twist of tying it in with raycasts was something interesting, new and useful.

Learning Foundations of Unity Shaders

My Blog – Learning Foundations of Unity Shaders

This was my first time learning about shaders and getting into the Shader Language and scripting anything, so I was introduced to a lot of new concepts and terminology. I covered it pretty extensively in the blog post, so I just relinked it here. These tutorials made by Unity in several steps are usually very informative conceptually as well as programmatically.

Quadtree and Octree

These data structures for use in games seem like they could be very useful for some things I am interested in since they seem like they can be pretty computationally beneficial if you want to have wide reaching field forces interacting between many different objects. I will still need to do more research into this, as well as find more tutorials as they were hard to find.

Unity Basic Enemy AI Patrolling

January 12, 2019

Unity Basic AI

Patroling

Youtube – PATROL AI WITH UNITY AND C# – EASY TUTORIAL

This tutorial just sets up a basic patrolling AI where empty gameobject waypoints (called movespots in the tutorial) determine the enemy’s movement. It used an array of positions which contained all the possible waypoints, then randomly selected between them and moved the enemy there. This movement isn’t particularly useful for much, but it showcased a basic waypoint system well enough.

This tutorial actually attempted to correct the issue I brought up from the previous tutorials in my last blog where they were using if statements that ended when a position exactly equaled another position, which was very susceptible to math errors. They even used my quick (but still not great) work around of using distance and “less than” to set a small acceptable range to account for these small math errors.

Unity Basic Enemy AI Following and Spacing

January 11, 2019

Basic Unity AI Tutorials

Player Following and Spacing

Youtube – AI TUTORIALS WITH UNITY AND C#
Youtube – SHOOTING/FOLLOW/RETREAT ENEMY AI WITH UNITY AND C# – EASY TUTORIAL

By: Blackthornprod

This first video used the Unity command “MoveTowards” which I thought was giving me some issues so I just created my own follow AI using simple vector math (this was a good opportunity to brush up on vector math again). It turned out everything was fine and both ways worked, but I like really knowing the math behind what my objects are doing. The addition of a stopping distance so the enemy stopped moving towards you at a certain distance was a nice extra touch that’s very easy to add with a simple if statement tied to distance between player and enemy.

The second video was a bit more interesting by having the enemy have 3 ranges: vary far away, away, too close. At very far away, it moved closer. At away, it stayed in position. At too close, it would move away from the player. This could be a good setup to test setting up a small state machine to get practice using those with AI. I could have each of those be a state and use an enum switch case setup to decide which action the enemy should use.

This was also a nice little refresher on instantiating projectiles, although the logic for them was strange and bad. The projectileScript class would get the position of the player and then end at that position. This was then “remedied” in the tutorial by having it destroy itself when the projectile position was equal to that original target position, but math errors were preventing them from EXACTLY matching the value for me, which kept them from being destroyed. As a quick solution, I just changed the if statement to occur when the distance between the projectile’s position and the target position was less than 0.1, so it just had to be pretty close, which allows for some math errors. I imagine this is not a great solution either though as calculating distance in Update for a projectile constantly sounds too aggressive computationaly.

AI Project Research – Looking for Inspiration

January 9, 2019

AI Project Research

GDC 2018 – AI Wish List: What Do Designers Want out of AI?

By: Raph Koster, Dave Mark, Richard Lemarchand, Laralyn McWilliams, Noah Falstein, and Robin Hunicke

GDC 2018 – AI Wish List: What Do Designers Want out of AI?

I’m bringing back a video for reference to try to come up with an AI intensive project. There are lots of quotes and the notation is very note-y, just using it to keep track of points of inspiration.

Laralyn mentions that she wants AI to be able to acknowledge that what the player is doing is unique. The way they are playing is differentiating from what is expected, and it should be noted by surrounding characters. Could we apply a type of “physical constraint” concept to this to help accomplish this goal? Physical constraints mathematically map out what an equilibrium should be, and then apply forces/actions to bring things out of line back to equilibrium. Could we use this concept to perform AI actions when it acknowledges a player diverges from what is expected, the “equilibrium” of the player experience?

From Raph Koster, “…building characters as props that are primarily reactive, and if we want to exploit AI, they need to have their own inner lives, out of which this stuff rises organically.”

”Heartificial Intelligence” – giving NPC’s empathy; Have deeper version of Sim system. Users more interested in “broken” characters with flaws. A dog that is scared of people now and is harder to train. Problem with Sim system was that every object had to echo out how it should affect an NPC, and they all needed to interact with each other so adding things to the system was very difficult(?).

Time Stamps: (0:49:40 – 0:51:18) : AI can be recursive and add detail. “…Can you do that but in a way that is data rich, instead of just going for ‘no let’s plot thousands of trees’, how about when I go look at that tree it’s way more detailed. Now there’s an ecosystem in that tree. Now there’s a burrow in this one and the gophers live under that, and there’s a woodpecker in that one. That’s the kind of detail we will never ever be manually able to do. We just can’t afford it, it’s ridiculous. And it’s exactly the kind of intelligently constructed, realistic, responsive environment. You want the woodpecker to know there’s a gopher. I’m not talking just shallowly, again, it’s not just splatting the textures. Can we actually create a data rich environment? … it’s a fractal or segmented thing. We don’t need each tree to know about each tree. But we could do something really interesting within the tree… each one could have variations.”

Solaris example – it builds things from your memory; makes you miss being on earth

”…go to a foreign planet and you find an object, and when you look inside that object there’s more objects, and you look inside those objects; and then come up with a design of mechanics that are interesting from the exploratory perspective that’s very similar to when you’re a child. When you wander into your backyard, and then the woods there and you find a stream then you look in the stream and you see the tadpoles and then you poke the tadpoles. Think of it from a mechanical perspective, not an aesthetics perspective, and then you get the dynamics that are so interesting.

Decentralize player, other things go on without the player. Make the player feel proud of something else. Have an AI do something the player didn’t expect it to do. Going off of this: Have an AI that the player needs to “teach” it how to do something with their actions. Similar to “Clumsy Ninja”.

Tactics Movement Tutorial (Cont.) – AI and A*

January 4, 2019

Tactics Movement – AI

AI with A* (A Star)

Youtube – Unity Tutorial – Tactics Movement – Part 6

By: Game Programming Academy

This part of the tutorial implements A* for use as the AI for NPCs. Most tactics games would generally involve some type of action phase, but this tutorial finishes with just getting the movement setup.

NOTES

The tutorial’s breakdown of A*:

  • processing the open list by finding the lowest f cost
  • put it in the closed list
  • checking for the target
  • then process adjacency and see if tile is in open list, closed list, or neither (in which case it is added)

Like in many tactics games, the NPCs will still show all of their selectable locations to move before actually selecting one.

For checking distance, tutorial just uses “Vector3.Distance”, but they suggest using the squared magnitude value since it is easier computing because Distance uses square root. Used for setting up simple AI searching algorithm to find the closest thing. It is a simple foreach loop that goes through an array of gameobjects checking the distance between the gameobject itself and each of those individual objects. The distance as well as the object is stored, so whenever a new lower distance is found, the object will be replaced, otherwise, the same object remains there (as that indicates the currently checked object is farther away).

The tutorial tries to keep the TacticsMove script as a generalized class that holds all the helper methods for use in the other classes.

A* uses two lists: an open list and a closed list. A* can also use a priority queue, but C# doesn’t have a built in priority queue. The open list is any tile that has not been processed, and the closed list is those that have been processed. It will finish when the target tile is added to the closed list.

Since we’re using tiles, we could use the “Manhattan Distance” instead. Manhattan distance may work better particularly with arrays though so tutorial sticks with Euclidean distance.

Since we’re using pathing to walk up to a unit, not on top of, the tutorial just decides to follow the A* path and then stop one node before the end (which would be where the target unit is located).

TERMINOLOGY

Manhattan Distance:The sum of the absolute differences of the Cartesian coordinates of two points. This represents the straight line nature of moving around on tiles in this case which is a grid-like layout.

Priority Queue: Like a normal queue but each element within has a priority value associated with it.

TODO

Suggested by the tutorial, program NPC for case where there is no target in range.

May 20th, 2018

Procedural Content Generation in Games

Galactic Arms Race (GAR)

Youtube Link – Evolving Particle Weapons in Galactic Arms Race | AI and Games

by: AI and Games

AI and Games briefly describes how GAR uses evolutionary algorithms to design weapons that the player likes using. The player takes the place of the fitness function to determine what is “good”. Techniques such as CPPN and cgNEAT are lightly touched upon and offer topics to be further explored.

May 7th, 2018

AI Wish List from GDC

AI Wish List: What Do Designers Want out of AI? – GDC Vault

GDC Talk with Raph Koster, Dave Mark, Richard Lemarchand, Laralyn McWilliams, Noah Falstein, and Robin Hunicke. They go over what directions they would like AI to move in in the future to further good game development.

Some Topics Discussed:
  • Content Creation: Don’t focus on procedurally generating a tree, or a forest of trees. Generate interestingly varied trees that have other interactive elements within/about them to create a much more detail-rich environment that could not be created by human hands in any practical way. Create a world, with a forest, with a pond, with tadpoles that you can interact with.
  • Feelings to Invoke: Feeling Proud for something. Have the player feel proud that they were able to guide something else to perform an action on its own.
  • Create systems of interactivity that decentralize the player. Make the player understand that their actions are upsetting a balance. Make the player see that their actions affect other lives, even NPC lives.
    Example: A player arrives at a procedurally generated world with procedurally generated pink moles that have built up an entire ecosystem and have a history. These moles will then react to the player arriving and their actions/decisions, but also carry on with their own lives, which they would have done regardless of whether a player ever arrived or not.