Pulley Wheel Positioning System – Weighted Ranges for Positioning

October 6, 2019

Pulley Wheel Positioning System

Video Demo of Wheel Positioning System

Demo – Link

By: me


General System Needs

My pulley system needed to create a rope with objects attached to it, as well as objects which act as the pulley wheels which will support the rope. The positioning of these wheel objects must be in line with the rope, but below it. Using the HFF system, the rope gets generated and then falls, draping itself over any objects below it.

Keeping those requirements in mind, I also wanted the system to be able to generate a varied amount of these pulley wheels and place them in varied positions below the generated rope. Having the option for multiple wheels became more important when I noticed that sometimes ropes with heavy objects attached can break through rigid body wheel support objects, but having more (therefore more support) can sometimes alleviate this issue.

So to summarize, I wanted a system that could choose a varied amount of positions along a randomly determined line, with extra rules dictating how far these positions should be from the ends of that line, and how much of a space buffer should be given to each position (so when an object is instantiated at one of these positions, it does not collide with another instantiated object).

Starting Off

To keep the system simpler for now, since the system needs to fall into place anyway, I was specifically randomly generating the rope on the xz plane at some height y. The wheel positions are then generated on basically the same line, but some distance below that line (to ensure the wheels are within the rope line, but below for the rope to fall on them).

Adaptive Range System

The difficult thing with this position range system is that choosing a position for an object removes a specific range around that position to account for the space needed to instantiate an object at that position. Because of this, I needed a system that could update the range options everytime a position was chosen. I came up with a system that could determine how to update the ranges everytime a position was chosen, and could also determine if a range needed split into multiple.

The first range is the easiest to determine. It is just the full range of values between the beginning and the start of the wheel positions, with a buffer at each end factoring in the value for distanceFromEnds that we want to give to keep objects from instantiating too close to the ends.

With this first range created, we choose a random position within this single range. After that is when the adaptive range system comes in. Depending on the position selected, there are several possible options to account for the options that have been removed from the range:

  • If the position is sufficiently far away from any other range bounds, a range is effectively split in two.
  • If the position is very close to both the min and max bounds of the range, that range is fully removed.
  • If the position is very close to either the min or the max bound of the range, that range can just be modified.

Weighting the Ranges

One issue with a system that creates multiple ranges is that these ranges can be different sizes. So if at some point in the process I am randomly selected a range first, to then select values from, each range will inherently have the same chance of being selected, regardless of its size. To remedy this, I used a weighting system.

Each range has a weight in the range (0, 1] (The minimum bound is exclusive, as nothing should ever have a weight of 0, but the maximum bound is inclusive since if there is only 1 range it is the only option) representing how much its individual range covers amongst the total coverage of all the ranges within a list of ranges. This is easiest to explain with an example:
We have 3 ranges: (0, 2] (2, 5] (5, 10]
The total range coverage is: (2 – 0) + (5 – 2) + (10 – 5) = 10
The individual weights for these ranges are then:
Range 1 (0, 2]: Weight = (2 – 0) / 10 = 0.2
Range 2 (2, 5]: Weight = (5 – 2) / 10 = 0.3
Range 3 (5, 10]: Weight = (10 – 5) / 10 = 0.5

So when we go to select a range, we randomly roll a number in the range of (0, 1] and use this to determine which range to pull a value from. This works by checking if the rolled value is less than or equal to the first range’s weight. If this is satisfied, that is the range to choose from. If not, it moves to check the next range’s weight. To fit this within this (0, 1] range, each time a range is not used, its weight is added to a pool to add to the next range’s weight. This helps move the weight check along the range of (0, 1] with many values significantly below 1.

To follow the previous example to show this in action:
Our randomly rolled value to select a range is: 0.75
General Formula:
Check range n: Is rolledValue <= (rangenWeight + weightCheckPool)? No, add rangenWeight to weightCheckPool
Check range 1: Is 0.75 <= (0.2 + 0)? No, add 0.2 to weight check pool and move to next range
Check range 2: Is 0.75 <= (0.3 + 0.2)? No, add 0.3 to weight check pool and move to next range
Check range 3: Is 0.75 <= (0.5 + 0.5)? Yes, use this range to select a value from

Using Properties in C#

October 3, 2019

Coding: Properties

In C#

