WIP 03a | Movement within a Motion Web
Goal: Learn how to create and use a Motion Web.
Create a web of Splines
In order to create a Motion Web, you need to create a network of Splines. The Splines describe the possible route an Actor can take to move from point A to point B. When you use multiple Splines, you can use Junction Cues to connect the Splines.
Create web destinations
For an Actor to move to a specific position on the Motion Web, destinations need to be assigned to it. These can be created using a DES Cue. Create a new DES Cue and position it on the position of destination on a Spline. Make sure to give the destination a clear and unique name.
Create a Motion Web Actor
Create a new Game Object to function as the Actor. Add the MotionWebActor component to it. You can either manually set the Starting Node (destination DES Cue) or have the Actor find the nearest Node upon simulation start. At the start of the simulation the Actor will be set its Starting Node’s position. Motion Modes and Types can also be set for the Motion Web Actor.
Create a MotionWebInstructor
Create a new script and have this class inherit from the MotionWebInstructor class.
using u040.prespective.prescripted.des.motionweb;
public class TutorialMotionWebInstructor : MotionWebInstructor
{
}
Add Motion Web Instructor to the scene
Create a new Game Object and add the created Motion Web Instructor component from the previous step.
Structure object hierarchy
In order for the simulation to function properly, all DES components should be a child of the DES Controller. In a similar way, all Motion Web components should be be parented under the Motion Web Instructor to be part of that Motion Web.
Re-index Motion Web
Each time something is changed about the Motion Web hierarchy, the Motion Web needs to be updated. This step is required for the Motion Web to index its (new) Splines and Actors.
Instruct the Motion Web Instructor to move an Actor over the Motion Web
At this point we already have a Motion Web set up that is complete and ready to use. The only thing left to do is to give it an instruction.
You can instruct the Motion Web Instructor from any scene object as long as you have a reference to the Instructor. Best practice is to do this from within the created Instructor script itself since we already created a custom script for it.
For this tutorial, we just want to start an instruction at the start of the simulation. Lets override the OnSimulationFrameUpdate method of the Motion Web Instructor.
public override void OnSimulationFrameUpdate(double _passedFrameTime, double _totalFrameTime, long _frameID, int _framePass, ref List<DESEvent> _resultingEvents, ref List<DESRecord> _resultRecords)
{
base.OnSimulationFrameUpdate(_passedFrameTime, _totalFrameTime, _frameID, _framePass, ref _resultingEvents, ref _resultRecords);
}
In order to instruct the motion web instructor to move an actor through the web we need to generate a MotionWebInstructionSequence.
System.Func<double, MotionWebInstructionSequence, bool> onDoneWithAssignment =
(double _passedTimeAfterAssignment, MotionWebInstructionSequence _sequenceComplete) =>
{
UnityEngine.Debug.Log("Motion sequence done!");
return false;
};
MotionWebInstructionSequence sequence = new MotionWebInstructionSequence()
{
SequenceID = "TestMove",
OnSequenceComplete = onDoneWithAssignment,
InstructionSteps = new List<MotionWebInstructionStep>()
};
Here the action set as OnSequenceComplete
will be executed when the sequence is completed.
Furthermore, we need to define the step to fulfill.
A few parameters needs to be set for the instruction:
The name of the instruction step. Make sure there are no duplicate names within the sequence. These names are used to reference steps by when applying an order of execution to the steps.
The velocity at which the Actor will move during this step.
The Actor that will perform this step.
The name of the (destination) Cue the Actor will move towards.
Once all these parameters are set we need to start the instruction sequence.
This completes instruction.
Since we call this instruction in the OnSimulationFrameUpdate, we still have to account for 2 things. Firstly we only want to apply the instruction step a single time and not every frame. This is resolved setting a boolean to true before execution and setting it to false after execution. Secondly, we have to be sure that the Motion Tensor of the Actor exists. This makes we also have to check if the passed frame is not the first frame. Including these checks, concludes the script. The entire script is given below.
Entering Play Mode shows that the Actor moves to the given Spline position.
Play around with the Cue names, velocities and include extra steps to get the hang of it. Make sure when you include extra steps to also define the preceding steps of the actor. Otherwise the Instructor will try to execute both steps at the same time. An example is given below. You can also find an example setup in Prespective scene “3.a Movement within Motion Web“.
Prespective Documentation