How to make object get affected by a moving object?

I am making a game that the player run’s on top the the train. However the player is not affected by a moving grounds and just stand on top of it. How can I make it so that the player get affected by a moving train?

Sorry but this is a bit like asking a question like “I once saw a blue car in New York City, who owns it?” There are not nearly enough details to answer your question with this little information. There are so many ways that a player and train can be implemented and we can’t know your specific case with the information you provided.

Take a look at this video for information on how to write good questions The Unity Answers Site - YouTube and read this Books — Matt Gemmell then hopefully you can provide enough information for us to understand how to help you.

I put my code in a comment section

Great, now it’s much easier to help. A simple solution to this is to set the player to have the train as its parent transform when the player collides with it and remove the trains parent status when the player leaves. Setting a parent means that any movement of the parent will also move the child in the same way. I added some example code here, written from my phone so there may be some minor errors but the general idea should be clear

Make sure your train has a RigidBody, if it does not have one. Mark the RigidBody as Kinematic so that it’s not affected by physic forces like gravity

To your train script add an OnCollisionEnter and OnCollisionExit function todo that, like this:

private void OnCollisionEnter(Collision collision)
    Debug.Log("Player collided with train");

private void OnCollisionExit(Collision collision)
    Debug.Log("Player left  the train");