UnityLearn – AI for Beginners – The Mathematics of AI – Pt. 01 – Cartesian Coordinates

March 6, 2020

AI for Beginners

The Mathematics of AI

Cartesian Coordinates


Artificial Intelligence for Beginners

Unity Learn Course – AI for Beginners

Cartesian Coordinates

Cartesian Plane

Cartesian coordinates:
used to determine locations in space for any number of dimensions
generally used for 2D and 3D space in games (x, y) and (x, y, z)

2 Main Projection Types: Orthographic and Perspective

Orthographic:
3D space represented by a cube or rectangular prism

Perspective:
3D space that looks like a rectangular pyramid with its top cut off

The Camera size in Unity when using the Orthographic perspective dictates how far the camera sees for the smaller dimension of the aspect ratio. It is the number of units in both the positive and negative direction away from the origin the camera sees, so the smaller dimension of the aspect ratio is double that of the Size value given to the camera. The larger dimension in the aspect ratio is then the ratio multiplied by that Size value.

For example, if the orthographic perspective Camera Size is 100, and you select the aspect ratio of 16:10 for your view, the overall height seen is 200 units (+100 to -100 on the y-axis) and the overall width seen is 320 units (+160 to -160 on the x-axis). The size directly correlates with the y-axis since it is the smaller of the dimensions in the aspect ratio, and then the range for x is determined by multiplying that size (100) with the aspect ratio (16:10 or 16/10).

The viewing volume for this orthographic view is a rectangular prism (completely straight on viewing angle). They expound upon this to show movement on the z-axis does not particularly do anything in a 2D game built around the x-axis and y-axis. Placement can matter however as objects do need to be in front of the camera, and objects can be placed in front of or behind other objects.

SUMMARY

  • Unity Camera Size and Aspect Ratio together exactly determine the number of units for the dimensions of place shown on the camera at a time (especially for Orthographic perspective).
  • Orthographic view uses a rectangular prism viewing space, where a Perspective viewing space uses a rectangular pyramid shape.
  • Cartesian planes and coordinates can be used for any number of dimensions (not restricted to just 2D and 3D)

UnityLearn – Beginner Programming – Creating a Character Stat System – Pt. 05 – Accessing Variables in Our System Part 2 (End of Course)

March 6, 2020

Beginner Programming

Creating a Character Stat System

Accessing Variables in Our System Part 2


Beginner Programming: Unity Game Dev Courses

Unity Learn Course – Beginner Programming

Accessing Variables in Our System Part 2

Leveling Up

They created a LevelUp method within the CharacterStats_SO scriptable object. This accesses the leveling array they created earlier and sets all of the max stat values to those designated in the array for that corresponding level (each level is its own individual array element holding an entire list of all the stats and what they should be set to at that level).

Configuring Your Systems

They added the CharacterStats script to the main Hero character. They created empty gameobjects within the Hero character gameobject hierarchy for the character inventory and the character weapon. They then applied this to the base prefab (to make sure it wasn’t only on a newly created variant of the prefab).

They created a method named SavecharacterData, which uses the EditorUtility.SetDirty method from within the UnityEditor namespace to mark this class itself (CharacterStats_SO) as dirty. This does something with letting the system know the data on this object is dirty and needs to be saved again.

They mention this is not a necessarily good practice because they have a script that is using UnityEditor within a gameobject. This would cause an issue for end users playing the game as it will not run if they do not have Unity installed, since it needs access to Unity files.

They reiterate that your game will NOT EVEN BUILD if you have a script with a UnityEditor reference inside your scene. This reinforces that UnityEditor scripts are useful for building tools for your development team and debugging, but it is not to be used for the game itself.

SUMMARY

  • Use new prefab editor in Unity 2019 versions to work with prefabs
  • The UnityEditor namespace can be very helpful for building tools and debugging, but it is NOT for use for actual gameplay as this will keep the project from building

UnityLearn – Beginner Programming – Creating a Character Stat System – Pt. 04 – Accessing Variables in Our System Part 2

March 5, 2020

Beginner Programming

Creating a Character Stat System

Accessing Variables in Our System Part 2


Beginner Programming: Unity Game Dev Courses

Unity Learn Course – Beginner Programming

Accessing Variables in Our System Part 2

Writing Your Character Stats MonoBehaviour

