Basics of Dependencies in Unity

October 19, 2020

Dependencies

Unity


Title: Dependencies in Unity
By: Infallible Code
Youtube – Tutorial
Description: Covers the basics of dependencies in programming, with a focus on them within Unity and how they deal with expensive method calls.


Overview

This tutorial nicely breaks down dependencies in programming in general in a very basic and understandable way. This is good for defining it well at the foundational level. It also covers some of Unity’s more expensive method calls and the different ways to cache these results, as well as the different ways to organize and manage dependencies within Unity specifically.

Notes

Dependency: When one object relies on another to function.
The initial example in this tutorial nicely shows how improper code can also hide dependencies. Since they were using FindObjectOfType in the Update method (an extremely expensive and inefficient process), it could be lost that that class was dependent on the Inventory object. Creating a private variable at the class level to hold that data and caching it in the Awake method makes it more much clear, exposing the dependency on the Inventory object.

Dependency Management Techniques

Dependency management techniques are just the ways your classes are accessed by the objects they depend on. There are two major groups for this: internal resolution and external resolution.

Internal Resolution

When an object is responsible for resolving its own dependencies
Accomplished by:

  • Instantiating new instances
  • Using factories or the factory method
  • Using Unity’s built in getters (i.e. GetComponent, FindByComponent)

External Resolution

When an object receives its dependencies from another object
Accomplished by:

  • Constructor parameters
  • Public fields and setter methods
  • Serialized fields

Examples of Using (Or Not Using) Different Methods

Some examples covered:

  • Do NOT use GetObjectOfType if you will have multiple objects of that type
  • For very simple cases, the external serialized field option is common practice
  • Serialized field does not work well if object is loaded dynamically
  • Dynamically loaded objects can usually use an initialization method that acts like a constructor
  • Dynamically loaded objects can also use constructors if they are normal C# classes, but monobehaviours do NOT support constructors

Extenject

Dependency injection is just another word for external resolution. Extenject is a dependency injection framework that manages all the dependencies across your entire project. Extenject can be very useful, but can quickly overcomplicate smaller and simpler projects.

Adding Details to Enhance the Player Character

October 16, 2020

Detailing and Enhancing

Player Character


Title: 6 DETAILS TO MAKE A GREAT PLAYER CHARACTER – UNITY TUTORIAL
By: Blackthornprod
Youtube – Tutorial
Description: Tutorial on adding a few small details to enhance the feel of the player character.


Overview

I came across this tutorial that covers a few quick ways to add some details to a player character to make them feel much more fun and interactive to use. They used Hollow Knight as a base for the types of details they were looking to add. The key details they created were: landing animation, jumping/landing vfx, moving vfx, background interaction while moving, and jumping/landing sound effects.

While on the simpler side, these are all very nice features to add that can quickly make your character much more enjoyable to use. As an important side note they mention, this can actually be beneficial to the developer because it can make them for motivated to work with the character.

Exploring Unity’s New Input System Further

October 15, 2020

Input System

Unity


Title: How to use the new Input System in Unity3D
By: Coding With Unity
Youtube – Tutorial #1
Description: Indepth tutorial on implementing Unity’s new input system along with programming its use.


Title: New Unity INPUT SYSTEM – Getting Started
By: Dapper Dino
Youtube – Tutorial #2
Description: Quick tutorial using Unity’s new input system and using it with programming.


UPDATE

Title: Unity Input System | How to Use Input Action Assets
By: Infallible Code
Youtube – Tutorial #3
Description: Good all around tutorial for getting started using Unity’s new input system.


Title: Unity Input System Quick Start Guide
By: Unity
Unity – Input System Documentation v.1.0
Description: Unity’s documentation on the new input system.


Overview

I have already tested using the new Unity input system a bit and it seems promising so I wanted to explore it further. These tutorials show it in use on a more in depth level so I expect them to show me some more ways to utilize it properly. The documentation was also included as it is useful to find any methods I may need to keep everything together and working.

Update

I found another good tutorial by Infallible Code that I have added. I think it covers the foundational work of using the new input system better than the other tutorials and gives a better explanation of what the generated class from the input system is actually doing. This makes using it much more sensible.

