HFFWS Object Instantiation and Setting Their Node Values

August 13, 2019

Human Fall Flat Workshop

Instantiating Objects with Nodes

Youtube – Moving Platform Tutorial in Unity for Human Fall Flat

By: Gotcha McFee

I just wanted to reference this tutorial again as this is how I got the moving platform in the first place for HFFWS. This also shows why I am interested in the specific components and their values when instantiating the object. This object also has a lot of node graph involvement, which is a big part of HFFWS in general, so it shows why being able to control their values in script is so necessary.

Creating Object Spawner

I wanted to create a script that could take some of these HFFWS prefabs and start spawning them with various values for their variables to see if I could produce varied objects in real time in the game.

This script needs to be able to:

  • Take a prefab reference
  • Access certain components (or children’s components)
  • Access the parameters of the components(s)
  • Randomly set them (within a given threshold)
  • Create instances of the prefab with these given values

The components for MovingPlatformVertical that we need access to are:

    Parent

  • Mesh Renderer
  • Mesh Filter
  • Mesh Collider
  • Signal Math Mul (Script): In 2
    Children (Axis)

  • Transform (Rotation)
  • Linear Joint
    • Max Value
    • Max Speed
    • Max Acceleration

It turns out SignalMathMul and LinearJoint are both part of the HFFWS .dll file. You can gain access to these classes in script however with the namespace “HumanAPI”. So at the top of your Unity C# script, you just need to add “using HumanAPI” to reference this namespace to create variable references for these classes.

The next issue I ran into however was attempting to set node values within script. SignalMathMul uses the node system, and I wanted to change the In 2 value of this component in script. By creating a variable reference for a SignalMathMul object, I saw there was simply a variable I could access within it called “in2”, which seemed like the variable I was looking for. Trying to set this as a float, I got an error telling me this was actually of type NodeInput. After creating this object, I looked at the methods and variables available and found one simply called “value”. So I tried setting the value of my number NodeInput to 0.5f, and then setting this signalMathMul.in2 to number (in an attempt to set the input 2 value of the signalMathMul component to 0.5f) but this just resulted in 0 at run time.

TESTING

I think that something with the whole Net Body setup may be causing issues, as this may not like values of these components being set at run time. This may be causing some value discrepancy for a very brief moment that when the networking system sees it it just defaults values to zero.

Initially, I was trying to instantiate a new moving platform with the given SignalMathMul values. I then tried to just alter the values of an existing in scene platform with the script. This also appeared to fail at first, as the inspector showed a value of 0 again. With further testing, this setup does appear to actually set the internal input node value. This change is not reflected in the Unity editor, but when play testing it did change the behavior of the platform. Upon further inspection in the node graph, it could also be seen here that the value was actually being set to that given in the script. I then went back to use the exact same setup but with instantiation again, and this did not work still. Again, this just set the input to zero. I checked in play mode in the updating node graph, and this confirmed it was actually set to a value of zero.

I then tried instantiating the object, and then changing its input values. This created an instance of the prefab with its default values just fine. The platform was moving properly. This however did not update the platform values with the scripted values.

SOLUTION

Since none of these approaches with setting in2.value to a number were working, I checked the methods and variables available again and saw there was also an initialValue variable for the Node Input class. I then tried setting in2.initialValue instead with my instantiated platform and this worked as intended. The value was properly set (in editor as well as in the node graph) and the platform’s moving behavior changed accordingly. The resulting script looks as follows:
public GameObject movingPlatformPrefab;
private SignalMathMul signalMathMul;
private LinearJoint linearJoint;

private void Awake()
{
GOInstantiatePlatform();
}

private void GOInstantiatePlatform()
{
GameObject newPlatform = (GameObject)Instantiate(movingPlatformPrefab);

newPlatform.GetComponent().in2.initialValue = 1.2f;
}

I left the extra component references in for later if I need them, and I was doing the instantiation in Awake through testing in case spawning it “later” was doing something weird with the networking scripts. I tested running it in Start instead and this also worked just fine.

DIGM 540 – Engineering Statics Game Project – Forces in Unity

May 23rd, 2018

Forces in Unity

AddForce – Unity Official Tutorials

Youtube – AddForce – Unity Official Tutorials

The bare basics of scripting forces in Unity with rigidBody’s. The AddForce can take two inputs: a vector for direction and magnitude, and what type of force it is.

ForceModes
  • Acceleration – Continuous change; not affected by mass
  • Force – (Default) Continuous change; affected by mass
  • Impulse – Instant change; affected by mass
  • VelocityChange – Instant change; not affected by mass

AddTorque – Unity Official Tutorials

