Moving Platform
Objective: script a moving platform with modular path
Another evergreen platform component is the moving platform. We are going to create and script it in a modular way, so we can easily duplicate its behaviour and just change the waypoints defining the path.
Yes, waypoints. In fact I’m going to reuse/rethink a large part of the code I wrote for the patrolling guards (here and here) in my previous game The Great Fleece.
Let’s design something first:
- The platform will move as smooth as possible from a waypoint to another with a given speed, stopping (or not) at each waypoint and making a longer pause on the first and last ones.
- The player must be well anchored to the platform, unless it jumps off, meaning that it will move with the platform
And that’s it.
Let’s start from the second point, which is way simpler. To make the player stick with the platform, the easiest way is to make it a child of the platform the moment the player lands on it and take it out when jumping.
In order to achieve this kind of behaviour, we need to add another box collider component to the platform, a trigger, leaning out of the platform in order to detect the landing. Of course we need a rigid body, with no gravity.
To make the collider lean out we can just change the y component of the centre. We also need to freeze x-y-z rotations to avoid unwanted strange colliding behaviours with the character.
Now the script, MovingPlatform.
We set the player child to the platform, or not, on trigger enter, or exit.
Now let’s look at the movement itself. We are going to perform it in the FixedUpdate()
method that ensures a consistent timestamp, resulting in a smoother effect.
WaypointsGood()
method returns a bool saying if there are waypoints in the list and it is safe to access them. Then at line 5
we check if the current waypoint has been reached.
If yes, we evaluate the next waypoint (7
)and if the platform is in the first or last waypoint we start a coroutine to make it wait, otherwise we make it wait for a shorter time, if the wait-on-waypoint option is enabled.
If the waypoint has not been reached yet, the platform moves, 16
.
The next waypoint is evaluated by changing the id, or list index. But if the limits are reached the direction of the path changes so from 0 to max the index is incremented, then starting from max is decremented and so on.
The platform is stopped with this smart trick. Since we have no possibility to wait and stop the movement from inside the FixedUpdate()
we don’t actually stop it! We just put the speed to zero with a coroutine! Inside the coroutine we can wait and then restore the speed.
The actual motion is performed via the MoveTowards()
method of Vector3 class.
Once the script is finished, we just need to properly populate it from the inspector.
Being the waypoints just transform components of empty objects.
Now, we can play and get fun by adding, removing or replacing the waypoints and have completely new paths: horizontal, vertical or diagonal!
Let’s look at the results.