Player slips off moving platform

so i have Player in 3D game and i made platform that moves in X axis with this script:
var Speed : float = 2;
var NewPos : Vector3;
private var StartPos : Vector3;
private var GTime : float;
private var IsThere : boolean;

    function Start () {
    StartPos = transform.position;
    function Update () {
    if (GTime <= 1 && !IsThere) { GTime += Speed * Time.deltaTime; }
    if (GTime >= 1) { IsThere = true; }
    if (GTime >= 0 && IsThere) { GTime -= Speed * Time.deltaTime; }
    if (GTime <= 0) { IsThere = false; }
    transform.position.x = Mathf.Lerp (StartPos.x,NewPos.x,GTime);
    transform.position.y = Mathf.Lerp (StartPos.y,NewPos.y,GTime);
    transform.position.z = Mathf.Lerp (StartPos.z,NewPos.z,GTime);

but my player dont follow it when he is on it,how do i fix it?

Two options:

  • Parent the player to the platform when they are grounded on it and unparent them when they are grounded on a new platform or simply when they jump

  • Use physics. If you’re using the built in rigidbody physics, keeping one body on a moving platform is a matter of playing with friction on the physic material

You could also maybe try a more programmatic solution where you don’t change the transform parent, but just also apply the movement of the platform to the player while they’re standing on the platform.