Instantiating HFFWS Platforms with Parameters Determined in Script

August 23, 2019

Human Fall Flat Workshop

Instantiating Moving Platforms with Varied Parameters

Goal: Instantiating various HFF prefabs with varied parameters determined in script
– Should be able to work with multiple prefabs or provide a base foundation that can make further scripts which can work with
other prefabs
– Should be able to work with the signal and node system of prefabs
– Should also work with more standard values (basic Unity components)

CreatePlatforms Test

– This script focuses on working with the MovingPlatformVertical prefab
– It seems that using initialValue works to set values in the node system for this object when doing it upon instantiation
– Need further testing to determine the difference between value and initialValue and when to use both
– Interesting components and values to check:

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: X and Y)
– Linear Joint
– Max Value
– Max Speed
– Max Acceleration

– Tests:
– 1) Try assigning various parameters all throughout the prefab to see which work and which don’t
– I used the following code to set a bunch of various values of the instantiated prefab through script:

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

        newPlatform.GetComponent<SignalMathMul>().in2.initialValue = input2Param;

        newPlatform.GetComponentInChildren<LinearJoint>().maxValue = maxValueParam;
        newPlatform.GetComponentInChildren<LinearJoint>().maxSpeed = maxSpeedParam;

        axisChild = newPlatform.transform.GetChild(0).gameObject;
        axisChild.transform.localEulerAngles = new Vector3(12.2f, 35.5f, 12.5f);
    }

– Many of the components I wish to alter are not using the node based system and were easy enough to set as pretty standard
Unity component values
– Setting these did change the values on the instantiated prefab, but they did not seem to impact the actual platforms
– For example, even though the axis was rotated at a different angle, it continued to just move up and down
– It appears the platform’s behavior is using its base prefab values before having the values set by the script
– UPDATE: Editing the LinearJoint params in script seems to work for instantiation as well
– It is specifically the Axis rotation that is not being properly followed

– 2) Try assigning values to an existing object through script
– See if this allows an object to operate based on scripted values
– This did not work either
– At least the axis alteration clearly does not work
– EDIT: This did work on the LinearJoint parameters
– it takes on the scripted values then uses those to drive the platform
– doesn’t use the Unity editor set values

– 3) Add script directly to Axis object to change rotation at Start
– Code:

private void Start()
{
RotateThis();
}

private void RotateThis()
{
transform.localEulerAngles = rotationValue;
}
– this also did not work
– still properly sets value, but platform uses original values to move

– 4) Try Test 3, but on Awake instead
– Code:

private void Awake()
{
RotateThis();
}

private void RotateThis()
{
transform.localEulerAngles = rotationValue;
}

– This actually worked! The rotation was set and the platform followed the new rotation

– 5) So try running the instantiation method of the platform in Awake
– this did not work
– similar outcome, values were edited but it did not follow the new axis

– 6) Set Axis rotation by using the LinearJoint Axis reference instead
– did not work
– similarly changed value, but did not have platform follow it

Thoughts for Further Tests:

– Might be worth seeing if this Axis is fed into the Signal system of the platform prefab main parent object
– Could be something where once it’s set in the Signal system, it’s set for the platform’s life
– Maybe could prevent this reference from happening until the Axis rotation is properly set

– Could be the LinearJoint reference for Axis that takes in the Axis transform
– this may get set once initially at Start and never be looked at again
– TESTING
– 1) Removing this reference at run time
– didn’t do anything
– platform kept moving as it was
– supports that this is just used once to set initial direction
– 2) Removed reference before playing in editor
– immediately grabbed a reference to the Axis transform anyway
– appears to be scripted to grab its own transform on Start
– POSSIBLE SOLUTION:
– could possibly use this by removing the LinearJoint on the prefab, so then we could set the
Axis transform and then add the LinearJoint so it would take on the Axis transform after being edited
– 3) Removed LinearJoint component from prefab, then in script, edited Axis transform, then added LinearJoint component,
then set the LinearJoint values
– This setup the object correctly (components were in the right place) but the platform did not move
– The LinearJoint did not take on the Axis transform automatically as assumed, and it was also missing the parent object
reference as the body to move
– Next: try setting those references manually in script as well
– 4) Tried adding LinearJoint with script as well as setting more values in script
– added the axis transform and the parent body object manually
– still did not move

– NOTES:
– from testing, found that editing LinearJoint values such as minValue, maxValue, maxSpeed, maxAcceleration at run time even work,
so it makes sense they were working sooner as they appear to be fine to edit at any time