C# Properties (GET, SET)

Link #1 – Tutlane

By: tutlane


Properties (C# Programming Guide)

Link #2 – Microsoft

By: Microsoft


How to: Declare and Use Read Write Properties (C# Programming Guide)

Link #3 – Microsoft

By: Microsoft


NOTES

I have been using private fields more often in my coding, even if it is not particularly necessary in Unity, as it is considered a better practice to keep control of the scope of your variables. I have also been working with code that needs to communicate outside of its own scene in Unity, which requires unique work as well. Both of these have led me to use various getting and setting techniques, and I am trying to clean up my code and do it more properly by learning more about creating properties and using getters and setters better.

It is my hope that these three sources cover everything that I am looking for, as well as giving me a better understanding overall of the capabilities of using these properties.

UnityLearn – Beginner Programming – Pt. 01

October 2, 2019

Beginner Programming: Unity Game Dev Courses

Beginner Programming: Unity Game Dev Courses

Unity Learn Course – Beginner Programming

Module Introduction

This has basic coding principles, like formatting code files, naming conventions, namespaces, and effectively using comments. These are all concepts I know about, but only have some experience with so seeing another view should be beneficial. One of their main principles is “Consistency trumps style”.

Formatting your Code Files

The main points for this section are: value of conventions, anatomy of a code file, and formatting guidelines and tips.

Conventions provide a consistent look, make your code more accessible, makes it easier to maintain and extend, and shows your understanding.

Starting basic script structure: At the top there are using directives for namespaces. This is followed by the definition of the class. This defaults to deriving from Monobehaviour, and creating methods for Start and Update.

Editing Visual Studio Code Formatting Preferences

You can change your code formatting preferences in Visual Studio on Windows by going to Tools -> Options. Here, you can find “Text Editor” and find a bunch of formatting options, some for general purposes and then others for specific languages. Under C# for example, it breaks down even further into “Code Style” and more formatting options. These can be used to change how Visual Studio does default spacing for you, bracket placement, etc.

What’s in a Name?

The values of conventions (again) are: provide consistent look to your code, makes your code more accessible, makes it easier to maintain and extend, and show your understanding.

The benefits of naming conventions are: tell us what something represents, tell us what something is, leverage capabilities of modern IDEs, and (although less common currently) can indicate scope.

As usual, they say make your names descriptive (without being too wordy) and be consistent.

C# Naming Conventions in Unity

The capitalization techniques are: Pascal Case and Camel Case. Pascal Case capitalizes the first letter of every word, and single word identifiers are capitalized. Camel Case starts lower case, and only capitalizes every word after the first. For single word identifiers, camel case leaves it lower case.

General Naming Guidelines:

  • use descriptive names
  • use easily readable names
  • avoid abbreviations
  • do not use underscores between words
  • be consistent

Use Pascal Case:

  • namespaces
  • classes
  • structs
  • methods

Use Camel Case:

  • fields
  • parameters (arguments)
  • local variables

There was an older convention to prefix field names with an underscore, an m, or both. This is not done as much anymore, but it is good to mention since you will come across it often. These were originally done to show a difference in scope between variables (i.e. local and non-local variables), but it has been deprecated with the advancement of modern IDEs. They will show you the scope of a variable just by hovering it.

Namespaces and Regions

Namespaces

These are spaces or containers for names. The benefits are modularity and avoiding naming collisions.

Modularity is accomplished by helping group types, classes, delegates, interfaces, etc. logically under a specific namespace. For example, the UnityEngine namespace contains a lot of commonly used tools such as Monobehaviour or Debug.

Fully qualifying the reference: This is when you precede something with its namespace to directly specify which reference you are using.

Including the namespaces used in the using directives at the top of your code file tells you and others that see it what types of objects they can expect to see in the file. Namespaces can even include other namespaces.

Naming Collisions

This occurs when the same name is used to reference different variables representing different things. Modern IDEs help minimize this issue, but it can still happen.

It is not mandatory to put your own code in namespaces, but it is strongly recommended to use in larger projects with project specific features to help keep your code organized and make it easier to maintain.

Regions

Regions are technically preprocessor directives, but they are used often in Unity for their visual purposes. They provide collapsable sections of code to help focus on what is necessary.

Regions are declared with the word region along with a “#” and the name of the region. Everything within the region is contained within a set of brackets. Finally, you end the region with the keyword “#endregion”.

FINAL NOTES

As expected, a lot of this is covering topics I already know, but it is good to confirm some of the practices I use and go over the terminology again. For example, I do still see a lot of underscores used for indicating scope of fields but it is not something I particularly use so it was nice to confirm that its less standard practice now with modern IDEs. These tutorials consistently use all the proper programming vernacular, which is a very nice plus over a lot of other learning sources I use that just want to get across the end game result as quickly as possible.

UnityLearn – Intermediate Programming – Pt. 01

September 30, 2019

Intermediate Programming: Unity Game Dev Courses

Swords and Shovels: Game Managers, Loaders, and the Game Loop

Intermediate Programming: Unity Game Dev Courses

Unity Learn Course – Intermediate Programming

What is a GameManager?

Game Systems

Many parts of your game will need to communicate with one another. You can directly makes these connections between different objects as needed as a way to accomplish this. This method however does not scale well, and becomes very complicated and hard to debug.

This is where the concept of having a GameManager comes in. This provides a central location where systems can communicate through. They can also hold important central data.

The tutorial’s basic definition of a GameManager was “A central location for data” which can serve one of the following purposes or both:

  • Determines who can change what
  • Informs other systems of changes

This Swords and Shovels tutorial will specifically use their GameManager for rule management and some informing.

Persistent Systems

The GameManager needs to be accessible to all the systems that need it. The GameManager should be globally accessible for the life of the game.

Unity Containers

They reference Scenes and Prefabs as Unity Containers. They represent collections of assets and use particular scripts to connect them. In Unity, Scenes are generally large collections of assets, while Prefabs are for smaller collections.

One technique is to have a persistent scene which contains all of your managers, since it will most likely even have responsibilities for loading and unloading other scenes. This is the technique that will be used in this tutorial. This also happens to be the technique I was using on my personal tower defense scene management project.

Preparing to Build a GameManager

You most likely will not be able to design and construct the perfect GameManager for your game immediately. Games are organic objects and you will need to change and adapt your systems as you build in most cases. With this in mind, it is important to build in a way that supports growth (but without over designing).

When starting to make your GameManager then, you want to layout the basic requirements you know it must have. The requirements for this tutorial are:

  • Tracks What Level Is Being Played
  • Can Create Other Global Managers (i.e. Audio manager, Game Save manager)
  • Knows the Current State of the Game
  • Can Cleanup Game Systems (i.e. Save game on quit, send message to server indicating how game ended)

Setting Up Your Scenes

This part finally gets into working with the Unity project. There was a package to download that contained all the necessary objects to get you up to speed to start working at this point.

When you create a script named GameManager in Unity now, it gets a unique icon that looks like a gear. This does not particularly do anything special, it is just a visual to help you identify the GameManager since it is normally a more important class.

This step mostly just wanted to have you get the package loaded in, create the GameManager script, and make a new Boot scene that will handle starting everything (as well as put that scene in the build settings). It also mentions how it is important to put all your scenes in the build settings to make sure they are accessible with your system.

FINAL NOTES

There is a actually a lot going on with the package I obtained for this tutorial, so it may be worth looking into the beginner part of this tutorial before progressing with this part. Even though it will most likely cover a lot that I already know, I think there is still a good bit of valuable information for me to gain from going through it.

Unity Learn Premium – First Look Tutorials

September 28, 2019

Unity Learn

Tutorials and Courses

Unity Learn

Link – Unity Learn

The Humble Bundle had a sale on a lot of interesting Unity content, including a year subscription to some learning tools such as Unity Learn Premium so I decided to finally grab it and look into it. There are definitely a lot of courses and videos I am interested in checking out, so I just wanted to quickly go through and list a few to do as soon as I can.

I am not sure how well these links will work since they will mostly be through this paid service of Unity Learn Premium, but I am hoping they at least work to get me back there when I want to use them.

Intermediate Programming: Unity Game Dev Courses

Unity Learn Course – Intermediate Programming

Advanced Programming: Unity Game Dev Course

Unity Learn Course – Advanced Programming

Introduction to ScriptableObjects

Unity Learn Tutorial – ScriptableObjects

These three stood out to me very quickly as useful tutorials and courses to look into. The courses will be very helpful at just covering general programming in Unity. Even if they cover some topics I have already done, it is always useful to find new and different ways to build systems or implement certain programming techniques. And finally, the ScriptableObjects tutorial just covers a topic that I still need to look into more and just have not yet.

Course on HLSL – Shader Fundamentals

September 26, 2019

Intro to HLSL Shaders

Shader Tutorials

80 lv – Series on HLSL Shader Fundamentals

Article

By: 80.lv


Introduction – HLSL Shader Creation 1 – HLSL Shader Fundamentals

Tutorials – Youtube List

By: Ben Cloward


The first link leads to the following youtube video list that contains the full course on HLSL shader fundamentals. This may be exactly what I was looking for (although apparently the course was first published in 2007, so some of the information is a bit dated). Regardless of age, this is supposed to be a very good run down of HLSL and shaders in general. This will help my search for understanding the basics of shader language while I continue to learn about Unity’s shader graphs separately.

AI State Machine Tutorials: Tutorial #2

September 25, 2019

Tutorial #2

From Blog Post: Unity Basic AI State Machine Tutorials

Youtube – Unity 3D – Make a Basic AI State Machine

Tutorial #2

By: Joey The Lantern


General Structure

There is a general state machine class, a class for each individual state, and then the class for whatever will be using these states. This format is very similar to the first AI state machine tutorial I followed by UnityCollege3D.

State Machine

They created a new namespace, StateStuff. This was because they did not want anything to be a monobehaviour. This namespace then holds the StateMachine class as well as a public abstract class State. They mention that could be used in place of to allow you to specify what types can use this class, but they went with because it’s more ambiguous and works with more things. This is something I will need to look into, but I am guessing at this point T just means any type. So basically anything can use the StateMachine.

The state machine class was given two variables, State currentState and T Owner. These are both set in a constructor for StateMachine. Owner is what will be using the state machine, and currentState is obviously what state it is currently in.

State Class (within StateMachine script)

There were several abstract methods created for this class. These were: EnterState, ExitState, and UpdateState. EnterState allows you to perform actions immediately upon entering a new state, ExitState performs actions necessary when leaving a state for another, and UpdateState performs the various update actions for that state on that object.

FirstState

This class inherits the State class in the StateStuff namespace. The State here takes in an AI reference (again, I need to look into this more to see what this is actually doing). I see what this is doing better after implementing the abstract class. The type that is passed into State is then the type required by all of the methods that take in a parameter of type T. So since we declared State, now all of the methods require an input of AI _owner (translated from T _owner).

Their general idea is that there is only one instance of each independent state class, and every object that wants to use this state will just reference this same single instance. Following that, they use a bit more of an involved singleton-looking pattern to create this. The setup is the following:
public class FirstState : State
{
private static FirstState _instance;

private FirstState()
{
if (_instance != null)
return;

_instance = this;
}

public static FirstState Instance
{
get
{
if(_instance == null)
{
new FirstState();
}

return _instance;
}
}
}

The second state is exactly the same as the first, it just changes some of the text around so that it matches the new class name.

AI Class

This class represents the main script that would be running on your object that you would like to be using AI. To show the state machine working with it, it simply has a timer in the Update method that changes a bool.

GENERAL FLOW

There is one StateMachine instance and one instance of each individual state class. Each state has an UpdateState method, which generally corresponds with what an object in that state should be doing with an Update method. This is actually called through the StateMachine’s Update method though for every object.

The class which you want to use with the StateMachine and various States (AI in this example) must first reference the StateMachine and pass itself in as a paramter. Then it just needs to make sure to call stateMachine.Update within its own Update method. This is what allows everything to communicate what an object using this state machine should actually do in that state.

Finally, each individual state class is responsible for determing when to change to a new state, and what that new state should be. This is however performed by using the ChangeState method found within the StateMachine class. This method is responsible for calling the ExitState method from the old class, and the EnterState method from the new class.

FINAL NOTES

This setup uses a single instance of a state machine and a single instance for each state, which can be a nice avenue to take since it can really keep things scaled down that way. It also seems nice that the classes simply using this StateMachine setup mostly just need to call the StateMachine’s Update method to really interact with it. It helps keeps them from getting messy with state machine code. It does seem like it could be a bit tricky making sure the individual states themselves have proper references to check within the AI classes to figure out when they should change states with this approach.

AI State Machine Tutorials: Tutorial #3

September 25, 2019

Tutorial #3

From Blog Post: Unity Basic AI State Machine Tutorials

Youtube – ADVANCED AI IN UNITY (Made EASY) – STATE MACHINE BEHAVIORS

Tutorial #3

By: Blackthornprod


NOTES

This tutorial actually dealt with using the Unity Animator to create a sort of state machine setup. You can add behaviours to animation states, which are already setup in a very similar way to the last AI State Machine tutorial I did from Joey the Lantern (Tutorial #2 in this recent tutorial list).

These behaviours already have methods such as OnStateEnter, OnStateExit and OnStateUpdate (as well as several other more animation focused methods). This tutorial uses these to setup the logic for their state machine.

While this seems nice for getting something running very quickly, especially from an animation focused perspective, I think I would rather use the setups from the other tutorials that start more from scratch and allow you to develop the full system yourself. I would much rather follow Tutorial #1 and Tutorial #2 than this one.

Unity AI with Basic State Machine

September 24, 2019

Basic State Machine Setup

Unity Tutorial

Youtube – Unity3D AI with State Machine (FSM), Drones, and Lasers!

Tutorial #1

By: Unity3d College


Tutorial #1

This tutorial goes over a “good” version and a “bad” version of setting up state machines. The bad version will work, and is ok for very small scale projects and objects, but it does lack scalability and has some poor design choices going on.

The real state machine covered in this tutorial is not a monobehaviour, but is still able to access components of objects. This was a point they wanted to make sure they covered with the laser object reference.

To start going over the state machine, they go through the Drone class and its variables. Target is a public Transform but has a private setter. The Team variable Team uses an expression body property to allow the setting of team from the inspector without letting anything else change it.

The next class they go over is the StateMachine class. This starts with a dictionary that contains all of the possible states. It takes in a Type and a BaseState value. The Type is just type of the state class, and the BaseState is a new object of that specific state.

The Update method of StateMachine first checks if there’s a current state. If there is none, it sets a default state. It then performs a method called Tick to determine what the next state is (if it is either the same state again, or a new state). It then checks if the nextState is a different state, and if so, switches to the new state.

Each state has had its own class created, each of which inherits from a public abstract class called BaseState. None of these are monobehaviours, but you can still pass in a GameObject object through any state which gets set to a GameObject variable in the BaseState so the states know which object to control. This BaseState also uses protected variables so that classes that inherit from it may use them, without allowing anything else to access them. And finally, BaseState has a public abstract Type Tick method just to make sure every state class has a Tick method (actions to have an object perform in this state).

While checking through the individual state classes, they mentioned the use of nullables. You can add a “?” to the end of a variable declaration to make it nullable. This allows you to set that variable to null when you normally cannot. They like to do this just as a consistent way to help them check if something actually has had a value assigned to it or not.
Example:
private Vector3? _destination;

They also setup a GameSettings class to help them change general variables that go along with their state machine. This uses a simple singleton pattern setup like the following example:

public static GameSettings Instance { get; private set; }

private void Awake() {
if (Instance != null}
Destroy(gameObject);
else
Instance = this;
}

Unity Basic AI State Machine Tutorials

September 23, 2019

Basic AI State Machines

Unity Tutorials

Youtube – Unity3D AI with State Machine (FSM), Drones, and Lasers!

Tutorial #1

By: Unity3d College


Youtube – Unity 3D – Make a Basic AI State Machine

Tutorial #2

By: Joey The Lantern


Youtube – ADVANCED AI IN UNITY (Made EASY) – STATE MACHINE BEHAVIORS

Tutorial #3

By: Blackthornprod


State machine is just a term I have come across when researching and working with AI projects, so I wanted to delve into it a bit more to understand it better. It is my hope that this will be useful as both game design experience as well as general programming experience. These tutorials I grabbed are mostly from well know Unity devolepers that put out generally good learning content, especially when it comes to the programming side.

I am also looking to take a general AI course and do some more advanced AI work in my thesis, so it makese sense to start finding some more interesting or well defined AI systems and learn how they are made. These tutorials are relatively all on the shorter end as well, so should be easy to knock them all out in a single session.