Creating moving platforms
Objective: Allow the player to jump on and off a mobile platform.
Create an empty game object called Mobile_Platform. Create two empty game objects named Point_A and Point_B and child them to the Mobile_Platform. Add a 3D object cube, Moving_Platform, with the following components and child it to the Mobile_Platform, so that the hierarchy looks similar to that shown below.
Create a C# script named MovingPlatform and add it to the Moving_Platform child object, not the parent. The script needs four variables _pointA, _pointB and the current _target for the Moving _Platform, and a float _speed. The _target is set to _pointA in void Start().
Using FixedUpdate() move the platform to the _target using Vector3.MoveTowards. Check to see if _pointA is reached using Vector3.Distance method, if so change target to _pointB, and visa versa for _pointB if not at _pointA, as shown below. Use void FixedUpdate(), not void Update(), because of the physics of jumping on and off the platform. The time step of FixedUpdate is constant to allow precise physics calculations; whereas, Update varies the time step frame to frame based on processing load and computer speed.
The physics of landing on the platform is handled using OnTriggerEnter, shown below, and if the other object is Player then the Player is made a child of the Moving_Platform to which the script is attached.
To jump off the platform, use OnTriggerExit to reverses the process, removing the parent object for the Player, if they leave the Moving_Platform.
Point_A and Point_B are are assigned in the inspector, as shown in the Moving_Platform, shown below.
Now that the script is done, finish setting up the Moving_Platform’s required components. Create a new material and color it green so we can easily distinguish the moving platform from a stationary one. Add a rigidbody without gravity to enable a trigger event. Add a second box collider is uses as the is trigger. In Unity when a collider is a trigger it does not act as a physical barrier, so two box colliders are needed, one for the trigger and one for the player to land on. In the is trigger box collider, change center Y to shift the collider a little above the other collider so that it look like the image below. If the offset is too small the Player will not activate the trigger event
On the Moving_Platform child, change the transform scale X to be 5 so that it is a cuboid. Set the transform positions for Point_A, Point_B and Mobile_Platform to X 0, Y 0, Z 0 then drag the parent Mobile_Platform into the Prefabs folder. Delete the original object and drag the new Mobile_Platform prefab back into the scene. In the scene, move the parent Mobile_Platform position to X 14, Y -1, Z 0. Point_A should be X 0, Y 0, Z0 and change Point_B to X 12, Y 0 and Z 0, which assigns destinations for the platform to move between. This modular approach allows multiple Mobile_Platforms to be easily created, each with there own destinations.
Check to make sure the player has the following components. Notice the capsule collider has been removed. If the player has both a capsule collider and a character controller then the OnTriggerExit function will not work.
The player can now jump on and off the mobile platforms. There are three key learnings. Do not have a capsule collider and character controller on the Player; otherwise, OnTriggerExit wont work. To allow OnTriggerEnter to function, two box colliders are required on the Moving_Platform, one for physics and the other as a trigger, with the trigger offset a little above the platform. A rigidbody is required on the Moving_Platform to enable a trigger event. Do not put a rigidbody on an object with a character controller, for example the Player, as these are both physics components with different functions.