Dev Blog

Pokemon Unity Tutorial – Player Movement [Pt.2]

August 13, 2019

Unity Pokemon Tutorial

Player Movement

Youtube – Lets Make… Pokemon in Unity! – Episode 2 Basic Player Movement

By: BrainStorm Games

Player movement in the overworld of Pokemon games is tile based. To emulate this, the tutorial uses a Coroutine which can move the player a full tile at a time while locking the player out from further inputs until the player character has completed its full tile of motion. It also uses a simple setup where it reads the player’s horizontal and vertical input, but checks which is greater and reduces the other to zero to ensure the player is moving only on the two intended directions.

Since the player is a 2D sprite, they used an Enum setup to tie certain sprites to certain movement directions. This way it would look like the character is facing the direction they are moving. This setup had the 4 directions (North, South, East, West) for the Enum Direction. The player’s input would set the Direction variable, currentDir, to a corresponding direction. This would then be referenced in a switch case to determine which sprite to display for the character’s SpriteRenderer at the time.

Top Down Movement in Unity Tutorial

August 13, 2019

Unity Top Down Movement

Brackeys Tutorial

Youtube – TOP DOWN MOVEMENT in Unity!

By: Brackeys

This seemed to fit well with the Pokemon tutorials I was doing recently so I decided to give it a look. It’s been a while since I used Animator variables to control 2D animations so this was a nice refresher for that, but they also used Blend Trees which is something I had not seen before.

The initial script was very simple. This was a 2D Rigidbody setup, so it’s more physics based than the Pokemon tutorial I am doing. As usual for physics in Unity, they do the physics work in a FixedUpdate method, however, they still receive the input in a standard Update method. I always see you should do your physics in FixedUpdate to deal with frame rate variance, but had not seen anything specific about splitting up the input and physics into the two different Updates.

The Animator starts by adding two simple parameters: float Horizontal and float Vertical. The Blend Tree happens in a single state in the Animator for the Animator Controller for the player. When you right click and select “Create State”, you select “From New Blend Tree” instead of “Empty”. You can double click it to go one level deeper into this state node. It’s important to note the parameters apply here too, so they are useable throughout all the levels.

In this Blend Tree, there are three main factors to account for: the parameter, the thresholds, and the animations. You start with the parameter to check. You can then set “Motion Fields” which will hold an animation and parameter thresholds. It will then check the parameter relative to the thresholds set to determine which animation to play. This simple 2D walking animation set is a great example to show this off in a simple way, as it basically just switches between the 4 different directional walking animations depending on the combination of the horizontal and vertical parameters.

They then go back to the top level of the Animator to tie everything together. They make a transition from Idle to this Blend Tree, and vice a versa. This just makes sure to set a state for what to do when the player isn’t putting in any inputs.

Finally, to tie this new Animator setup in with the actual movement script and player input, they go back to the PlayerMovement script. The small addition needed here is simply setting the Animator parameters equal to the input gotten in the Update method.

Overall, this was a neat way to approach 2D movement in Unity a bit differently than I have before. The Animator blend tree definitely seems nice for keeping the PlayerMovement script cleaner and easier to read instead of cluttering it with a lot of input comparisons to determine different sprites and animations to use.

Using Coroutines to Control Animation in Unity

August 12, 2019

Coroutines for Animation Timings

Unity

Youtube – Coroutines and Animation: Beyond Beginner Unity Tutorials

By: Tom coAdjoint

For a Unity project, I needed to control the flow of my program to process an animation fully before moving on to the next operations. Timing concerns normally lead me to coroutines, so I looked into controlling animations with coroutines in Unity.

The approach I ended up using for a quick fix solution was just to use WaitForSeconds in Unity. I basically just had the operation start the animation and then WaitForSeconds approximately the duration of the animation (which needs to be hard coded in at this point), and then continue with operation. While this works, it feels a bit clunky and hard to work with. For example, changing out the animation will require changing the time accordingly every time. Also different animations will need different values entered individually.

