Moving player while gameobject is moved ?

Hello

I’m creating a game, and I created an bridge that move to right and left.
I want my player when colliding with the bridge go along on the move.
I’m trying set position player with position of the bridge, but when my player is on the bridge his doing small jumps

How can I do this ?

I’m trying this.

public class MoveBridge : MonoBehaviour {

    private bool isLeft = false;
    public float speed = 5f; 
    public float delaySpeed; 
    private float moveTime;

    public GameObject player;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	    move();
	}

    private void move(){
        moveTime += Time.deltaTime;

        if (moveTime <= delaySpeed){
            if (isLeft){
                gameObject.transform.Translate(-Vector2.right * speed * Time.deltaTime);
            }else{                
                gameObject.transform.Translate(Vector2.right * speed * Time.deltaTime);                
            }
        }else{
            isLeft = !isLeft;
            moveTime = 0f;
        }
    }

    
    void OnCollisionStay2D(Collision2D coll){       
        if(coll.gameObject.name.Equals("PlayerObject")){            
            player.transform.position = gameObject.transform.position;
        }        
    }
     
}

Add this function at the end.

   void OnCollisionStay2D(Collision2D
 coll){       
         if(coll.gameObject.name.Equals("PlayerObject")){
 
            coll.gameObject.transform.parent = transform;
         }

Your player will become a child of the bridge and inherit its traits including tranform.position.
Just make sure you set coll.gameObject.transform.parent = null when you no longer want the player to move with the bridge.

Cheers!

Note: setting the parent object of a gameObject can carry on after it’s called so you can set it during OnCollisionEnter2d(). There’s just that much less strain put on your CPU when you do that.