Field of View Tutorial E01 – by Sebastian Lague

January 14, 2019

Field of View

Tutorial – E01

Youtube -Field of view visualisation (E01)

By: Sebastian Lague

This tutorial sets up a field of view (FoV) for a character. This FoV gives basic sight to a character. This sight has a radius and a viewing angle, and it is blocked by obstacles. This FoV also has a global setting that determines if it follows the character’s rotation or not. There are also editor elements that make it visually very clear what is happening by drawing out all of these factors.

Unity angles: Normally we start with 0 degrees to the right of the circle, but Unity starts with 0 at the top of the circle (where you’d normally have 90 degrees) for its angular math calculations. Since sin (90 – x) = cos x, this just means we swap sin and cos in Unity math.

PROBLEM

I had an issue with GetAxisRaw command not working for mouse inputs with the axes being “Horizontal” and “Vertical”. It just constantly returned a value of 0. This was ok with keyboard inputs however.

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.

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.

Tactics Movement Tutorial (Cont.) – Turn Manager

January 3rd, 2019

Turn Manager – Tactics

Turn Management

Youtube – Unity Tutorial – Tactics Movement – Part 5

By: Game Programming Academy

This part of the tactics game tutorials focused on creating the turn manager. An NPC character was created to represent another team. Even though there are only two units, the tutorial design deals with entire teams. The turnManager used static variables and methods because it should be very accessible and will act as a sort of global since there should only ever be one.

TERMINOLOGY

Subscriber Pattern: the tutorial mentions using this method to add units to the turn manager queue. This was tied to the idea that when a unit comes “online”, it should be able to add itself to the proper list in dictionary.

Dictionary: more uses of this in turnManager system, so I still need to look into this to see how they work.

NOTES

Will need to add method to remove unit from queue when it is defeated, which will then also lead into another method that will be needed for dealing with what happens when an entire team is defeated.

There was a method named “EndTurn” in the TurnManager script and the TacticsMove script. I am not sure if this is a bad practice or something that is generally done without consequence.

PROBLEMS

My units could move through each other or even on to the same space. Going back to tutorial video “Tactics Movement – Part 2” I saw there was an adjacencyList.Clear(); command I was missing in my Tile Reset method. Adding this back in fixed the issue. I wasn’t sure if this was removed later in the tutorial by the tutorial itself for bug testing when player movement was buggy. After checking video “Tactics Movement – Part 4” in the debugging segment, this does not appear to be the case, so it does seem that I just missed that line of code the first time.

Tactics Movement Tutorial (Cont.) – Jumping and State Machines

December 23rd, 2018

Tactics Movement (Cont.) – Jump State Machine

Jumping in Tactics Game

Unity Tutorial – Tactics Movement – Part 4

By: Game Programming Academy

This part of the tutorial focused a lot on the jumping aspects of the player movement. The movement when the unit jumps up or down to different elevation tiles should operate differently than when it just moves around on equal elevation tiles. A state machine is created to deal with all the various aspects of a jump, as well as the possibility of jumping upward or downward. The state machine however appears to have significant bugs in it that will need to be worked out.

Terms

  • Stack: Pop, Peek
  • Protected class

Problems

This part of the tutorial had several bugs that could possibly occur, which the creator of the videos also encounters. The tutorial video showed an issue where the unit would just float in a straight line when it should come down, but I have yet to have that occur. Most bugs I saw had the unit not perform a jumping action, such as just instantly teleporting onto the top of a block instead of performing its slow jump animation. My errors seem to come from the jump state machine not updating properly consistently, where the tutorial video errors seemed to be coming from an issue with the forward-facing vector of the unit not being perfectly lined up at all times (the floating issue might also just be a strange state machine update issue).

Lessons Learned

Locomotion is a good term to use as a commenting term in coding to determine where movement of an element is truly occurring. Can be helpful for figuring out timing for adding animations.

State machines can require a lot of debugging. This will just require consistently calling out what state the object is in at what times, and checking if all of the flag changes are occurring properly.

Learning Tactics Movement (Cont.) – BFS and Collections

December 22, 2018

Tactics Movement (Cont.) – Programming Collections