I came across this tutorial linked above which seemed promising for a better animation timer, especially for game building purposes. Unfortunately, I don’t have a lot of experience programming animation related objects in Unity, so I was not able to fully grasp how this setup worked. I need to look into the already available methods and variables Unity provides when working with the Animator and Animation more.

The main coroutine driving everything here is IEnumerator WaitForAnimation, which has the following setup:

IEnumerator WaitForAnimation(string name, float ratio, bool play)
{
var anim = animation[name];

if(play) animation.Play(name);

while(anim.normalizedTime + float.Epsilon + Time.deltaTime < ratio)
yield return new WaitForEndOfFrame();
}

The overall premise makes sense. You input an animation name, a ratio of how much of the animation you want to play, and a bool to activate it. It was unclear which variables were created by the tutorial and which exist in Unity already. animation[name] appears to be an array reference, but I am unsure if this is how Unity normally deals with animations or if this was created separately. normzlizedTime is also new to me, but that does appear to be a built in time of animation reference.

It will take a bit more researching, but this approach definitely seems to be more general and useful if I can determine exactly how it works.

Setting Up HFFWS Project

August 9, 2019

HFFWS

Setting Up Initial Project

Youtube – Human Fall Flat – How to make a map – Ranul

By: Ranul

So following this tutorial, the bare minimum for getting a level started is simply adding the “Level” prefab into your scene. This comes with:

  • InitialSpawnPoint: where the player spawns in; even has an indicator with a ray to show exactly where they will hit
  • FallTrigger: this is the trigger that determines when the player should be respawned
  • PassTrigger: the trigger for finishing the level (the goal)
  • Directional Light: standard directional light

It also sounds like “everything” needs to be childed to this Level prefab, so I’ll stick with that until I find otherwise.

Thrower Tutorial

Youtube – Spinning Thrower + Space Unity Tutorial for Human Fall Flat Workshop

By: Gotcha McFee

This tutorial shows how to use some basic tools that Probuilder affords you in Unity to make a moving, interactable object in your HFF level. This specific object is a “Thrower”, which is a spinning wheel that the player can hold onto then release to get launched to a new area from the wheel’s momentum.

Notes that the mass of objects makes a big difference in HFF in general, so modifying this can help if you aren’t getting the desired results. Generally objects start with a rigid body mass of 1, but in this example they set the wheel to a mass of 500. This significant difference shows you should be careful keeping track of your object masses so you don’t get issues just because you missed a very low mass value.

There are some notes on how to make a Probuilder object into a moving and interactable object in HFF. To allow it to move, there is an Entity Type under the Pb_Entity component that must be changed to “Mover”. To allow it to then be interactable, the Mesh Collider component needs to have the Convex option turned On. I noticed you could still interact with the thrower holders (just long cubes on the sides of the wheel) without changing anything, so it seems the Convex option is only important for moving objects.

Noted important factors for a wheel object:

  • (If Probuilder object) Pb_Entity – Entity Type – Mover
  • (If Probuilder object) Mesh Collider – Convex (On)
  • Hinge Joint component (automatically adds Rigidbody component)
  • Hinge Joint – Axis (Ex. 0, 1, 0)
  • Hinge Joint – Use Motor (On)
  • Hinge Joint – Motor – Target Velocity (Ex. 175)
  • Hinge Joint – Motor – Force (Ex. 500,000)
  • Rigidbody – Mass (Ex. 500)

Rope Tutorial

Youtube – Rope tutorial in Unity for Human Fall Flat

By: Gotcha McFee

This shows how to create a simple rope in the HFFWS from scratch.

The main objects needed as children objects of your overall rope object are:

  • Start
  • End
  • Rope

The Start and End are just Rigidbody objects with certain masses and constraints (want the start to be more solid and fixed where the end is more manueverable and flexible). The Rope holds a lot of the inner workings of the rope object.

