How to compensate for moving platforms that move side to side

Question 694

I followed the exact way that was described in the link. It works great with vertical moving platforms. However, when I got my character to a platform that was moving side to side, the character didn't move with the platform. Is their a way to compensate for this?

I got the simplest solution for this problem from another forum.

So basically, you create another object with collider on top of your normal platform, and parent it to your platform.

Apply this script to your collider object, and it should work.

function OnTriggerEnter (other : Collider) { other.transform.parent = gameObject.transform; } function OnTriggerExit (other : Collider) { other.transform.parent = null; }

Create an empty GameObject. Add a mesh collider in the shape of your platform, and make it a trigger. Then make it a child of your platform/elevator (it works for both), and raise it so it is just a little higher then the platform itself. Then attach this script to the trigger you made:

var Player : Transform; 
var Elevator : Transform; 

function OnTriggerEnter (other : Collider) {
Player.parent = Elevator; 

function OnTriggerExit (other : Collider) {

Make the trigger GameObject in the Elevator slot and your player in the Player slot. When your player gets on the moving platform, he/she will not slide off and will move with the platform perfectly in any direction.