Resetting Keybindings with New Unity Input System

October 14, 2020

Rebinding Keys

Unity – New Input System


Overview

I have been trying out the new Unity input system to see if it is worth using instead of the older method of building out everything yourself. In this process I wanted to make sure it had a way to rebind keys since this is a gigantic feature to have in almost all games. I was able to find that there are several different ways to change keybindings with the methods found in Unity’s documentation on the new input system found here:

Unity Editing Keybindings for New Input System Version 1.0

The two main methods I am focused on currently are ApplyBindingOverride and PerformInteractiveRebinding. PerformInteractiveRebinding allows for keybinding modification by the player at runtime, and is a quick way to implement a feature commonly found in many games where the player selects an action to change the keybinding for and the game awaits their input to set the new keybinding to that input. I do not think this change is saved inherently however, which is where the other method comes in.

ApplyBindingOverride allows the system to override the default keybinds set by the game originally. So after using PerformInteractiveRebinding methods, that data can be saved to then later be utilized by the ApplyBindingOverride method to reapply the keybind changes every time the game is started. This helps it fall in line with how the feature is utilized in most games in a very convenient and user-friendly way.

Creating an Input Buffer System for an Action Game

October 13, 2020

Input Buffer System

Game Development


Title: Character Action Game Development Tutorial 8– Input Buffer
By: Tyra Doak
Youtube – Tutorial
Description: Small segment of a tutorial list that focuses on creating an input buffer system in a 3D action game.


Overview

Having input buffering in a game can help a game feel much more responsive and smoother to a player, especially in hectic, fast paced gameplay such as fighting games or action games. This tutorial has a large emphasis on building out their input buffering system for their 3D action game so this is right in the prime territory for the type of game I am interested in building an input buffering system for as well. They also show a decent visual display of the frame data at the top of the screen during play mode that could be an ok starting point to help with starting to build frame data focused systems.

6 Design Patterns for Game Devs by Jason Weimann

October 8, 2020

Design Patterns

Game Development


Title: The 6 Design Patterns game devs need?
By: Jason Weimann
Youtube – Tutorial
Description: Quick coverage on 6 general design patterns often used for game development.


Overview

This video quickly covers a lot of design patterns or design pattern-like architecture that are often used in game development. These include: singleton pattern, observer pattern, command pattern, component pattern, flyweight pattern, and state pattern. I have experience with most of these so this seems like a good video to cover all of them quickly to give me another view on them on how they might be utilized, as well as seeing if there are any I haven’t used that could be good to keep in mind.

Unity Create Your Own Keybindings Manager

October 5, 2020

Unity

Input Systems


Title: How to Create a Custom Keybinding Handler for your Unity Game – *Improved*
By: Dapper Dino
Youtube – Tutorial
Description: Tutorial for creating your own basic input manager within Unity.


Overview

The idea behind creating an input manager is to have a flexible and easily accessible tool for both setting up your input system within a game as well as modifying it. This manager should help keep programming associated with the player input more organized and consistent, as well as providing pathways to allowing both the designer and eventually the players options to easily change the inputs to their liking.

Tutorial Notes

Setup

Immediately they create an InputManager class that follows a singleton pattern, but also includes a DontDestroyOnLoad method. This is ok to start, but may be worth looking into editing that if using a more sophisticated multi-scene project setup later on.

Within the InputManager, they created classes to replace some of Unity’s basic input reading methods. These included: GetKey, GetKeyDown, and GetKeyUp.

They then added another class named KeybindingActions, which was a container for a public enum that held references to all the possible actions you may want for the game. For now I decided to just include this enum directly in the InputManager class, but will see if it makes sense to separate later.

Next they add the scriptable object core of this setup, which is named Keybindings. The benefit of using a scriptable object here is that it makes it easy to swap it out with different sets of keybindings (especially with different types of input or controllers).

Building Out InputManager and Keybindings Scriptable Object

