Other Unity articles
Simple system for moving platforms (not only!)
Traversing GameObjects hierarchy
Simple system for moving platforms (not only!)" we created simple system that allowed us to move platforms or other objects along path defined with path nodes. This path had visual representation in editor and the system had some parameters that allowed us easy parametrization (waiting in nodes of our choice, setting speed and speed modifiers, moving in ping-pong way, loops or only once, setting movement type like linear, cos, quadratic ...).
One of the features was ability to send events when platform or any other object following path arrived to destination path node or when leaving current path node. Originally this event was defined like stupid enum - just listing of some generalized event names:
In this tutorial we will change this enum to regular event object. The result animated above shows small dust animation when platform is hit. This animation is initiated with event. Each event will have also some parameters as pre-delay and post-delay and also it will have possibility to point to next event. In this way we can initiate chains of events. Here is how our inspector for PathNode will change:
You can download final source files here. We first change two public variables holding enum values in PathNode.cs to this:
This will cause small change in our existing PathFollower class where we have to change OnLeaveEvent and OnArriveEvent like this:
Now we need to create new class. This class will name PathNodeEvent. Its properties will be delay before event is processed and time after the event is processed. The delay after event may seem unnecessary, but there is one more property - next event (again PathNodeEvent or derived class). The delay after processing current event says how much time to wait before processing next event. The event chain is stopped when next event is null.
Here is listing of the beginning of PathNodeEvent class:
And here is rest of this small, but powerful class:
From the PathFollower class you see that ProcessEvent is called. The method is virtual so we can override it in subclasses. The implementation here is default. What it does is, that coroutine (Process) is started. There is first processed delay before event, then event itself - DoProcess. If some delay after event is entered, it is processed next and finally we check if there is any next event.
This class does not do anything interesting except it prints message to debug console. Most of the real event will be subclasses of this class. You can override ProcessEvent if you are not interesting in delays or in chaining (for some simple event) or you can override DoProcess and you will have all the features described above.
So, let's subclass it with simple SpawnEvent. The class is note derived from MonoBehaviour, but from our PathNodeEvent:
This type of event is very simple, so we override directly ProcessEvent and are not interested in delays. It has two additional public properties - Spawn Point and Spawn Object. Spawn object is "something" that will be spawned at "point". the inspector view looks like this:
It means, you have to create event object somewhere in the scene. Spawn point can be any other Transform in the scene. SpawnObject can be prefab, which is very useful. To save one one GameObject in scene, the event here has its own Transform as spawn point - it is pointing to itself. Object in scene are arranged like this:
tutorial. The blue circle is event. The spawn object I am pointing to is short dust animation. When running it. you will get the result already shown in the beginning:
Download (4 kb) here
As you can imagine, the original PathFollowing system in connection with events can have lot of usages. I think it can also be used for some character animation scenes. Good thing is that both, the path system and events are easily extensible.