Components needed for the Rope object within the overall rope hierarchy:

  • Rope (Script)
  • Mesh Filter
  • Mesh Renderer
  • Net Body (Script)(which has Net Identity attached as required component)
ISSUES

The tutorial shows how to add some objects like spheres and cubes to the rope object to give it an attachment point at the top and interactable objects at the end. I just wanted to quickly test adding the objects to hold onto on the end of the rope, so I just added a sphere as a child of the end and tried running the game. This caused the rope to glitch out and jump around violently. It appears that adding an object that has too much “rope in it” causes some weird collisions that just make everything move around erractically. I was able to get similar results to the tutorial by moving the sphere a bit below the end transform (without completely removing it visually from the end of the rope). This is just something to be aware of if adding to your rope.

PARAMETERS

The tutorial goes over some of their experience with the parameters of the rope script and results that have and have not worked for them.

Rigid Segments:
Having too many of these makes the rope too “heavy” and hard to use. You can change the individual segment weights as well, but too low of a segment mass causes the rope to “break” and become very glitchy. They generally make normal ropes have about 6 or 7 segments, with 15 being about the max for a very large rope.

Segment Mass:
The default value for this is 20 and that seems to be a pretty nice standard value. As mentioned previously, very low values break the rope and cause it to function incorrectly. The 15 to 20 range seems to be pretty consistent, but just make sure to test ropes with varying mass values if you are getting strange results.

Learning from Pokemon Unity Tutorial

August 8, 2019

Unity Pokemon Tutorial

Setup

Youtube – Lets Make… Pokemon in Unity! – Episode 1 – Basic Setup World/Character

By: BrainStorm Games

As a big fan of Pokemon, I wanted to finally jump into this tutorial just to learn from what it has to offer. It could be very helpful for any 2D projects, especially those that are more RPG-based, and I imagine a lot of concepts can be carried over to 3D projects as well.

I am especially interested to see:

  • How they perform the movement in a way that’s consistent and easy to control
  • Setup of the overall “Pokemon” class structure to deal with many different options of very similar creature objects
  • How they deal with encounters (and the implementation of randomness)
  • Connected to points 1 and 3, how they structure the overall class for encounter areas
Using Tiles

Sometimes tiles can have a line between them even though they are connected, but this is normally caused by anti-aliasing. This can be turned off in the quality settings in Unity. Newer versions (I’m using 2019.1.12 currently) seem to have that disable initally in 2D projects.

Since we’re using tiles that are generally positioned pretty precisely, I thought this would be a good time to use Progrids again. This is perfect for continously placing all these tiles in exact incrementally different locations right next to each other. It is a bit awkward that the tiles get placed onto the intersection points of the grid as opposed to filling in the nicely made grid squares, but that’s just an aesthetic pain. It functions perfectly for this.

You can also achieve a similar effect to Progrids just normally in Unity by holding [Ctrl] while clicking and dragging to move an object. This can move the object one unit at a time.

Human Fall Flat Workshop Resources Updated

August 7, 2019

HFF WS Resources

Links to Useful Information

I am getting back into some HFF WS editing for my thesis project, so I gathered a lot of resources together on how to work with it, how the updates change things, and a few tutorials for putting some objects together. I compiled this list of resources while I downloaded the updates, added the new WS package to the Unity project, and updated all the prefab icons in editor to make everything nice and easy to use once I finally started putting a scene together to test things out.

Steam HFF WS Help

[NEW] Human Workshop: Features, Multiplayer Support and Backward Compatibility to 1.2 Alpha2

[NEW] Human Workshop: Example Workshop Levels, Templates and Prefab Testbeds

[NEW] Human Workshop: Editor Improvements and Node Graph System

[NEW] Human Workshop: Post-Launch Fixes, Updates and Additions – v1003292

HFF Tutorials

By: Gotcha McFee

Rope tutorial in Unity for Human Fall Flat

Moving Platform Tutorial in Unity for Human Fall Flat

Unity Controlling Coroutines