They simply created the base CharacterStats class (non-scriptable object version). They gave it a constructor that created a reference to the CharacterInventory and had a Start method which initialized all the stat variables automatically if the “setManually” option was not checked.

Using Your Scriptable Objects Methods

They begin to fill the CharacterStats class they just created with many methods that simply call methods from the referenced CharacterStats_SO scriptable object.

Here they also use the UnEquipWeapon method, which has a return type of bool. They use it in the ChangeWeapon method, and have it in the if statement condition. This is interesting as the method here will run as the if statement checks the condition (regardless of the outcome being true or false), then based on the result of this method, decides if it will continue through the if statement block.

The method can be seen here:
public void ChangeWeapon(ItemPickUp weaponPickUp)
{
if(!characterDefinition.UnequipWeapon(weaponPickUp, charInv, characterWeaponSlot))
{
characterDefinition.EquipWeapon(weaponPickUp, charInv, characterWeaponSlot);
}
}

Here, characterDefinition.UnequipWeapon will be performed during the if statement condition check, regardless of the result. The result is determined through running that method, which will then determine if the enclosed if statement block is run or not.

Finally they cover their reporter methods, which are simply methods which when called report back information about a specific variable within CharacterStats_SO. For example, this can just read what the currentHealth of the CharacterStats_SO is.

SUMMARY

  • Scriptable objects can be created as a basis for other classes to use as long as those other classes have a reference to the scriptable object
  • Methods can be called and run from within if statement conditions
  • Reporter methods can be useful for passing read-only information

UnityLearn – Beginner Programming – Creating a Character Stat System – Pt. 03 – Accessing Variables in Our System Part 1

February 12, 2020

Beginner Programming

Creating a Character Stat System

Accessing Variables in Our System Part 1


Beginner Programming: Unity Game Dev Courses

Unity Learn Course – Beginner Programming

Accessing Variables in Our System Part 1

Equipping Weapons

They mostly just created the scriptable object, ItemPickUp_SO, for now so that the base of the EquipWeapon method would work. This method simply adds a designated amount to the character’s base stats.

Equipping Armor

Here they created two separate enums within ItemPickUp_SO to define options for the types of items and options for the types of armor. These enums on the scriptable objects were actually very nice in the editor as they provided drop down lists easily accessible to the designer.

The EquipArmor method in CharacterStats_SO used a switch statement, which goes well with enums. They however performed the same calculations with every single case (adding the different values to the player’s resistances), so I think it would be cleaner to just perform this calculation directly after the switch statement.

Un-Equipping Weapons

Using bool return type methods is useful for equipment systems since you normally want two different events to occur depending on if the player can and successfully equips the item, or if they are unable to equip the item. In their case, they just check if a weapon pickup is the same as the current weapon with the bool field within the bool method UnequipWeapon method. It also checks if there was previously a weapon, and it will destroy the current weapon, set it to null, and return the currentDamage to baseDamage.

Un-Equipping Armor

The UnequipArmor method followed the same idea as the UnequipWeapon method, where it had a return type of bool and returned a value based on whether the item passed in (the item being unequipped in this case) matched the item currently equipped to the player. Since this dealt with armor, they used a switch case again to account for all the various armor types so they could direct the actions at a specific armor slot on the character. This check was similar to what was done in UnequipWeapon again, as it checked if the current slot matched the item being unequipped and then adjusted the current stats (resistance in this case) to the base value and set that item slot to null.

This was repeated for every armor type in the switch case, with the slight difference that they had to change the item slot (specific ItemPickUp field on this particular CharacterStats_SO object). I found it easier to just create a helper method that took in an extra ItemPickUp parameter to determine which ItemPickUp slot was the one being tested and have that method do all the work for that specific slot. This way I only needed that block of code once in the script and only needed a single line for each case in the switch statement.

This ensures everything is uniform (which it currently is according to the tutorial) and keeps the code more organized and cleaner. This may have to be edited if different events are needed for different armor types in the future on unequip, but it’s much better currently.

SUMMARY

  • Enums are very nice on the editor side for scriptable objects since they provide an organized list of options to work with
  • Using bool return type methods can be useful with equip systems
  • Identify repeated code in switch statements to clean/organize code

UnityLearn – Beginner Programming – Creating a Character Stat System – Pt. 02 – Damage, Leveling Up, and Death

