Creating Moving Platforms For Your Game In Unity
With any platformer game, you gotta have PLATFORMS! And not every platform has to be the same. Some may sit static, others might fall when stepped on. You could make platforms made of quicksand that starts sucking the player in as soon as they land on it.
You could create a “Moving Platform”!
But just making a platform move left and right or up and down isn’t enough. If the player doesn’t move with it then what kind of platform would it be!
To start let's make a cube game object and stretch it out until we're satisfied with its length/shape. Add a “Rigidbody” and make sure to turn off gravity.
Then add a second “Box Collider” to this object but mark this one as a trigger and raise it above the object a little.
Now create two empty game objects and position them in the start and end positions that you want your platform to move between.
Next, create a new C# script and attach it to the moving platform object that you just created.
Add this to your moving platform script and drag and drop your references to the two empty game objects you created.
This will move your platform!
But the platform just slides out from under the player if you stand on it…
Add this to the script to add/remove the player game object as a child of the moving platform when you step on it and when you get off. This sticks the player to the platform!
And there you have it, “Moving Platforms”!
- Removing the “Mesh Renderer” on your platform will make it an “Invisible Platform”!
- You can change the target points that the platform moves between to be above or below one another to make a vertically rising platform or even diagonal!
- Add a multitude of points in your scene and expand upon your script to account for them and you can move your platform all over! This is the foundation of basic “AI Navigation”.