How can I move an object along with another object?

I am new at 3d programming
I have a scene that contains two object:

  1. A kinematic cube attached to a script
    that will moving back and forth
    according to Z-axis

  2. A cylinder with mesh collider on top of the cube

Both of them have a rigid body with gravity.

However, the cylinder is not moving along with the cube, I have no idea how to make it move along with the cube
Can anybody five me some tips? Thank you!

Updated: I would like to have the effect just similar to the game “Coin Dozer”, where adding a cylinder on the moving platform will make it move along with the cube, and may drop down from the cube and push by it.

It sounds like you want the two objects to move together, as one single object? If so, you might want to try a setup like this:

  • Parent object contains rigidbody and collider
  • Child object for the cube renderer
  • Child object for the cylinder renderer

You can set up parent-child relationships by dragging objects onto each other in the scene hierarchy. When a parent moves, its children move with it.

Apologies if that’s not the sort of setup you’re looking for.

You should add the cylinder as a child game object of the cube, and remove rigidbody component from the cylinder.

Children ridgid bodies must also be Kinematic if they are expected to be “attached” to something up the parent chain. Check IsKinematic on them and they will stick to another ridgid body.