player is not moving with the moving platform

please watch the video.

it’s the code,I attached it to the player.

void OnCollisionEnter2D(Collision2D other){
	if (other.gameObject.tag == "movingPlatform") {
		transform.parent = other.transform;
		moveWithPlatform = true;
	}

}
void Update(){
if (moveWithPlatform) {
		rig.velocity = new Vector3 (rig.velocity.x,0);
	}
}

Hello @PersianKiller. I made some modifications to your script, and I managed to get it to work. Take a look at the script:

public GameObject character;

private Rigidbody2D rig;

private float speed = 10f;

private bool moveWithPlatform = false;
private bool moveRight = false;

Vector2 newPosition;
Vector2 oldPosition;

void Start()
{
    rig = GetComponent<Rigidbody2D>();
}

void Update()
{
    newPosition = transform.position;

    if (moveRight == true)
    {
        rig.velocity = new Vector2(speed, 0);

        if ((transform.position.x) >= 2f)
        {
            moveRight = false;
        }
    }
    else
    {
        rig.velocity = new Vector2(-speed, 0);

        if ((transform.position.x) <= -2f)
        {
            moveRight = true;
        }
    }

    float difference = newPosition.x - oldPosition.x;

    oldPosition = transform.position;

    if (moveWithPlatform == true)
    {
        character.transform.position += Vector3.right * difference * Time.deltaTime;
    }
}

void OnCollisionEnter2D(Collision2D other)
{
    if (other.gameObject.tag == "movingPlatform")
    {
        moveWithPlatform = true;
    }
}

Take note that both of my Character object and the platform had a RigidBody2D() component and they both had colliders not set as triggers.

Also, don’t forget to set the moveWithPlatform to false (when the character jumps) so the character won’t move while it’s jumping.

I don’t know how you move your platform, but I wrote my own code to work in a way similar to how yours work.

Let me know how it goes.

I did some changes and now it’s working fine.

// Moving Platform’s script

ublic class MountainMovingPlatfrom : MonoBehaviour {

public float speed;
public float firstPosX;
public moveMent player;
public float maxDistance;
public float playerSpeed;
public bool moveToRight;
// Use this for initialization
void Start () {
	firstPosX = transform.position.x;
	player = FindObjectOfType<moveMent> ();

	moveToRight = true;
}

void Update(){
	if (moveToRight) {
		transform.Translate (speed*Time.deltaTime,0,0);
	}
	else if(!moveToRight){
		transform.Translate (-speed*Time.deltaTime,0,0);

	}

	if (transform.position.x > firstPosX + maxDistance) {
		moveToRight = false;
	}
	else if(transform.position.x < firstPosX - maxDistance){
		moveToRight = true;
	}

}

void OnCollisionEnter2D(Collision2D other){
	if (other.gameObject.CompareTag( "player")) {
		player.transform.parent = transform;
	}
}

 
void OnCollisionExit2D(Collision2D other){
	if (other.gameObject.CompareTag( "player")) {
		player.transform.parent = null;
	}
}

}

so when the player lands on the platform , he can move with it, and when he jumps ,he’s not moving with the platform any more .

https://streamable.com/a9wl8

  1. Player should not be the child of the Platform.
  2. Player does not move along with Platform. Platform has moved Player.
  3. Do not use Update(), use OnCollisionStay2D.
  4. Make sure that the Player and Platform has Collider component is not isTrigger.
  5. Make sure the tag is spelled correctly.

Specifically, you need to write a script attached to the Platform that will move the GameObject with the “Player” tag, using OnCollisionStay2D, it which is called each frame when there is a collision.