Youtube – AddTorque – Unity Official Tutorials

The bare basics of scripting torques in Unity with rigidBody’s. The AddTorque can take two inputs: a vector axis to apply torque around, and what type of torque it is. This is very similar to AddForce. Important to remember, Unity uses “LEFT HAND RULE” for rotation.

ForceModes
  • Acceleration – Continuous change; not affected by mass
  • Force – (Default) Continuous change; affected by mass
  • Impulse – Instant change; affected by mass
  • VelocityChange – Instant change; not affected by mass

Game Ideation

Engineering Teaching & Research Equipment – Armfield

EF-1.1 – Statics – Forces experiment kitYoutube – EF series video Statics Forces 1 1 3

The links are similar videos, the first is just directly to the site of those that made the kit, and the second is a Youtube link. This kit provides hand on experience for understanding a lot of topics based around static equilibrium. Topics such as 2D shape center of masses, force vectors, and much more can be covered with this tool.

Game Idea – Engineering Statics Game

Following the general idea of the kit shown above, the game environment is populated with nodes that apply controllable forces to a central ring object. These forces can be altered to change the position of the ring. The environment will spawn collectibles that the player must direct the ring towards in order to collect them. To move the ring, they will need to intelligently alter the forces applied by the nodes.

Quick sketch of concept
May 20th, 2018

Sources to Learn Procedural Generation Techniques for Games

Procedural Generation in Unity

Unity Tutorial: A Procedurally-Generated Galaxy – Part 1 by: quill18creates

Youtube Link to Part 1 of Tutorial

A tutorial to look into for basic procedural generation of a galaxy in Unity. Also has some information on different variable types in C#.

Unity – Cellular Automata Tutorial

Unity Link to Cellular Automata Tutorial

Tutorial directly from unity on the basics of using the procedural generation technique of cellular automata.

May 19th, 2018

Math for Game Programmers – Talks from GDC – Notes

GDC 2015 – Math for Game Programmers: Fast and Funky 1D Nonlinear Transformations

Presenter: Squirrel Eiserloh

Youtube link to presentation

Notes:

  • Other names used for these concepts in other fields:
    • Easing functions
    • Filter functions
    • Lerping functions
    • Tweening functions
  • Implicit vs. Parametric Equations
    • Implicit: Ex. x^2 + y^2 = R^2
    • Parametric: Ex. Px = R * cos(2*pi*t); Py = R * sin(2*pi*t)
  • Parametric Equations use a single variable (usually a float) as input
  • Opportunities to Use Parametric Transformations
    • Anywhere you have a single float to change
    • Anywhere that could be expressed as a single float
    • Anytime you use time
  • The two most important number ranges in mathematics and computer science are:
    • 0 to 1: Good for fractions and percentages
    • -1 to 1: Good for deviations from some original value
  • These functions are normalized
    • Input of 0 has output of 0, and input of 1 has output of 1
    • The in-between values are those that vary function to function
  • Types of functions
    • Smooth Start: exponential functions with various orders
    • Ex: t^2
    • Smooth Stop: gives opposite feel of smooth start
    • Ex: 1 – (1-t)^2
    • Mix functions
    • Crossfade: mix but “blend weight” is the t parameter itself
    • Scale: multiply something by t; can also multiply functions together
    • Bezier Curves
Other suggested talks:

Juice it or Lose it – by: Martin Jonasson and Petri Purho
The art of screen shake – by: jan willem Nijman vlambeer

Math for Game Programmers – Talks from GDC – Notes

GDC 2013 – Math for Game Programmers: Interaction With 3D Geometry

Presenter: Stan Melax

Youtube link to presentation

