movement inside a train problem

Hello, i’m having a big problem with a moving platform, well it’s not a platform it’s a train, wich has this script.

       function OnTriggerEnter(col: Collider)
{
    if(col.tag == "Player")
    {
       col.transform.parent = transform.parent;
      
    }
}

function OnTriggerExit(col: Collider)
{
    if(col.tag == "Player")
    {
       col.transform.parent = null; 
    }
}

this script just parents my player to my train when the player is inside the train, but since i have to use a rigidbody for my player so the game can detect collisions between a sword my character has and enemyes, when my characters gets out of the train he still continues to move with the train, so i don’t know what to do, i think it would be nice to have a solution for moving platforms, or in my case trains, that does not require parenting in order to work, but i have no idea on how to use other kinds of solutions.

So anybody knows why my character continues to move when he’s outside of the train?

or does anyone knows if there’s a better solution on moving platforms, trains or vehicles other than parenting?

There is a solution to this. You can add a momentum vector to your player, which is set whenever you touch the ground, and depends on the ground you touch. First of all, assign a rigidbody to the train (kinematic if you don’t want actual physics on it). You can then raycast into the ground from your character, and if the character has a rigidbody, use rigidbody.GetPointVelocity(hit.point). This gets the current velocity of a specific point on the rigidbody collider. By setting the momentum value equal to this, you will find the player moves along with the train, and by resetting it when you land on other ground, you’ll stop that momentum when the player leaves the train.