title: Stay on Moving Platforms
body: <p>I'm writing a 2D Platformer in Unity and I'm trying to get the player to stay on a moving platform. I've done searching and tinkering for a day or two now, and I'm not having any luck.</p>
  
  <p>Basically, I've been told to try to keep the character moving with the platform when they are touching.
  Firstly, if I use anything related to OnTriggerEnter(), the player goes right through the platform. If I do OnCollisionEnter() (with a CharacterController on the player and a BoxCollider on the platform), nothing happens at all. These are two things I've found suggested most. The other is parenting the player with the platform, but this apparently causes "problems" (frequently stated, never explained).</p>
  
  <p>So, how can I get the player to stay on the moving platform? Here is the code for the moving platform:</p>
  
  <pre><code>public class MovingPlatform : MonoBehaviour
  {
  private float useSpeed;
  public float directionSpeed = 9.0f;
  float origY;
  public float distance = 10.0f;
  
  // Use this for initialization
  void Start () 
  {
      origY = transform.position.y;
      useSpeed = -directionSpeed;
  }
  
  // Update is called once per frame
  void Update ()
  {
      if(origY - transform.position.y &gt; distance)
      {
          useSpeed = directionSpeed; //flip direction
      }
      else if(origY - transform.position.y &lt; -distance)
      {
          useSpeed = -directionSpeed; //flip direction
      }
      transform.Translate(0,useSpeed*Time.deltaTime,0);
  }
  </code></pre>
  
  <p>AND here is the code for the Player's movement (in Update):</p>
  
  <pre><code>CharacterController controller = GetComponent&lt;CharacterController&gt;();
      float rotation = Input.GetAxis("Horizontal");
      if(controller.isGrounded)
      {
          moveDirection.Set(rotation, 0, 0); //moveDirection = new Vector3(rotation, 0, 0);
          moveDirection = transform.TransformDirection(moveDirection);
  
          //running code
          if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) //check if shift is held
          { running = true; }
          else
          { running = false; }
  
          moveDirection *= running ? runningSpeed : walkingSpeed; //set speed
  
          //jump code
          if(Input.GetButtonDown("Jump"))
          {
              //moveDirection.y = jumpHeight;
              jump ();
          }
      }
      moveDirection.y -= gravity * Time.deltaTime;
      controller.Move(moveDirection * Time.deltaTime);
  </code></pre>
  
  <p>EDIT: I think it might have to do with how I'm defining the player and the platform, but I've tried different combinations. If the platform is a trigger (on its collider), the player goes through it consistently. If not, I can't use OnTrigger functions. I have a rigidbody attached to both the player and the platform, but it doesn't seem to affect anything. When the player DOES get on the platform in some setups, he jitters and often just ends up falling through.</p>