Breadth First Search (BFS) and Using Programming Collections

Unity Tutorial – Tactics Movement – Part 3

By: Game Programming Academy

This section begins to finally create the breadth first search algorithm for creating list of available selectable tile options. This section relies heavily on collections type elements in C# programming which I will need to delve into more.

Notes

Look into breadth first search, stacks, queues, and lists.

Stacks: Used to add things to a collection in a certain order and use/apply them in reverse order. Stack uses command “push”.

Queues: Use “enqueue” to add to queue and “dequeue” to remove/pull from the queue.

halfHeight was a variable used to find the center point of the game object in conjunction with the “bounds” and “extents” values of the collider. This is used when positioning the unit on a tile to make sure it is on/above the tile.

Lessons Learned

Had an issue where the mouse clicking functionality wasn’t working. It turned out that the main camera just was not tagged “maincamera”. Make sure to check camera labeling and that it matches what you call out in code when dealing with functions that deal with screen space and the camera.

Creating a Basic High Score System in Unity

October 13th, 2018

How to Make a High Score in Unity

How to make a HIGH SCORE in Unity – Brackeys

How to make a HIGH SCORE in Unity – Youtube Link

This youtube tutorial from Brackeys shows the bare bones way of getting a high score system started for a game in Unity. It uses the built in PlayerPrefs function to save data to the user’s system.

PlayerPrefs.SetInt and PlayerPrefs.GetInt were used. Playerprefs.SetInt lets you set a key value (which is basically a name for this value when you want to find it) and the actual value to set to this key at the time. Playerprefs.GetInt will return the value stored at the key value given to it, or if nothing is stored within that key location, it will return the default value, which is the second variable value you set when using PlayerPrefs.GetInt.

Saving Data in Unity: PlayerPrefs

Saving Data in Unity: PlayerPrefs – Youtube Link

Youtube tutorial by Board to Bits Games going over the basics of PlayerPrefs in Unity. This shows limitations of PlayerPrefs as something that is mostly useful for storing smaller amounts of data for the player on their system. This also showed a good practice to follow when using key value pairs: set your key as a string variable so that you can call that key variable name every time you need it instead of hoping you type it in exactly correctly every time.

Lessons Learned:

I wanted my score and high score text objects to contain text other than solely the score value (in this case it was a simple “Score: ” and “High Score: “). This did lead to a couple issues throughout the debugging process where I realized I didn’t add the extra text when changing/resetting score values, so the extra text would be missing and just the number would be shown. For larger projects, it may be safer and more consistent to make a separate small method for setting a score and/or high score that will add that extra text every time when called (removes a copy/paste step of doing similar actions throughout the scoring script.) And again following a main point from the Board to Bits video, set your key names as string variables within the code so they are easier to consistently call throughout your program.

Experimenting with Unity: Player Movement – Rotation

August 1st, 2018

Experimenting with Unity: Player Movement

Player controlled by rotation about a point

Vimeo – Video of Rotational Movement

I was experimenting with controlling a player agent solely through rotational forces. The first style I looked at was not a force in the physical sense as it just operates on moving the player at a constant rotation speed. This is dictated by the placement of a rotation pivot which the player can place to indirectly move the player agent.

To add an extra amount of control, the player can also press a key in order to reverse the direction of the rotation (switch between clockwise and counterclockwise). This combination of features led to some interesting movement patterns. One of the first things that jumps out is the fact that the movement being dictated by a constant rotation speed still leads to varying velocities by the player agent as this will vary drastically with the radius between the pivot point and the player object. Large pivot distances can lead to dramatic speeds, but will lead to the player covering a larger area.

Notes to remember:

I was having an issue where the rotation was not perfectly making a circle in the game view. It turns out the scripts were correct, but the camera needed to be set to orthographic so the camera was just viewing it at an angle. Remember to check both the editor view and game view closely to help determine camera-based issues.

Using cursor inputs in Unity is a bit awkward. It has been done plenty of times so the basic scripting is available on Unity’s main site, but the process of creating a plane, raycasting, and converting between screen and world space is a lot to keep track of. This might also be something to look into if mouse inputs are acting strange.