Notes:

  • Basic Vector Math
    • Dot product
    • Cross product
    • Outer product
    • Jacobian
    • Determinants
  • Geometry Building Blocks
    • Traingles and planes
    • In games, triangles and planes need to know above and below
    • Triangles use normals
    • Planes use Ax + By + Cz + D == 0
  • Ray-Triangle Intersections
  • Ray-Mesh Intersection
    • Could check against all triangles, but there are ways to remove some for efficiency
    • Need to check if you’ve hit the nearest triangle
    • Mesh must be intact, can have issues if there are holes
  • Convex Mesh
    • Neighboring face normals tilt away
    • Volume bounded by number of planes
    • Every vertex lies at/below every other face
    • Convex used because it makes a lot of tests/calculations simpler
    • If point lies under every plane, it is inside
    • Can move rays as they intersect triangles
  • Convex Hulls
    • Techniques for converting meshes into convex meshes
    • There are two main techniques:
    • Expand Outward – start with small mesh and expand out until all vertices are accounted for
    • Reduce Inward – start with a large mesh and shrink it down to fit all vertices
  • Convex Decomposition: breaking down complex shapes into separate convex meshes
  • Objects in Motion – Spatial Properties
    • Find the center of triangles, then the center of tetrahedrons
    • Find the area of triangles and the volumes of tetrahedrons
    • These properties can be used for basic accurate physics simulations of objects
  • Time Integration without Numerical Drift
    • Forward Euler update versus Runge Kutta update
    • Forward Euler applies derivative at every step, which can give inaccurate results in too large of time steps
    • Runge Kutta looks at multiple time steps and their derivatives and takes a weighted average to make steps more consistent
  • Soft Body Meshes
    • Connect points/vertices of mesh with spring-like connections
    • Two main techniques:
    • Kinematic: can have issues with stressed state oscillating as it will not come to rest, forces continue to act
    • Dynamic: has better resting stressed states that will settle down
  • How to learn more
    • Practice tinkering with your own physics engine code
    • Write gjk algorithm
    • Help understand physics engines and their limitations
March 19th, 2018

Real Time Updating Gear Mesh – PROTOTYPE

Prototype – Real Time Gear Mesh Editor

This example build shows the real time updating capabilities of the current scripting behind the gear mesh creator. This also has some of the functionality of the original build, but that part is buggy when trying to tie it in to this current build. This build is solely to show that the real time editing of the mesh does work with the current script structure.

There is still significant work needed to combine this real time updating feature with the effective gear generation coding to create a single, cohesive build of all the elements.

March 15th, 2018

Coding Conventions – Write Better C# Code

Some practices to write better C#/.NET code

As I teach myself more about C# for scripting games, it becomes more apparent that it would be beneficial to learn some of the general conventions for coding to help stream line my process. Things as simple as deciding where to list out types of variables in classes can take a small amount of time that adds up over many projects, but can be eliminated by having committed to hard set conventions. This will provide resources to look to to help with the organization of code in my scripting, and increasing its efficiency.

Book on Proper Coding for Game Development

http://gameprogrammingpatterns.com/

March 10th, 2018

Working with Meshes Via Scripting in Unity

Creating a Game Object and Mesh at Runtime
Tutorial Series – Moving Mesh Vertices at Runtime

This short video shows a very basic way to manipulate existing vertices. It simply moves a few vertices along their normals in a sine wave type pattern.

Writing to .CSV Files with Unity Scripting

How to write data to CSV file in UNITY

Printing out arrays of data to .CSV files can be useful when creating large arrays of data for things such as vertices and tris as it can give you a better way to figure out where errors are occurring.

Using InputFields with C# Unity 5.4 (Basic Tutorial)

This video shows the most basic use of input fields in Unity. This may be a direction to take to connect the player to the variables in the gear mesh editor.

Basic UI Functionality – Interacting Directly with Game Objects Unity

Unity 5 UI Tutorial – Input field and event handlers

This video is in the middle of a series by this author on some basic UI functions within Unity. These UI elements focus on types which the player can interact with, and they will cause some change in the game (as opposed to more static UI elements that just show values for things such as score or lives). This can prove useful as a simple way to give users access to the parameters of the gear mesh objects.

March 10th, 2018

Adding Fog of War Effects – Unity

Some full asset tools created that can create fog of war effects

RPG Map Editor – Fog Of War – Youtube

This tool looks to setup a fog effect on a grid based tile system in a 2D game/environment.

Tutorial 3 : Fog of War – Fog of War 3D Effect – Youtube

This tool looks like it uses light to emphasize the fog of war effect, which it can do since it’s set in a 3D environment, which is more commonly associated with lighting in Unity.

March, 4th 2018

Creating 2D Enemy Movement in Unity that is Able to Avoid Obstacles

How can I make an AI that avoids obstacles WITHOUT using NavMesh?

This link contains oStaiko’s answer to the above question, and describes two standard ways to deal with basic enemy AI movement, especially on a 2D plane. These two ways are: a waypoint setup and a more “vision” focused setup.

Waypoint Setup Pros/Cons
  • Good on static maps where enemy can “know” the entire map
  • Deciding fastest routes can be tricky
Vision Setup Pros/Cons
  • Works better for non-static maps/obstacles
  • More difficult to setup (scripting-wise)
  • Better for open maps, can work poorly in more constricted spaces

Example Script for Setting Up Raycast AI to Move Around Obstacles

Unity 3D – Enemy Obstacle Awareness – AI – Code Sample

This link leads to some sample C# code for setting up a script that gives an object raycast vision to avoid obstacles and refocus on a target.