MovingPlatform with Target Movement Type

gurayg

New member
Hi Justin,
I'm trying to make Moving Platforms work. I'm having some trouble with how to start them from code.

I have a script for calling an elevator to a floor. A floor is a Path(Waypoint struct) array index.
When player interacts, I'm just setting "TargetWaypoint" to the desired index and then calling "Move();"
but I couldn't make it work.

Maybe I'm not getting it but "UpdatePath" checks for "m_MovementType != PathMovementType.Target" and it should call "UpdateWaypoint" with switch case.
Even if "UpdateWaypoint" gets called, "PathMovementType.Target" is not getting the target waypoint from the array. It is just calling "GoToNextWaypoint"

I'm a bit confused on this.
Can you please help me?
 
Move does the actual movement and is used by ISmoothedObject so you don't want to call Move. Instead you should have the platform disabled and after the target is set you can then enable the component.
 
Hmm, it seems there is more to my problem.
Even when I did what you said, I was having problems.
The moving platform was moving to a position (not any of the waypoints) at start.
If anyone else is having such a problem, check if you have a "Kinematic object" component on the moving platform Game Object.
I've added and forgotten it while testing. After removing it and with Justin' help, I was able to call moving platforms to floors like elevators.

One more thing.
I've written a short code to remind others that may use Target mode.
Justin if you think it is might help here it is:

C#:
//for line 85 of MovingPlatformInspector.cs
    if (movementType.enumValueIndex == (int) MovingPlatform.PathMovementType.Target)
    {
        EditorGUI.indentLevel++;
        EditorGUILayout.HelpBox("To use Target mode in code, set Enable On Interact to true on component , in code: set the Target Waypoint and enable the component.", MessageType.Warning);
        EditorGUILayout.PropertyField(PropertyFromName("m_TargetWaypoint"));
        EditorGUI.indentLevel--;
    }
 
Spoke too soon.

My waypoints have same X and Z (think 2 floors of a building) but different Y values.
At start of the game, if the moving platform has an X offset (Y=0) and you call it to an index it first goes to the closest index and to the index you called.
But if the moving platform has only Y offset (like it is positioned in between floors) when you call it, it moves to a totally different position.
This might be bug or this might mean moving platforms need to start at zero position at start.

Same thing happens at the start of the game if I call the moving platform to the floor that it is on. It shouldn't move but it moves to a wrong position.
I can only guess that it has something to do with how moving platform position is being cached at start in relation to waypoints. (just a guess)
 
KinematicObject is an old component that is no longer used. Moving platforms don't need to start at the 0th position - in the demo scene we have a few Moving platforms that start in a unique position. Can you determine what is causing the moving platform to move in a different direction?
 
I think I now understand what is going on. It is not a bug, it is happening because of how I create the level in code.
After I instantiate the Elevator prefab, I have to move it to its actual position.
But it is too late, because Awake has been called and I think Moving Platform caches some positions in Awake.

You can actually force it in the demo scene.
Turn the "Elevator" in demo scene into a "Target".
After game starts move "Elevator" transform' position.
When you enable the component, it should move to wrong position.
But as I said, it is not a bug.

You may remember this happened with Interactable too.
I was adding Interactable component to some Game Objects at runtime and its Awake was caching an array immediately.
(Before I could set some properties)
I think a similar thing is happening here.

Do you think something can be done to those 2 components?
 
One more thing.
I "might" get away with my Moving platforms problem by using Instantiate(GameObject, Vector3, Quaternion) method.
I'll have to test it, but if I set prefab' position while calling Instantiate and not set its position after it' been instantiated, I might still use Moving Platform as is.
But my problem with "Interactable" is different.
And having an "UpdateCache" method might help other users in future.
 
Back
Top