Within this scriptable object, they created another class named KeybindingCheck. This provides the connection to setup which keys are tied to which actions. They mention a dictionary could be used here, but they created a list of this serializable class instead so it can easily be seen and modified within the Inspector. This class simply holds data for a KeyBindingActions object and a KeyCode object (to ensure they are connected). The scriptable object then just has a list of these KeyBindingChecks.

They mention if you want the player to be able to set their own keybindings you technically just need to edit the scriptable object and this will modify the keybindings. However, scriptable object changes only remain if they are done in the inspector, so you would also need to save that data somewhere else if you had a game where you want the player to be able to set their keybindings once and have the same setup each time they close and reopen the game, which would be the standard implementation.

With that setup, they go back to fill out the methods within the InputManager which effectively replace those provided by Unity. Each of them follow the same pattern seen here in the GetKeyDown replacement method:

public bool GetKeyDown(KeyBindingActions key)
{
foreach (KeyBindings.KeyBindingCheck keyBindingCheck in keybindings.keyBindingChecks)
{
if (keyBindingCheck.keyBindingAction == key)
return Input.GetKeyDown(keyBindingCheck.keyCode);
}

return false;
}

So effectively whenever you would use GetKeyDown for example, you use this InputManager.GetKeyDown method and pass in one of the enum options you have provided as a parameter (i.e. Jump, Crouch) and it searches through the list of KeyBindingCheck objects you have in the KeyBindings scriptable object and if it finds that enum in that list, it returns the Unity base input method (i.e. GetKeyDown here) with the corresponding keyCode you have associated with that enum as its input parameter.

Summary

I like that this method allows for the use of easily accessible and modifiable enums that are specifically chosen for your game when it comes to programming the input of your game, but I am concerned how efficient this is since it appears to add small extra checks with every single input from this user. While small, this is an extremely common action that will be used thousands and thousands of times easily in normal gameplay so I wonder if it ends up being worth the cost. This method does also allow you to edit the keybindings at runtime and because it is with a scriptable object, the changes are actually saved even when exiting play mode.

Newer Unity Input System

October 5, 2020

Unity

Input Systems


Title: NEW INPUT SYSTEM in Unity
By: Brackeys
Youtube – Tutorial
Description: Tutorial for using Unity’s newer input system.


Overview

This setup uses Unity’s improved new input setup system. It provides a nice way to setup your inputs more specifically without directly going into the Project Settings -> Input Manager window. The user starts with a blank slate and adds actions to this input manager asset to match up with all the actions they will want in the game.

From this tutorial they obtained the tools to use this new input system through a project on github, but now it is available as a standard package within Unity simply named Input System. It however mentions that the backend has been modified (I am using Unity version 2020.1.5f1 currently when testing this) so I am keeping an eye out for any problems it may cause.

Tutorial Notes

There are three major sections to this input system: Action Maps, Actions, and Properties. The action maps appear to be like keybinding sets, so you can save entire different sets of actions with their own keybindings to each map you create. Then the actions are the core focus, where actions can be named and keybindings can be set, as well as which types of controllers are used. Finally the properties section allows for more detailed additions to each action such as other modifiers to apply to the input when used as well as the type of action.

After filling this out to the desired result, there is an option to “Generate a C# Class” from this new input system you have created. This provides you with the proper tools to utilize the asset you created within your programming system yourself. The context is a bit more awkward as it uses events, so you have to assign methods to them when using them in your own programming. They set this up in the Awake method for example:

controls.Player.Shoot.performed += _ => Shoot()