August 6, 2019

Controlling Coroutines

More Advanced Coroutines in Unity

Youtube – Unity3D – 2 Ways to Start & Stop Coroutines (the good & bad ways)

By: Unity3d College

Youtube – Unity3D – Using Delegates / Actions as Callbacks in Coroutines

By: Unity3d College
1st Tutorial

This first tutorial is one I’ve looked at before, and it makes more sense now when I want more accurate control of a coroutine in Unity. Basically you can create an IEnumerator variable that can just hold the coroutine you want to run. This same variable can be checked for being null as a way to determine if it is already running, which can be helpful to ensure only one instance of the coroutine is running at a time.

This also suggests a different approach than normal for creating a more accurate timer in a Unity coroutine. Instead of counting a timer up or down with Time.DeltaTime, they set a float value to Time.time when the coroutine is started, and then check if difference of the current time (Time.time) and that set start time is greater than your intended timer value.

2nd Tutorial

This was a bit more advanced, and I need to look more into Unity Actions to fully understand how this works, but I think I got the general concept. It appears the basic premise is using an IEnumerator which takes on a method (methods) as a delegate input to ensure that a certain step is completed before running the input delegate (method(s)).

For example here, they wanted to replace an image but they wanted to ensure that the www object was obtained first to use for that method. Since a delegate was used, they also showed the flexibility of how the same foundation could support different methods.

What I am a bit confused by here, is how the ReplaceImage method that is set as the delegate in the LoadImage IEnumerator gets its input. It requires an input of Texture2D but I don’t see if or how that is set. I believe this may be my lack of knowledge on either Actions or Delegates in Unity, so I’ll need to look into that.

Unity Composition and Inheritance

July 29, 2019

Unity Composition

Composition vs Inheritance

Youtube – Unity Architecture – Composition or Inheritance?

By: Unity3d College

I was looking to use inheritance with an overall parent abstract class to create a few simple scripts, but was having some trouble getting it setup properly. When looking into how to approach this better, I came across the concept of “Composition vs Inheritance”. This is the first time I heard the word composition, but it appeared to be the idea in Unity of creating a bunch of small scripts that perform specific types of functions that can be placed on objects in a varied structure style to create the proper type of object you want.

I thought this was a neat concept that made a lot of sense in Unity, so I ended up using this idea to approach the issue I was working on. This got me into using RequireComponent(typeof) in Unity, which I’d seen before but never really used. This seemed to be a nice feature to keep track of with a composition style approach since there will probably be a lot of cases where certain components will need others to be there, and this is just a good practice to make sure you don’t have any issues when setting up your structured object.

Updating Waypoint System in Unity

July 19, 2019

Updating Waypoint System

Starting with a base waypoint system I created with the Brackeys tower defense tutorial, I wanted to update it so that the overall path could be altered at run time. Basically, I didn’t want objects traveling along the waypoints to travel the full length everytime. I needed them to sometimes travel part way, sometimes more/less, sometimes the full way.

I decided I would have the main script holding the general waypoint information to hold the value for where objects should stop traveling. This way all objects could reference this for how far they should travel, and I could have anything else just change this single value to update where everything is going. This was as simple as adding an int value called currentEnd to this main script, Waypoints. Then I added the methods GetCurrentEnd (to return currentEnd) and SetCurrentEnd (to change the currentEnd value).

Next was updating the Waypoint_Movement script, which is attached to the objects moving along the waypoints to tell them how/where to move along the waypoints. They were originally moving from waypoint to waypoint until they reached the end of the Waypoints array (Waypoints.points.length – 1, the length of the waypoints array minus 1). This causes them to travel until they hit the end of the array. This was simply changed to check for [Waypoints.GetCurrentEnd() – 1] so they would only move to a waypoint determined by the currentEnd value at the time.

Finally, other scripts were able to add in the SetCurrentEnd() method call from Waypoints to their current functionality to have it basically update the length of the waypoint path further objects would travel.