I recently needed to create some moving platforms for this 2.5D platformer I’ve been working on for the GameDevHQ course. The main aims were to first implement the main functionality (in this case, move between two waypoints and seem to ‘carry’ the player across) and second to ‘modularise’ it (save it in a way which makes it easy for me to re-use and apply elsewhere).
Objective: Implement the main functionality of the moving platform and modularise it for use elsewhere.
1.To help us with modularising this, we can start by creating an empty GameObject in the scene called ‘Moving_Platform’. This will be the parent object for the main components of our moving platform setup. Secondly, create the platform itself as a child (in this case, a cube stretched out should suit us nicely). We can then duplicate this component twice, remove the MeshRenderer and other components from them, and rename these to points A and B respectively. Try to keep all of these objects zeroed out on their Transform components, as this’ll help us with modularising this later.
2. Next, add a second BoxCollider to the platform itself, set isTrigger to true, and increase its center on the Y axis a little. This is what we’ll be using to detect when the player is on top of the platform (i.e. when they trigger it via the OnTriggerEnter method).
3. Create a new ‘MovingPlatform’ script and attach it to our platform GameObject. Inside the script, we’ll want to use FixedUpdate instead of Update for consistency (and to avoid a jittering effect when using it to move our character). I’ve opted for using a boolean called ‘_moveForwards’ which will be determining whether the platform is moving towards targetB’s position or targetA’s position. I’ll be moving the platform along using the Vector3.MoveTowards method and will also be checking to see whether we’ve reached our current destination within FixedUpdate.
As part of how I like to organise my code at the moment, I’m using a dedicated MoveToPosition method (which takes in a Vector3) and a dedicated CheckPositionReached method (which takes in a Vector3 and returns a boolean value which depends on whether the current position is equal to the specified one). If CheckPositionReached returns true, I’m then flipping the value of the _moveForwards bool using this handy little line of code: ‘_moveForwards = !_moveForwards’.
For organisation purposes, I’m also opting for using a couple of #region preprocessor statements to group my code (between that which handles the movement of the platform and that which detects the player).
4. Let’s finish up our work on this script by adding and filling in the OnTriggerEnter and OnTriggerExit methods. We should be checking to see whether the tag of the object that just entered is “Player” (and the Player GameObject should be tagged accordingly in order for this to work) and, if it does, we should either set the transform of that GameObject to this object’s transform or to null. This is all we need to do to get ourselves to be pulled along by the platform: parenting and unparenting the player object to the platform itself (whenever they’re on top of the platform).
I’ll include the full code for this below:
5. Head back over into Unity, let it compile, and drag in our point A and point B objects to the public Transform slots. We can now drag the entire Moving_Platform object into our Assets/project view and save it as a prefab. And with this done, our moving platform should be ready for us to use and implement in our game! All we need to do is reposition Point A and/or Point B accordingly and the platform will effectively ping-pong between them 😉 Since we’ve prefabbed and modularised this, we can just drag and drop this platform into our scene from the Assets folder and use it wherever we want (diagonally, vertically, horizontally, etc.).
Assuming our player is tagged correctly and has their CharacterController component (or, if not, a collider and RigidBody component), our player should be getting transported as intended.