February 12, 2020

Beginner Programming: Unity Game Dev Courses

Creating a Character Stat System

Damage, Leveling Up and Death


Beginner Programming: Unity Game Dev Courses

Unity Learn Course – Beginner Programming

Damage, Leveling Up and Death

Encapsulation and Item Pickups

They cover encapsulation again just to explain why they use different access modifiers for different fields and methods within the scriptable objects they are creating to deal with the character’s stats and items.

Stat Increasers

They covered methods that increased the stats of the character. They were pretty basic, where they added values to specific stats and checked if the value would go over the max value to set it directly to max if that was the case.

Stat Reducers

This was very similar to Stat Increasers methods, but with subtraction and checked if the value would go below 0. This made me realize there’s a better way to do this where you check if a value would go below 0 BEFORE actually performing the operation, and then set it to 0 if that is the case. This prevents cases where you make a value negative for any step in the code which could result in weird problems down the road.

Leveling Up and Dying

They took an interesting approach to the level up process. They created a new class within the CharacterStat_SO scriptable object named CharLevelUps. These CharLevelUps objects just contained a bunch of int and float fields for different stats, and would be the amount to increase those base stats upon attaining the next level.

They then created an array of these CharLevelUps objects in the CharacterStat_SO class. You could then see this array in the editor and set a number of them to create (in this case, it would dictate the max level of the player). The designer could then set the individual values for each of these stats for each CharLevelUps object in the array to tell the system how much to increase each stat specifically upon attaining that specific level.

SUMMARY

  • Control your encapsulation when dealing with scriptable objects
  • Make sure to have checks for max and min values when modifying stats
  • Creating new classes that just hold a bunch of fields and then creating an array of those classes can be a very nice modfiable tool for a designer to work with

Unity Learn Tutorials – Artificial Intelligence for Beginners and Intro to Optimization

February 7, 2020

Unity Learn Tutorials

Note: The AI tutorial require Unity Learn Premium to access
Artificial Intelligence for Beginners

Tutorial Series #1 – Link

By: Penny de Byl


Introduction to Optimization with Unity – 2019.3

Tutorial Series #2 – Link

By: Unity Technologies


Notes

These were both tutorial series I saw on Unity Learn that I wanted to note specifically to check out later. The Optimization tutorial is a very small series, so I can look into that rather quickly, but the AI series is a 15+ hour comprehensive class so that will take a large commitment to fully cover.

The AI series however is something I am interested in in general and even covers some topics I am specifically looking into at this time. Some of these topics include: Navigation Meshes, Crowd Simulation, State Machines, and Goal Driven Behavior. This would be very good to follow for my own personal interests as well as possibly being useful for some projects I am looking to work on.

Exploring NavMesh Capabilities

February 5, 2020

NavMesh

Unity Tutorials

Unity NavMesh Tutorial – Basics

Tutorial #1 – Link

By: Brackeys


Navigation Mesh Basics | Unity AI Pathfinding (Part 1) | Table Flip Games

Tutorial #2 – Link

By: Table Flip Games


Use Unity3D NavMeshAgent.Move to customize your navigation control

Tutorial #3 – Link

By: Jason Weimann


Crowd Behaviours on a Dynamic Navmesh in Unity Part 1

Tutorial #4 – Link

By: Holistic3d


Notes

I am looking to explore Unity’s NavMesh to see what features it has to compare with A* Pathfinding, especially my setup I worked on specifically. I want to see the benefits it readily provides over A* as well as see if I can find any useful features that could be implemented in some way into A*.

Tutorials 2 and 4 both are the beginning of series so they can be useful for exploring deeper into NavMesh. Jason Weimann in tutorial 3 is also generally a good source for exploring features more deeply at a more advanced level.

Building Unity UI that scales for a real game – Prefabs/Scenes? by Jason Weimann

January 24, 2020

Unity UI

Scaling and Scenes

Building Unity UI that scales for a real game – Prefabs/Scenes?

Youtube – Link

By: Jason Weimann


Notes

UI is a big part of a lot of games, so it is something that is always good to learn more about. This also deals with using UI that works across multiple scenes, so this could possibly be another route to take when dealing with the new scene management techniques I have looked into.

UnityLearn – Beginner Programming – Creating a Character Stat System – Pt. 01 – Overview and Creating Scriptable Objects