controls is the name of the InputMaster object (which is what they named the generated C# file from the input system), Player is the name of the action map, shoot is the name of the action from the input system, performed is a subset of that action, and finally Shoot is the method being subscribed to that event. The underscore is simply a place holder used for this programming syntax so it is necessary but I am unsure exactly why the syntax is like that.

I was then having an issue following the tutorial because they simply made controls (their InputMaster object) public so they could drag/drop the input system asset itself directly into the component so they were connected and running. When I did this the InputMaster object did not show up in my Inspector, even when made public. To get it to work at all, I found that I just needed to assign a new InputMaster to controls in the Awake method. I found that in the documentation provided here: Unity New Input System Manual

I then found that natively you could not use both the old input system and the new input system at the same time. So while this did get the new input system working and I could jump (which was the only action I assigned for testing), I could not move horizontally at all as the inputs for that just did not work anymore. Exploring the documentation further, I did find that you can switch back and forth between the old and new input system, as well as activate both at the same time, in the: Project Settings > Player, under: Active Input Handling. Upon setting that to both, I could move horizontally (with the old inputs) and jump (with the new inputs).

Installation Guide here: Unity New Input System Installation and Setup Documentation

Summary

This approach seems promising for setting up complex keybinding setups where you also want multiple devices. It does provide the base benefit of not needing to use strings with the input system anymore when doing the programming so that is a large benefit already. I would have to explore how to edit keybindings at runtime with this setup to see if it is a good final approach for this system that would also support letting the player set their own keybindings.

Brackeys 2D Movement and Melee Combat Tutorials – 2D Movement

September 30, 2020

Unity

2D Movement


Title: 2D Movement in Unity (Tutorial)
By: Brackeys
Youtube – Tutorial #1
Description: Tutorial for using a simple Unity player controller.


Title: MELEE COMBAT in Unity
By: Brackeys
Youtube – Tutorial #2
Description: Tutorial for setting up basic melee combat with a 2D player controller.


Overview

I started with the Melee combat tutorial but it referenced the movement tutorial for how they setup the starting movement and animation so I wanted to move back to them just to make sure I was starting in a similar spot. The player controller made in the 2D movement tutorial is actually pretty bad unfortunately, but I still wanted to use this melee combat tutorial as a base point for something that is workable but not amazing just to get an idea of how to approach combat.

2D Player Movement – Tutorial #1

While overall not a great tutorial, it did support some concepts I have come across. They separated the player input functionality and the actual movement application into the Update and Fixed Update methods respectively, which is a solid approach I have seen more and more in my search for information on player controllers now. However, all of the Fixed Update work was basically already done in a Player Controller script they gave you, so it really just covered programming the script responsible for receiving player input and passing it on to the movement logic.

To make matters worse, the given script does not even work particularly well. Its checks for the ceiling and the ground can give weird results unless at good ranged values which can prevent your player from crouching or jumping, or getting out of crouch at times. Then when air control is turned on, there is a bug where the character automatically crouches in midair (which removes their upper hitbox) simply from them jumping into objects as the ceiling sensor activates and automatically makes them crouch in the air.

I was able to fix the midair crouching bug by adding an extra check in the tutorial given script to also ensure the player was grounded before making crouch true. This led to another interesting aspect of the tutorial with the separation of input and actual player movement because the movement was receiving information from the input such as “is crouch being pressed” and then would use that information and check the surroundings (such as if they are under a ledge) to determine whether they could actually move out of crouch or not. This was just an interesting way of separating the input logic from the actual player control logic and is good to keep in mind for an input buffering system.

After fixing the bugs and messing with adding some animation similar to what they already have at the start of the melee combat tutorial, I did not have time to move on to that one. I will look into completing that next with my decent player controller in place now with most of the animations.

Unity Custom Keybinding Manager

September 29, 2020

Unity

Keybinding Manager


Title: How to Create a Custom Keybinding Handler for your Unity Game – *Improved*
By: Dapper Dino
Youtube – Tutorial
Description: Tutorial for setting up an input manager scriptable object in Unity.


Overview

I was looking for a nicer way to control inputs and controls when setting up a Unity project other than the very basic way of listing out all the specific key codes in the user scripts themselves or using fragile string references when tying it to Unity’s natural input manager. Initially I came across this creator’s first attempt at setting up a keybinding handler (hence the *Improved* tag on the title of this tutorial) but was not as interested in it since they still used strings and I thought an enum implementation would be better, but lo and behold, the same creator had actually improved their system a year later with an enum system like I was imagining.

While maybe not perfect, I do still like this overall implementation better than just using Unity’s natural tools for sure. Adding the enum setup removes the necessity of typing in direct and exact strings in your code which is great for reliability, while also making it easy to change key bindings in the scriptable object itself during develop. Having a system like this is also a good start for a truly accessible product on release that allows users to set their own keybindings, which is standard in many games.