Moving Platforms in Unity
Creating some moving platforms for my 2.5D game in Unity
Objective: Implementing moving platforms for my 2.5D game.
To get things going, for my implementation I started by creating an empty GameObject and named it MovingPlatform. Within this newly created object, I gave it a cube primitive with a scale transform of (5,1,1) and two empty GameObjects named TargetA and TargetB. On cube primitive, I attached a custom C# script I named MovingPlatform. This is the code I used to get my platforms moving:
This gave me the following result:
As you can see, with this implementation, the platforms can move both vertically and horizontally. Unfortunately, the player does not stick to the horizontally moving platform, so let’s fix that.
Getting the Player to Stick to the Platform
To get the player to stick, I changed the code in MovingPlatform to what I am showing here below:
The biggest changes I made were adding the OnTriggerEnter and OnTriggerExit methods, as well as changing the Update() method to FixedUpdate() so we can take into physics calculations. This gave the result we expect for our player to move with the platform when on it.
That’s all for today! Thanks for reading! In the next article, I will implement some lives for our players.