January 8, 2020

Beginner Programming: Unity Game Dev Courses

Beginner Programming: Unity Game Dev Courses

Unity Learn Course – Beginner Programming

Project Overview

General Overview of Creating Character Stat System Tutorials

  • Defining and Configuring scriptable objects
  • Accessing data inside of scriptable objects
  • Setting up helpful debugging items
  • Scriptable objects inside of monobehaviours

Creating Scriptable Objects

Scriptable Objects Explained

Scriptable objects are very effective for creating designer tools.

Systems Map: image showing the separate pieces of the game system and how they all connect, in a web-like design

SCriptable Objects: data containers of which, we can have multiplie instances

  • Defined by programmer
  • Treated as assets in Unity
  • Easy for designers to work with
  • CANNOT be added to gameObjects as components

The basic template for turning a script to a scriptable object is adding a [CreateAssetMenu()] header to the top of the script and having the class inherit from SCriptableObject. The CreateAssetMenu header is what adds the scriptable object as an option in Unity’s own Asset Menu at the top of the screen. This also holds the information on how it is displayed there.

Again, it is important to note that scriptable objects CANNOT be added to gameObjects as components. This is worked around in the tutorials however by adding them as a field to a Monobehaviour that is located on a gameObject.

Debugging the Aggro Radius

This segment mostly dealt with importing the project if you had not already, and doing some folder organization for these tutorials. They also added a bool to control showing aggro radius of enemies.

Building Your Scriptable Object

The first scriptable object they create is called CharacterStats_SO.
The CreateAssetMenu tag used at the top of the script is directly used to store this information in an item in the drop down menus of Unity itself. This is why there is a fileName and a menuName.

MenuName: This will be the string seen in the drop down menus directly. You can also add a “/” to create a chain of drop downs where the item will be located.

The fields used for CharacterStats_SO:

  • bool setManually: this will determine if the stats were created manually, or created dynamically
  • bool saveDataOnClose: this will determine if data should be serialized, or written to disk, on close
  • The rest are just general player stats fields (health, damage, resistance, etc.)

SUMMARY

These tutorials were just the tip of getting introduced to scriptable objects in Unity. The rest of the tutorials in this set will delve deeper into the topic and how to incorporate them into other Monobehaviours.

Unity Event Systems: Introductory Research

December 17, 2019

Unity Event Systems

Basics, Setup, and Tutorials

ADDENDUM: An EVEN MORE AWESOME Event System, by viewers!

Link – Tutorial 1

By: quill18creates


C# Events

Link – Tutorial 2

By: Jonathan Weinberger {ON Unity Learn Premium}


Tutorial 1

They created a public abstract class, Event, that all types of events will just inherit from. This helps keep all your events nice and similar, as well as making sure they all have key methods such as those necessary for registering and unregistering listeners. Since each event type is its own version of this base event, they can each hold a different collection of listeners, which makes sense since you only want certain behaviors to happen when certain events happen (such as a unit dying).

Inheritance and Static Fields in C#

This was an interesting interaction that I did not know it worked this way. When a base class in C# contains a static field, and then you create a new class that inherits from this class, it actually creates its own separate and unique version of that static field (as opposed to their just being one overall static field contained in the base class for all classes that inherit it). This has pros and cons. The pros are that it is helpful when you want the derived classes to be able to hold data only amongst themselves. The cons are obviously that you will have to do something a bit extra if you want something to contain all the information from all derived classes from that original base class.

Tutorial 2

This is just a very basic intro to events in C# I found on Unity Learn premium. This was beneficial to just cover some basic rules of events in general again.

General Events Notes

  • allow us to create broadcast system so other objects can subscribe/unsubscribe
  • need delegate type
  • will cause an error if it’s null when called
  • helps classes be self contained (since they do not rely on communication directly with another object)
  • every instance of an object that wants to be subscribed to the event must make sure that it itself subscribes
  • have inherent security (as opposed to delegates)
  • General Rule: always have an unsubscribe for every subscribe

Final Notes

Unity Learn actually has a ton of information, tutorials, and practice for learning about events, so I will most likely look to follow some of them in the future. They range from a few more really basic ones such as the one I just checked out, to more challenging ones, and even advanced ones that are much more project based as well. I really think this type of system will be useful for my current personal project, so I want to understand it well in an effort to properly utilize it.